{
 "cells": [
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Tutorial for Visualization\n",
    "\n",
    "This tutorial contains some examples for the visualization of the different objects dealt with in polymake.\n",
    "\n",
    "\n",
    "## Intro\n",
    "\n",
    "The most straighforward way of visualizing things in `polymake` is by using the `VISUAL` method that visualizable objects provide. For example, you can look at the 3-simplex by doing this:\n",
    "\n",
    "    \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "simplex(3)->VISUAL;"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "By default, this will open `jReality` and show you a tetrahedron in a pretty color. See the [visual_tutorial#Backends](visual_tutorial#Backends) section for more information on using other backends.\n",
    "\n",
    "\n",
    "To get a list of visualization methods available, you can use the build-in help system. To obtain the visualization possibilities for, e.g., Polytopes in application `polytope`, type:\n",
    "\n",
    "    \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "help 'objects/Polytope/methods/Visualization';"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "    \n",
    "    Subtopics of objects/Polytope/methods/Visualization:\n",
    "    GALE, SCHLEGEL, VISUAL, VISUAL_BOUNDED_GRAPH, VISUAL_DUAL, VISUAL_DUAL_FACE_LATTICE,\n",
    "    VISUAL_DUAL_GRAPH, VISUAL_FACE_LATTICE, VISUAL_GRAPH, VISUAL_TRIANGULATION_BOUNDARY\n",
    "\n",
    "Most visualization methods provide a variety of parameters. Get a list for the method of yout choice (here: `VISUAL`) by typing\n",
    "\n",
    "    \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "help 'objects/Polytope/methods/Visualization/VISUAL';"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "The syntax for passing parameters can be seen in this example:\n",
    "\n",
    "    \n",
    "    simplex(3)->VISUAL(EdgeThickness => 10);\n",
    "\n",
    "It visualizes a tetrahedron with really thick edges. See the section on [visual_tutorial#application polytope](visual_tutorial#application polytope) for more.\n",
    "\n",
    "### Specifying colors\n",
    "\n",
    "Some visualization methods requite you to specify colors. There are various ways of doing this in polymake, i.e. the color red may be given by\n",
    "\n",
    "*  the String `'red`', which will be looked up in rgb.txt to obtain the rgb values,\n",
    "\n",
    "*  the String `'1 0 0`', which contains three decimal values in the interval [0,1] corresponding to the 'r g b'-values of the color,\n",
    "\n",
    "*  the String `'255 0 0`', which contains three integer values in the interval [0,255] corresponding to the 'r g b'-values of the color,\n",
    "\n",
    "*  an object `new RGB(1,0,0)` of polymake's RGB class.\n",
    "\n",
    "## application polytope\n",
    "\n",
    "The application 'polytope' contains a large amount of visualization routines. Most\n",
    "of them allow you to change the way things look a various ways. See the following\n",
    "subsections for examples.\n",
    "\n",
    "### Changing vertex attributes \n",
    "\n",
    "It is possible to change the way the vertices are displayed directly from the command line. In the following we will explain how to change the color, labels, and size of the vertices.\n",
    "\n",
    "#### Colors\n",
    "\n",
    "There are different ways to specify the colors of the vertices. You may choose a single color for all vertices or set each vertex color individually. \n",
    "\n",
    "**Single color**: To specify one color for all vertices use the *VertexColor* attribute of the *VISUAL* method: \n",
    "\n",
    "    \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "simplex(3)->VISUAL(VertexColor=> '0 100 200');"
   ]
  },
  {
   "attachments": {
    "visual_vertexcolor_val.jpg": {
     "image/jpg": [
      "[encoded data, 9184 characters]"
     ]
    }
   },
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "\n",
    "![{{ :tutorial:visual_vertexcolor_val.jpg?300 }}](attachment:visual_vertexcolor_val.jpg)\n",
    "\n",
    "You can also change the colors in the visualization backends (e.g. jReality). How to do this is explained below.\n",
    "\n",
    "To set the colors of the vertices individually, you can either specify an array that contains a color for each of the vertices or a perl function, i.e. `sub {...}`, that returns a color depending on the vertex index.\n",
    "\n",
    "**Array of colors**: Instead of passing one color to the *VertexColor* attribute, you can pass an array of colors:\n",
    "\n",
    "    \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "simplex(3)->VISUAL(VertexColor=>['red','green','blue','yellow']);"
   ]
  },
  {
   "attachments": {
    "visual_colors_array.png": {
     "image/png": [
      "[encoded data, 29740 characters]"
     ]
    },
    "visual_vertexcolor_sub.jpg": {
     "image/jpg": [
      "[encoded data, 8240 characters]"
     ]
    }
   },
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "\n",
    "![{{ :tutorial:visual_colors_array.png?300 }}](attachment:visual_colors_array.png)\n",
    "\n",
    "The following line produces the same picture but each color is specified using a different color format: \n",
    "\n",
    "    \n",
    "     polytope > simplex(3)->VISUAL(VertexColor=>['red','0 1 0',new RGB(0,0,1),'255 255 0']); \n",
    "\n",
    "\n",
    "See [](tutorial/visual_tutorial#Specifying Colors) for different ways to specify colors.\n",
    "\n",
    "**Function**: You may also pass a function, i.e. a perl `sub`, to the *VertexColors* attribute that returns a color depending on the vertex index. The following line produces a tetrahedron with colors ranging from black for vertex 0 to yellow for vertex 3:\n",
    "\n",
    "    \n",
    "     polytope > simplex(3)->VISUAL(VertexColor=> sub { $i = shift; new RGB($i*0.33,$i*0.33,0); });\n",
    "\n",
    "\n",
    "![{{ :tutorial:visual_vertexcolor_sub.jpg?300 }}](attachment:visual_vertexcolor_sub.jpg)\n",
    "\n",
    "#### Labels\n",
    "\n",
    "The labels can be specified either by an array or a function that returns a label depending on the index of the vertex.\n",
    "\n",
    "** Array of labels **: To label the vertices of a tetrahedron by A, B, C, and D we just pass the array `[\"A\", \"B\", \"C\", \"D\"]` to the *VertexLabels* attribute:\n",
    "\n",
    "    \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "simplex(3)->VISUAL(VertexLabels=>[\"A\", \"B\", \"C\", \"D\"]);"
   ]
  },
  {
   "attachments": {
    "visual_labels_array.png": {
     "image/png": [
      "[encoded data, 33372 characters]"
     ]
    }
   },
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "\n",
    "![{{ :tutorial:visual_labels_array.png?300 }}](attachment:visual_labels_array.png)\n",
    "\n",
    "If you want to set only one label for example, you can do so by passing the empty string `\"\"` for the other vertices:\n",
    "\n",
    "    \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "simplex(3)->VISUAL(VertexLabels=>[\"\", \"\", \"My favorite vertex\", \"\"]);"
   ]
  },
  {
   "attachments": {
    "visual_labels_array_2.png": {
     "image/png": [
      "[encoded data, 44164 characters]"
     ]
    }
   },
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "\n",
    "![{{ :tutorial:visual_labels_array_2.png?300 }}](attachment:visual_labels_array_2.png)\n",
    "\n",
    "** Function **: We may also write a perl function that returns a label depending on the index of the vertex. For example, if we want to label the vertices by their coordinates, we can use the following commands:\n",
    "\n",
    "    \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "$tet = simplex(3);\n",
    "$tet->VISUAL(VertexLabels=> sub { my $i = shift; return $tet->VERTICES->[$i]; });"
   ]
  },
  {
   "attachments": {
    "visual_labels_sub.png": {
     "image/png": [
      "[encoded data, 36240 characters]"
     ]
    }
   },
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "\n",
    "![{{ :tutorial:visual_labels_sub.png?300 }}](attachment:visual_labels_sub.png)\n",
    "\n",
    "If you prefer dehomogenized float coordinates, then you need to use:\n",
    "\n",
    "    \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "$tet->VISUAL(VertexLabels=> sub { my $i = shift; return convert_to<Float>(dehomogenize($tet->VERTICES->[$i]));});"
   ]
  },
  {
   "attachments": {
    "visual_labels_sub.png": {
     "image/png": [
      "[encoded data, 36240 characters]"
     ]
    }
   },
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "\n",
    "![{{ :tutorial:visual_labels_sub.png?300 }}](attachment:visual_labels_sub.png)\n",
    "\n",
    "#### Vertex Size\n",
    "\n",
    "Similar to changing the colors using the *VertexColor* attribute you are able to change the sizes of the vertices via the *VertexThickness*\n",
    "attribute. Again, you may set the sizes of all vertices by a single value or use an array or a function to specify individual sizes.\n",
    "\n",
    "** Single size **: To increase the size of the vertices, just pass a number larger than 1 to the *VertexThickness*\n",
    "\n",
    "    \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "simplex(3)->VISUAL(VertexThickness=>2);"
   ]
  },
  {
   "attachments": {
    "visual_sizes_single1.png": {
     "image/png": [
      "[encoded data, 31244 characters]"
     ]
    },
    "visual_sizes_single2.png": {
     "image/png": [
      "[encoded data, 37932 characters]"
     ]
    }
   },
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "\n",
    "![{{:tutorial:visual_sizes_single1.png?300 }}](attachment:visual_sizes_single1.png)\n",
    "![{{ :tutorial:visual_sizes_single2.png?300 }}](attachment:visual_sizes_single2.png)\n",
    "\n",
    "** Array of sizes **: As in the case of colors and labels, you may also specify individual sizes using an array:\n",
    "\n",
    "    \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "simplex(3)->VISUAL(VertexThickness=>[1,2,3,4]);"
   ]
  },
  {
   "attachments": {
    "visual_sizes_array.png": {
     "image/png": [
      "[encoded data, 36912 characters]"
     ]
    }
   },
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "\n",
    "![{{ :tutorial:visual_sizes_array.png?300 }}](attachment:visual_sizes_array.png)\n",
    "\n",
    "** Function **: You may define a function that returns the size of the vertex depending on the vertex index. The following\n",
    "example sets the size of the odd vertices to 1 and the sizes of the even vertices to 2:\n",
    "\n",
    "    \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "cube(3)->VISUAL(VertexThickness=> sub { my $i = shift; if($i%2 == 0) { return 2; } else {return 1;} });"
   ]
  },
  {
   "attachments": {
    "visual_sizes_sub.png": {
     "image/png": [
      "[encoded data, 30776 characters]"
     ]
    }
   },
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "\n",
    "![{{ :tutorial:visual_sizes_sub.png?300 }}](attachment:visual_sizes_sub.png)\n",
    "\n",
    "If you do not want to display the vertices at all, you can use the *VertexStyle* attribute and set it to `hidden`:\n",
    "\n",
    "    \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "cube(3)->VISUAL(VertexStyle=>\"hidden\");"
   ]
  },
  {
   "attachments": {
    "visual_style_hidden.png": {
     "image/png": [
      "[encoded data, 22964 characters]"
     ]
    }
   },
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "\n",
    "![{{ :tutorial:visual_style_hidden.png?300 }}](attachment:visual_style_hidden.png)\n",
    "\n",
    "### Visualizing multiple polytopes\n",
    "\n",
    "The following sequence creates a 0/1-cube and a translate.  The final command triggers the joint visualization of both.\n",
    "\n",
    "    \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "$c1=cube(3,0);\n",
    "$c2=transform($c1,new Matrix<Rational>([[1,-1,0,0],[0,1,0,0],[0,0,1,0],[0,0,0,1]]));\n",
    "compose($c1->VISUAL,$c2->VISUAL);"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "\n",
    "If `JavaView` is used for visualization then `Method:Effect:Explode Group of Geometries...` allows to show an explosion.\n",
    "\n",
    "## application topaz\n",
    "\n",
    "For information on how to visualize simlicial complexes and other topology-related objects, see [here](tutorial/apps_topaz#visualization).\n",
    "\n",
    "## Backends\n",
    "\n",
    "'polymake' provides different visualization backends: jReality, JavaView, povray, postscript and even TikZ and Sketch are supported.\n",
    "### TikZ and Sketch\n",
    "\n",
    "Sketch is a script language which produces TikZ output. It is quite sophisticated. There is a interface from polymake to Sketch.\n",
    "\n",
    "    :::perl\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "$my_polytope = cube(3);\n",
    "sketch($my_polytope->VISUAL,File=>\"myfile.sketch\");"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "When you downloaded the Sketch interpreter ([link](http://sketch4latex.sourceforge.net/)) you can use it to create your TikZ file via:\n",
    "\n",
    "    :::perl\n",
    "    sketch myfile.sketch > myfile.tikz\n",
    "\n",
    "\n",
    "The drawback is that the produced TikZ code is quite cryptic and not really readable or editable anymore. If you do want to create TikZ code which is easier to edit afterwards, you might want to use polymake's TikZ interface via:\n",
    "\n",
    "    :::perl\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "tikz($my_polytope->VISUAL,File=>\"myfile.tikz\");"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "For a different viewing angle you may use jReality. Rotate the polytope into the position you want and click the `'save view`' button on the bottom of the jReality window. Then produce the TikZ or Sketch output.\n",
    "\n",
    "### jReality\n",
    "\n",
    "The jReality viewer offers a versatile perspective for your favourite tools on 4 Panel which may be enabled/disabled on the toolbar or using\n",
    "the keyboard shortcuts Alt+Shift+(Up|Down|Left|Right) or the window menu. The \"Visualization\", \"Split geometries\", \"Content Appearance\",\n",
    "and \"Navigator\" Plugin(usually in the Left- resp. Right-Slot) may be used to change the appearance of the displayed geometry. How to achieve your desired parameters is explained in the following section.\n",
    "\n",
    "\n",
    "#### Appearance\n",
    "\n",
    "There are several objects which control the appearance of a geometry in jreality:\n",
    "\n",
    " 1.  geometry attributes,\n",
    " 2.  appearances at a specific scene graph nodes, and\n",
    " 3.  the content appearance.\n",
    "\n",
    "The most versatile point to set different parameters for the appearance is via geometry attributes. This is needed, for example, if every vertex of the displayed\n",
    "geometry should have its own thickness or color like in VISUAL_GRAPH->VERTEX_COLORS. These may only be edited via jreality's bean-shell, which requires\n",
    "knowledge of the jreality API and in particular the attribute handling.\n",
    "\n",
    "An appearance at a scene graph node (every VISUAL object is put into one of these) may have its own appearance which stores single values for colors/thicknesses\n",
    "of vertices/edges/faces. Hence this does not allow, e.g., to assign a different color to each vertex. But editing is a little easier, since it is possible to use jreality's \n",
    "navigator. The navigator displays the entire scene graph. The polymake part of the scene graph starts with \"root->content->Polymake Root\". The VISUALs are stored\n",
    "in seperate geometry nodes with its appearances. In each of the appearance you find the RenderingHints and Shader that either inherit properties of the content \n",
    "appearance or override them with their own values. This is already much less tedious than using the beanshell.\n",
    "\n",
    "The content appearance comes into play if no other appearances exist in the content subtree, i.e. neither specific node appearances nor geometry attributes. The\n",
    "sizes/thicknesses set in the content appearance are multiplied with the values of the specified values deeper in the tree, but colors will only apply if nothing else is\n",
    "set.\n",
    "\n",
    "So to be able to change the colors of the faces/edges/vertices of the geometry using the content appearance you need to get rid settings in the other appearances \n",
    "and geometry attributes. This may be done using the \"clear attributes\" and \"clear appearance\" item of the polymake menu.\n",
    "\n",
    "#### Save View\n",
    "\n",
    "Clicking the \"Save View\" button on the bottom of the jReality window attaches a \"ViewTransformation\" matrix to your object (read [this](reference/clients#attachments) if you don't know how attachments work). It contains the transformaiton necessary to convert the coordinates of your object to the coordinates of the (possibly rotated, translated etc. by you in the interactive visualization) object as you currently see it. This information is then used by jReality and Sketch to display the object in exactly that way if you visualize it the next time.\n",
    "\n",
    "\n",
    "\n",
    "\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "polymake",
   "language": "polymake",
   "name": "polymake"
  },
  "language_info": {
   "codemirror_mode": "perl",
   "file_extension": ".pm",
   "mimetype": "text/x-polymake",
   "name": "polymake"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}