ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
ImageWidget.cpp
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // - CloudViewer: www.cloudViewer.org -
3 // ----------------------------------------------------------------------------
4 // Copyright (c) 2018-2024 www.cloudViewer.org
5 // SPDX-License-Identifier: MIT
6 // ----------------------------------------------------------------------------
7 
9 
10 #include <Image.h>
11 #include <imgui.h>
12 
14 #include "visualization/gui/Util.h"
15 
16 namespace cloudViewer {
17 namespace visualization {
18 namespace gui {
19 
21  std::shared_ptr<UIImage> image_;
22 };
23 
25  impl_->image_ =
26  std::make_shared<UIImage>(std::make_shared<geometry::Image>());
27 }
28 
29 ImageWidget::ImageWidget(const char* image_path)
30  : impl_(new ImageWidget::Impl()) {
31  impl_->image_ = std::make_shared<UIImage>(image_path);
32 }
33 
34 ImageWidget::ImageWidget(std::shared_ptr<geometry::Image> image)
35  : impl_(new ImageWidget::Impl()) {
36  impl_->image_ = std::make_shared<UIImage>(image);
37 }
38 
39 ImageWidget::ImageWidget(std::shared_ptr<t::geometry::Image> image)
40  : impl_(new ImageWidget::Impl()) {
41  impl_->image_ = std::make_shared<UIImage>(image);
42 }
43 
45  float u0 /*= 0.0f*/,
46  float v0 /*= 0.0f*/,
47  float u1 /*= 1.0f*/,
48  float v1 /*= 1.0f*/)
49  : impl_(new ImageWidget::Impl()) {
50  impl_->image_ = std::make_shared<UIImage>(texture_id, u0, v0, u1, v1);
51 }
52 
53 ImageWidget::ImageWidget(std::shared_ptr<UIImage> image)
54  : impl_(new ImageWidget::Impl()) {
55  impl_->image_ = image;
56 }
57 
59 
60 void ImageWidget::UpdateImage(std::shared_ptr<geometry::Image> image) {
61  GetUIImage()->UpdateImage(image);
62 }
63 
64 void ImageWidget::UpdateImage(std::shared_ptr<t::geometry::Image> image) {
65  GetUIImage()->UpdateImage(image);
66 }
67 
68 std::shared_ptr<UIImage> ImageWidget::GetUIImage() const {
69  return impl_->image_;
70 }
71 
72 void ImageWidget::SetUIImage(std::shared_ptr<UIImage> image) {
73  impl_->image_ = image;
74 }
75 
77  const Constraints& constraints) const {
78  Size pref;
79  if (impl_->image_) {
80  pref = impl_->image_->CalcPreferredSize(context, constraints);
81  }
82 
83  if (pref.width != 0 && pref.height != 0) {
84  return pref;
85  } else {
86  return Size(5 * context.theme.font_size, 5 * context.theme.font_size);
87  }
88 }
89 
92 }
93 
95  auto& frame = GetFrame();
96  UIImage::DrawParams params; // .texture defaults to kBad handle
97  if (impl_->image_) {
98  params = impl_->image_->CalcDrawParams(context.renderer, frame);
99  }
100 
102  ImTextureID image_id =
103  reinterpret_cast<ImTextureID>(params.texture.GetId());
104  ImGui::SetCursorScreenPos(
105  ImVec2(params.pos_x, params.pos_y - ImGui::GetScrollY()));
106  ImGui::Image(image_id, ImVec2(params.width, params.height),
107  ImVec2(params.u0, params.v0),
108  ImVec2(params.u1, params.v1));
109  } else {
110  // Draw error message if we don't have an image, instead of
111  // quietly failing or something.
112  Rect frame = GetFrame(); // hide reference with a copy...
113  frame.y -= ImGui::GetScrollY(); // ... so we can adjust for scrolling
114  const char* error_text = " Error\nloading\n image";
115  Color fg(1.0, 1.0, 1.0);
116  ImGui::GetWindowDrawList()->AddRectFilled(
117  ImVec2(float(frame.x), float(frame.y)),
118  ImVec2(float(frame.GetRight()), float(frame.GetBottom())),
119  IM_COL32(255, 0, 0, 255));
120  ImGui::GetWindowDrawList()->AddRect(
121  ImVec2(float(frame.x), float(frame.y)),
122  ImVec2(float(frame.GetRight()), float(frame.GetBottom())),
123  IM_COL32(255, 255, 255, 255));
124  ImGui::PushStyleColor(ImGuiCol_Text, colorToImgui(fg));
125 
126  auto padding = ImGui::GetStyle().FramePadding;
127  float wrap_width = frame.width - std::ceil(2.0f * padding.x);
128  float wrapX = ImGui::GetCursorPos().x + wrap_width;
129  auto* font = ImGui::GetFont();
130  auto text_size =
131  font->CalcTextSizeA(float(context.theme.font_size), wrap_width,
132  wrap_width, error_text);
133  float x = (float(frame.width) - text_size.x) / 2.0f;
134  float y = (float(frame.height) - text_size.y) / 2.0f;
135 
136  ImGui::SetCursorScreenPos(ImVec2(x + frame.x, y + frame.y));
137  ImGui::PushTextWrapPos(wrapX);
138  ImGui::TextWrapped("%s", error_text);
139  ImGui::PopTextWrapPos();
140 
141  ImGui::PopStyleColor();
142  }
144 
145  if (params.image_size_changed) {
147  }
149 }
150 
151 } // namespace gui
152 } // namespace visualization
153 } // namespace cloudViewer
Rect frame
std::shared_ptr< core::Tensor > image
cmdLineReadable * params[]
void SetUIImage(std::shared_ptr< UIImage > image)
Definition: ImageWidget.cpp:72
std::shared_ptr< UIImage > GetUIImage() const
Definition: ImageWidget.cpp:68
void Layout(const LayoutContext &context) override
Definition: ImageWidget.cpp:90
Size CalcPreferredSize(const LayoutContext &context, const Constraints &constraints) const override
Definition: ImageWidget.cpp:76
DrawResult Draw(const DrawContext &context) override
Definition: ImageWidget.cpp:94
void UpdateImage(std::shared_ptr< geometry::Image > image)
Definition: ImageWidget.cpp:60
virtual void Layout(const LayoutContext &context)
Definition: Widget.cpp:86
virtual const Rect & GetFrame() const
Returns the frame size in pixels.
Definition: Widget.cpp:51
ImGuiContext * context
Definition: Window.cpp:76
MiniVec< float, N > ceil(const MiniVec< float, N > &a)
Definition: MiniVec.h:89
ImVec4 colorToImgui(const Color &color)
Definition: Util.cpp:20
Generic file read and write utility for python interface.