Tatooine
position.h
Go to the documentation of this file.
1#ifndef TATOOINE_FLOWEXPLORER_NODES_POSITION_H
2#define TATOOINE_FLOWEXPLORER_NODES_POSITION_H
3//==============================================================================
4#include <tatooine/flowexplorer/point_shader.h>
7#include <tatooine/vec.h>
8#include <tatooine/gl/imgui.h>
10//==============================================================================
12//==============================================================================
13template <size_t N>
14struct position : tatooine::vec<real_type, N>, renderable<position<N>> {
18 using parent_type::at;
19 //============================================================================
21 int m_pointsize = 1;
22 std::array<GLfloat, 4> m_color{0.0f, 0.0f, 0.0f, 1.0f};
23 std::array<ui::input_pin*, N> m_input_pins;
24 //============================================================================
25 auto pos() -> vec<real_type, N>& { return *this; }
26 auto pos() const -> vec<real_type, N> const& { return *this; }
27 //----------------------------------------------------------------------------
28 auto point_size() -> auto& { return m_pointsize; }
29 auto point_size() const -> auto const& { return m_pointsize; }
30 //----------------------------------------------------------------------------
31 auto color() -> auto& { return m_color; }
32 auto color() const -> auto const& { return m_color; }
33 //============================================================================
34 constexpr position(position const&) = default;
35 constexpr position(position&&) noexcept = default;
36 constexpr auto operator=(position const&) -> position& = default;
37 constexpr auto operator=(position&&) noexcept -> position& = default;
38 //============================================================================
39 constexpr position(flowexplorer::scene& s)
40 : renderable<position>{"Position", s,
41 *dynamic_cast<tatooine::vec<real_type, N>*>(this)} {
43 for (size_t i = 0; i < N; ++i) {
45 this->template insert_input_pin<real_type>(""), at(i));
46 }
47 }
48 //============================================================================
49 auto render(mat4f const& P, mat4f const& V)
50 -> void override {
52 point_shader::get().bind();
53 point_shader::get().set_color(m_color[0], m_color[1], m_color[2], m_color[3]);
54 point_shader::get().set_projection_matrix(P);
55 point_shader::get().set_modelview_matrix(V);
57 m_gpu_data.draw_points();
58 }
59 //============================================================================
60 void set_vbo_data() {
61 auto vbomap = m_gpu_data.vertexbuffer().map();
62 vbomap[0] = [this]() -> gpu_vec {
63 if constexpr (N == 3) {
64 return {static_cast<GLfloat>(this->at(0)),
65 static_cast<GLfloat>(this->at(1)),
66 static_cast<GLfloat>(this->at(2))};
67 } else if constexpr (N == 2) {
68 return {static_cast<GLfloat>(this->at(0)),
69 static_cast<GLfloat>(this->at(1)),
70 0.0f};
71 }
72 }();
73 }
74 //----------------------------------------------------------------------------
75 auto create_indexed_data() -> void {
76 m_gpu_data.vertexbuffer().resize(1);
77 m_gpu_data.indexbuffer().resize(1);
79 m_gpu_data.indexbuffer() = {0};
80 }
81 //----------------------------------------------------------------------------
82 auto is_transparent() const -> bool override {
83 return m_color[3] < 1;
84 }
85 //----------------------------------------------------------------------------
86 auto on_mouse_drag(int offset_x, int offset_y) -> bool override {
87 auto const P = this->scene().camera_controller().projection_matrix();
88 auto const V = this->scene().camera_controller().view_matrix();
89
90 auto x = [this]() -> vec4 {
91 if constexpr (N == 2) {
92 return {at(0), at(1), 0, 1};
93 } else if constexpr (N == 3) {
94 return {at(0), at(1), at(2), 1};
95 }
96 }();
97
98 x = P * (V * x);
99 x(0) = (x(0) * 0.5 + 0.5) * (this->scene().window().width() - 1) + offset_x;
100 x(1) = (x(1) * 0.5 + 0.5) * (this->scene().window().height() - 1) - offset_y;
101
102 x(0) = x(0) / (this->scene().window().width() - 1) * 2 - 1;
103 x(1) = x(1) / (this->scene().window().height() - 1) * 2 - 1;
104
105 x = *inv(V) * *inv(P) * x;
106
107 at(0) = x(0);
108 at(1) = x(1);
109 if constexpr (N == 3) {
110 at(2) = x(2);
111 }
112
113 for (auto l : this->self_pin().links()) {
114 l->input().node().on_property_changed();
115 }
116 return true;
117 }
118 //----------------------------------------------------------------------------
119 auto draw_properties() -> bool override {
120 bool changed = false;
121 constexpr auto label = [](size_t const i) {
122 switch (i) {
123 case 0:
124 return "x";
125 case 1:
126 return "y";
127 case 2:
128 return "z";
129 case 3:
130 return "w";
131 default:
132 return "";
133 }
134 };
135 for (size_t i = 0; i < N; ++i) {
136 changed |= ImGui::DragDouble(label(i), &at(i), 0.01);
137 }
138 changed |= ImGui::DragInt("point size", &m_pointsize, 0, 20);
139 changed |= ImGui::ColorEdit3("color", m_color.data());
140 return changed;
141 }
142 //----------------------------------------------------------------------------
144 -> std::optional<intersection<double, 3>> override {
145 if constexpr (N == 3) {
146 geometry::sphere<real_type, 3> s{0.01, vec3{at(0), at(1), at(2)}};
147 return s.check_intersection(ray<real_type, 3>{r});
148 } else if constexpr (N == 2) {
149 geometry::sphere<real_type, 3> s{0.01, vec3{at(0), at(1), 0}};
150 return s.check_intersection(ray<real_type, 3>{r});
151 }
152 return {};
153 }
154};
157//==============================================================================
158} // namespace tatooine::flowexplorer::nodes
159//==============================================================================
162 TATOOINE_REFLECTION_INSERT_METHOD(position, pos()),
163 TATOOINE_REFLECTION_INSERT_GETTER(point_size),
164 TATOOINE_REFLECTION_INSERT_GETTER(color))
167 TATOOINE_REFLECTION_INSERT_METHOD(position, pos()),
168 TATOOINE_REFLECTION_INSERT_GETTER(point_size),
169 TATOOINE_REFLECTION_INSERT_GETTER(color))
170#endif
Definition: indexeddata.h:13
auto vertexbuffer() -> auto &
Definition: indexeddata.h:148
TATOOINE_FLOWEXPLORER_REGISTER_RENDERABLE(tatooine::flowexplorer::nodes::aabb2d, TATOOINE_REFLECTION_INSERT_GETTER(min), TATOOINE_REFLECTION_INSERT_GETTER(max), TATOOINE_REFLECTION_INSERT_GETTER(line_width), TATOOINE_REFLECTION_INSERT_GETTER(line_color))
DLL_API auto DragDouble(const char *label, double *v, double v_speed=1.0, double v_min=0.0, double v_max=0.0, const char *format="%.3lf", float power=1.0) -> bool
Definition: abcflow.h:7
DLL_API auto point_size(GLfloat size) -> void
constexpr auto inv(diag_static_tensor< Tensor, N, N > const &A) -> std::optional< diag_static_tensor< vec< tatooine::value_type< Tensor >, N >, N, N > >
Definition: diag_tensor.h:109
auto is_transparent() const -> bool override
Definition: position.h:82
auto pos() const -> vec< real_type, N > const &
Definition: position.h:26
auto color() -> auto &
Definition: position.h:31
std::array< GLfloat, 4 > m_color
Definition: position.h:22
auto create_indexed_data() -> void
Definition: position.h:75
std::array< ui::input_pin *, N > m_input_pins
Definition: position.h:23
auto on_mouse_drag(int offset_x, int offset_y) -> bool override
Definition: position.h:86
auto color() const -> auto const &
Definition: position.h:32
int m_pointsize
Definition: position.h:21
auto render(mat4f const &P, mat4f const &V) -> void override
Definition: position.h:49
constexpr position(position &&) noexcept=default
gl::indexeddata< gpu_vec > m_gpu_data
Definition: position.h:20
auto point_size() -> auto &
Definition: position.h:28
auto point_size() const -> auto const &
Definition: position.h:29
auto draw_properties() -> bool override
Definition: position.h:119
void set_vbo_data()
Definition: position.h:60
auto pos() -> vec< real_type, N > &
Definition: position.h:25
auto check_intersection(ray< float, 3 > const &r) const -> std::optional< intersection< double, 3 > > override
Definition: position.h:143
constexpr position(position const &)=default
Definition: renderable.h:42
Definition: scene.h:17
auto scene() const -> auto const &
Definition: node.h:72
auto insert_input_pin_property_link(input_pin &pin, Prop &prop) -> auto &
Definition: node.h:56
auto self_pin() const -> auto const &
Definition: node.h:79
Definition: sphere.h:16
Definition: mat.h:14
Definition: ray.h:10
auto constexpr at(integral auto const ... is) -> decltype(auto)
Definition: tensor.h:38
type_list_at< this_type, I > at
Definition: type_list.h:269
Definition: vec.h:12
auto constexpr x() const -> auto const &requires(N >=1)
Definition: vec.h:102