# HG changeset patch
# User Pantxo Diribarne <pantxo.diribarne@gmail.com>
# Date 1500584367 -7200
#      Thu Jul 20 22:59:27 2017 +0200
# Node ID 4955be6eb5f8eeb2a1ebd01e8733302bdd99a247
# Parent  7b43a96c21799981c735252f66524bd77bcdb718
Fix regression introduced by cset 06805aabbdd1 (bug #50750)

* graphics.in.h/cc (axes::properties::get_is2D): Define in graphics.cc. Add new
 bool  argument to indicate wether children should be checked. 
 (figure::properties::has_3D_primitive): new public method.
 (has_3D_primitive): new static method
* gl2ps-print.cc (gl2ps_renderer::draw): make use of new figure method to 
  determine the sorting algorithm to be used
* __opengl_print__.m: don't check dimensionality here.
* gl-render.cc (opengl_renderer::draw_axes): include children in dimensionality
  check

diff -r 7b43a96c2179 -r 4955be6eb5f8 libinterp/corefcn/gl-render.cc
--- a/libinterp/corefcn/gl-render.cc	Thu Jul 20 09:17:03 2017 -0700
+++ b/libinterp/corefcn/gl-render.cc	Thu Jul 20 22:59:27 2017 +0200
@@ -2108,8 +2108,9 @@
 
     setup_opengl_transformation (props);
 
-    // For 2D axes, draw from back to front without depth sorting
-    bool is2D = props.get_is2D ();
+    // For 2D axes with only 2D primitives, draw from back to front without 
+    // depth sorting
+    bool is2D = props.get_is2D (true);
     if (is2D)
       glDisable (GL_DEPTH_TEST);
     else
diff -r 7b43a96c2179 -r 4955be6eb5f8 libinterp/corefcn/gl2ps-print.cc
--- a/libinterp/corefcn/gl2ps-print.cc	Thu Jul 20 09:17:03 2017 -0700
+++ b/libinterp/corefcn/gl2ps-print.cc	Thu Jul 20 22:59:27 2017 +0200
@@ -240,10 +240,11 @@
         // Default sort order optimizes for 3D plots
         GLint gl2ps_sort = GL2PS_BSP_SORT;
 
-        // For 2D plots we can use a simpler Z-depth sorting algorithm
         // FIXME: gl2ps does not provide a way to change the sorting algorythm
         // on a viewport basis, we thus disable sorting only if all axes are 2D
-        if (term.find ("is2D") != std::string::npos)
+        const figure::properties& fprop = 
+          dynamic_cast<const figure::properties&> (go.get_properties ());
+        if (! fprop.has_3D_primitive ())
           gl2ps_sort = GL2PS_NO_SORT;
 
         // Use a temporary file in case an overflow happens
diff -r 7b43a96c2179 -r 4955be6eb5f8 libinterp/corefcn/graphics.cc
--- a/libinterp/corefcn/graphics.cc	Thu Jul 20 09:17:03 2017 -0700
+++ b/libinterp/corefcn/graphics.cc	Thu Jul 20 22:59:27 2017 +0200
@@ -4563,6 +4563,42 @@
     return get_name ();
 }
 
+static bool
+has_3D_primitive (const graphics_handle& h)
+{
+  bool retval = false;
+  graphics_object go = gh_manager::get_object (h);
+  
+  if (! go.valid_object ())
+    return retval;
+
+  if (go.isa ("figure") || go.isa ("axes") 
+      || go.isa ("hggroup") || go.isa ("uipanel"))
+    {
+      Matrix  children = go.get ("children").matrix_value ();
+      for (octave_idx_type ii = 0; ii < children.numel (); ii++)
+        {
+          retval = has_3D_primitive (graphics_handle (children(ii)));
+          if (retval)
+            break;
+        }
+    }
+  else if (go.isa ("line") || go.isa ("patch") || go.isa ("surface"))
+    {
+      NDArray zdata = go.get ("zdata").array_value ();
+      if (! zdata.isempty ())
+        retval = true;
+    }
+
+  return retval;
+}
+
+bool
+figure::properties::has_3D_primitive (void) const
+{
+  return ::has_3D_primitive (__myhandle__);
+}
+
 octave_value
 figure::get_default (const caseless_str& name) const
 {
@@ -4756,6 +4792,12 @@
 %! end_unwind_protect
 */
 
+bool
+axes::properties::get_is2D (bool include_kids) const
+ { 
+   return (include_kids ? (is2D && ! has_3D_primitive (__myhandle__)) : is2D); 
+ }
+
 Matrix
 axes::properties::calc_tightbox (const Matrix& init_pos)
 {
diff -r 7b43a96c2179 -r 4955be6eb5f8 libinterp/corefcn/graphics.in.h
--- a/libinterp/corefcn/graphics.in.h	Thu Jul 20 09:17:03 2017 -0700
+++ b/libinterp/corefcn/graphics.in.h	Thu Jul 20 22:59:27 2017 +0200
@@ -3112,6 +3112,8 @@
 
     std::string get_title (void) const;
 
+    bool has_3D_primitive (void) const;
+
     // See the genprops.awk script for an explanation of the
     // properties declarations.
 
@@ -3483,7 +3485,7 @@
     bool get_x2Dtop (void) const { return x2Dtop; }
     bool get_y2Dright (void) const { return y2Dright; }
     bool get_layer2Dtop (void) const { return layer2Dtop; }
-    bool get_is2D (void) const { return is2D; }
+    bool get_is2D (bool include_kids = false) const;
     bool get_xySym (void) const { return xySym; }
     bool get_xyzSym (void) const { return xyzSym; }
     bool get_zSign (void) const { return zSign; }
diff -r 7b43a96c2179 -r 4955be6eb5f8 scripts/plot/util/private/__opengl_print__.m
--- a/scripts/plot/util/private/__opengl_print__.m	Thu Jul 20 09:17:03 2017 -0700
+++ b/scripts/plot/util/private/__opengl_print__.m	Thu Jul 20 22:59:27 2017 +0200
@@ -148,17 +148,6 @@
 
   opts.pipeline = pipeline;
 
-  ## Tell gl2ps to use different rendering options for 2D plots
-  haxes = findall (opts.figure, "type", "axes");
-  vw = get (haxes, "view");
-  if (iscell (vw))
-    vw = vertcat (vw{:});
-  endif
-  is2D = all (abs (vw(:,2)) == 90);
-  if (is2D)
-    gl2ps_device{end} = [gl2ps_device{end}, "is2D"];
-  endif
-
   for n = 1:numel (pipeline)
     if (opts.debug)
       fprintf ("opengl-pipeline: '%s'\n", pipeline{n});
