GNU Octave  4.2.1
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
gl-select.cc
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 2011-2017 Michael Goffioul
4 
5 This file is part of Octave.
6 
7 Octave is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by the
9 Free Software Foundation; either version 3 of the License, or (at your
10 option) any later version.
11 
12 Octave is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
16 
17 You should have received a copy of the GNU General Public License
18 along with Octave; see the file COPYING. If not, see
19 <http://www.gnu.org/licenses/>.
20 
21 */
22 
23 #if defined (HAVE_CONFIG_H)
24 # include "config.h"
25 #endif
26 
27 #include <iostream>
28 
29 #include "gl-select.h"
30 
31 namespace octave
32 {
33 
34  void
36  {
37  GLdouble p_matrix[16];
38  GLint viewport[4];
39 
40  glGetDoublev (GL_PROJECTION_MATRIX, p_matrix);
41  glGetIntegerv (GL_VIEWPORT, viewport);
42  glMatrixMode (GL_PROJECTION);
43  glLoadIdentity ();
44  gluPickMatrix (xp, yp, size, size, viewport);
45  glMultMatrixd (p_matrix);
46  glMatrixMode (GL_MODELVIEW);
47  }
48 
49  void
51  {
54  }
55 
56  void
57  opengl_selector::init_marker (const std::string& m, double sz, float width)
58  {
59  opengl_renderer::init_marker (m, sz, width);
61  }
62 
63 # define BUFFER_SIZE 128
64 
66  opengl_selector::select (const graphics_object& ax, int x, int y, int flags)
67  {
68  glEnable (GL_DEPTH_TEST);
69  glDepthFunc (GL_LEQUAL);
70 
71  xp = x;
72  yp = y;
73 
74  GLuint select_buffer[BUFFER_SIZE];
75 
76  glSelectBuffer (BUFFER_SIZE, select_buffer);
77  glRenderMode (GL_SELECT);
78  glInitNames ();
79 
80  object_map.clear ();
81 
82  draw (ax);
83 
84  int hits = glRenderMode (GL_RENDER);
85  graphics_object obj;
86 
87  if (hits > 0)
88  {
89  GLuint current_minZ = 0xffffffff;
90  GLuint current_name = 0xffffffff;
91 
92  for (int i = 0, j = 0; i < hits && j < BUFFER_SIZE-3; i++)
93  {
94  GLuint n = select_buffer[j++],
95  minZ = select_buffer[j++];
96 
97  j++; // skip maxZ
98  if (((flags & select_last) == 0 && (minZ <= current_minZ))
99  || ((flags & select_last) != 0 && (minZ >= current_minZ)))
100  {
101  bool candidate = true;
102  GLuint name =
103  select_buffer[std::min (j + n, GLuint (BUFFER_SIZE)) - 1];
104 
105  if ((flags & select_ignore_hittest) == 0)
106  {
108 
109  if (! go.get_properties ().is_hittest ())
110  candidate = false;
111  }
112 
113  if (candidate)
114  {
115  current_minZ = minZ;
116  current_name = name;
117  }
118 
119  j += n;
120  }
121  else
122  j += n;
123  }
124 
125  if (current_name != 0xffffffff)
126  obj = object_map[current_name];
127  }
128  else if (hits < 0)
129  warning ("opengl_selector::select: selection buffer overflow");
130 
131  object_map.clear ();
132 
133  return obj;
134  }
135 
136  void
137  opengl_selector::draw (const graphics_object& go, bool toplevel)
138  {
139  GLuint name = object_map.size ();
140 
141  object_map[name] = go;
142  glPushName (name);
143  opengl_renderer::draw (go, toplevel);
144  glPopName ();
145  }
146 
147  void
148  opengl_selector::fake_text (double x, double y, double z, const Matrix& bbox,
149  bool use_scale)
150  {
151  ColumnVector xpos, xp1, xp2;
152 
153  xpos = get_transform ().transform (x, y, z, use_scale);
154 
155  xp1 = xp2 = xpos;
156  xp1(0) += bbox(0);
157  xp1(1) -= bbox(1);
158  xp2(0) += (bbox(0) + bbox(2));
159  xp2(1) -= (bbox(1) + bbox(3));
160 
161  ColumnVector p1, p2, p3, p4;
162 
163  p1 = get_transform ().untransform (xp1(0), xp1(1), xp1(2), false);
164  p2 = get_transform ().untransform (xp2(0), xp1(1), xp1(2), false);
165  p3 = get_transform ().untransform (xp2(0), xp2(1), xp1(2), false);
166  p4 = get_transform ().untransform (xp1(0), xp2(1), xp1(2), false);
167 
168  glBegin (GL_QUADS);
169  glVertex3dv (p1.data ());
170  glVertex3dv (p2.data ());
171  glVertex3dv (p3.data ());
172  glVertex3dv (p4.data ());
173  glEnd ();
174  }
175 
176  void
178  {
179  if (props.get_string ().is_empty ())
180  return;
181 
182  Matrix pos = props.get_data_position ();
183  const Matrix bbox = props.get_extent_matrix ();
184 
185  fake_text (pos(0), pos(1), pos.numel () > 2 ? pos(2) : 0.0, bbox);
186  }
187 
188  Matrix
190  double x, double y, double z,
191  int halign, int valign, double rotation)
192  {
193  uint8NDArray pixels;
194  Matrix bbox (1, 4, 0.0);
195 
196  // FIXME: probably more efficient to only compute bbox instead
197  // of doing full text rendering...
198  text_to_pixels (txt, pixels, bbox, halign, valign, rotation);
199  fake_text(x, y, z, bbox, false);
200 
201  return bbox;
202  }
203 
204  void
206  {
207  Matrix xd = props.get_xdata ().matrix_value ();
208  octave_idx_type nc = props.get_cdata ().columns ();
209  double x_pix_size = (nc == 1 ? 1 : (xd(1) - xd(0)) / (nc - 1));
210 
211  Matrix yd = props.get_ydata ().matrix_value ();
212  octave_idx_type nr = props.get_cdata ().rows ();
213  double y_pix_size = (nr == 1 ? 1 : (yd(1) - yd(0)) / (nr - 1));
214 
215  ColumnVector p1(3, 0.0), p2(3, 0.0), p3(3, 0.0), p4(3, 0.0);
216  p1(0) = xd(0) - x_pix_size/2;
217  p1(1) = yd(0) - y_pix_size/2;
218 
219  p2(0) = xd(1) + x_pix_size/2;
220  p2(1) = yd(0) - y_pix_size/2;
221 
222  p3(0) = xd(1) + x_pix_size/2;
223  p3(1) = yd(1) + y_pix_size/2;
224 
225  p4(0) = xd(0) - x_pix_size/2;
226  p4(1) = yd(1) + y_pix_size/2;
227 
228  glBegin (GL_QUADS);
229  glVertex3dv (p1.data ());
230  glVertex3dv (p2.data ());
231  glVertex3dv (p3.data ());
232  glVertex3dv (p4.data ());
233  glEnd ();
234  }
235 
236 }
#define BUFFER_SIZE
Definition: gl-select.cc:63
Octave interface to the compression and uncompression libraries.
Definition: aepbalance.cc:47
virtual void init_marker(const std::string &m, double size, float width)
Definition: gl-render.cc:3763
octave_idx_type rows(void) const
Definition: ov.h:489
virtual graphics_xform get_transform(void) const
Definition: gl-render.h:59
Matrix get_data_position(void) const
Definition: graphics.cc:7981
octave_value get_cdata(void) const
Definition: graphics.h:8714
octave_idx_type numel(void) const
Number of elements in the array.
Definition: Array.h:363
virtual void draw(const graphics_object &go, bool toplevel=true)
Definition: gl-render.cc:652
virtual void init_marker(const std::string &m, double size, float width)
Definition: gl-select.cc:57
virtual void setup_opengl_transformation(const axes::properties &props)
Definition: gl-select.cc:50
octave_value get_xdata(void) const
Definition: graphics.h:8724
virtual void setup_opengl_transformation(const axes::properties &props)
Definition: gl-render.cc:1094
std::map< GLuint, graphics_object > object_map
Definition: gl-select.h:79
graphics_object select(const graphics_object &ax, int x, int y, int flags=0)
Definition: gl-select.cc:66
void apply_pick_matrix(void)
Definition: gl-select.cc:35
octave_value get_ydata(void) const
Definition: graphics.h:8726
bool is_hittest(void) const
Definition: graphics.h:2682
octave_idx_type columns(void) const
Definition: ov.h:491
OCTAVE_EXPORT octave_value_list any number nd example oindent prints the prompt xample Pick a any number!nd example oindent and waits for the user to enter a value The string entered by the user is evaluated as an so it may be a literal a variable name
Definition: input.cc:871
nd deftypefn *octave_map m
Definition: ov-struct.cc:2058
void fake_text(double x, double y, double z, const Matrix &bbox, bool use_scale=true)
Definition: gl-select.cc:148
Matrix get_extent_matrix(void) const
Definition: graphics.cc:7992
const T * data(void) const
Definition: Array.h:582
virtual void draw_image(const image::properties &props)
Definition: gl-select.cc:205
base_properties & get_properties(void)
Definition: graphics.h:3288
Definition: dMatrix.h:37
sz
Definition: data.cc:5342
Matrix matrix_value(bool frc_str_conv=false) const
Definition: ov.h:787
octave_value get_string(void) const
Definition: graphics.h:8095
void warning(const char *fmt,...)
Definition: error.cc:788
ColumnVector untransform(double x, double y, double z, bool use_scale=true) const
Definition: graphics.cc:6635
bool is_empty(void) const
Definition: ov.h:542
virtual void draw(const graphics_object &go, bool toplevel=true)
Definition: gl-select.cc:137
=val(i)}if ode{val(i)}occurs in table i
Definition: lookup.cc:239
virtual void text_to_pixels(const std::string &txt, uint8NDArray &pixels, Matrix &bbox, int halign=0, int valign=0, double rotation=0.0)
Definition: gl-render.cc:4091
the element is set to zero In other the statement xample y
Definition: data.cc:5342
virtual Matrix render_text(const std::string &txt, double x, double y, double z, int halign, int valign, double rotation=0.0)
Definition: gl-select.cc:189
ColumnVector transform(double x, double y, double z, bool use_scale=true) const
Definition: graphics.cc:6622
If this string is the system will ring the terminal sometimes it is useful to be able to print the original representation of the string
Definition: utils.cc:854
F77_RET_T F77_REAL &F77_RET_T F77_DBLE &F77_RET_T F77_REAL &F77_RET_T F77_DBLE &F77_RET_T F77_REAL &F77_RET_T F77_DBLE &F77_RET_T const F77_REAL const F77_REAL F77_REAL &F77_RET_T const F77_DBLE const F77_DBLE F77_DBLE &F77_RET_T F77_REAL &F77_RET_T F77_DBLE &F77_RET_T F77_DBLE &F77_RET_T F77_REAL &F77_RET_T F77_REAL &F77_RET_T F77_DBLE &F77_RET_T const F77_DBLE F77_DBLE &F77_RET_T const F77_REAL F77_REAL &F77_RET_T F77_REAL F77_REAL &F77_RET_T F77_DBLE F77_DBLE &F77_RET_T const F77_DBLE * x
virtual void draw_text(const text::properties &props)
Definition: gl-select.cc:177
charNDArray min(char d, const charNDArray &m)
Definition: chNDArray.cc:205