Skip to content

14. The field in 3-D ​

A heatmap shows the temperature by colour; a 3-D view shows it by height as well. The studio gets a 3-D panel: the temperature as a surface over the plate, as high and as coloured as it is hot, with the body standing on it as a cylinder. The user turns it with the mouse, picks standard views, shows the grid, cuts it along chapter 13's probe line, and reads the temperature under the pointer; a click moves the Result panel's probe point there.

docs/examples/src/tut14_view3d.F90, run as tut14_view3d --examples /tmp/figure-studio --case /tmp/figure-studio/plate

The whole program: tut14_view3d.F90.

What this chapter adds to chapter 13's program, tut13_field.F90:

  • build_3d: The 3-D view of the field
  • draw_3d: The 3-D panel
  • temperature: The temperature of the node nearest (x, y)
  • changed: draw_panels, draw_result, draw_tools, load_field, open_window

The meshes ​

fortran
call view%clear()                                 ! the meshes go before their arrays change
deallocate(carpet)
allocate(carpet(3, size(field, 1), size(field, 2)))
dx = flx / (size(field, 1) - 1)
dy = fly / (size(field, 2) - 1)
tmin = minval(field)
h = 0.4_real64 * max(flx, fly) / max(1.0e-12_real64, maxval(field) - tmin) ! the hottest 0.4 plate high
do j = 1_int32, size(field, 2, kind=int32)
   do i = 1_int32, size(field, 1, kind=int32)
      carpet(:, i, j) = [(i - 1) * dx, (j - 1) * dy, h * (field(i, j) - tmin)]
   enddo
enddo
do i = 1_int32, size(body, 2, kind=int32)       ! the cylinder, from the plate up to the body's temperature
   th = 2.0_real64 * acos(-1.0_real64) * (i - 1) / (size(body, 2) - 1)
   body(:, i, 1) = [inp%body_x + inp%body_r * cos(th), inp%body_y + inp%body_r * sin(th), 0.0_real64]
   body(:, i, 2) = [body(1:2, i, 1), h * (inp%body_t - tmin) + 0.15_real64 * max(flx, fly)]
enddo
carpet_mesh = view%surface(carpet, scalar=field, colormap=trim(MAPS(map)), edges=grid3d) ! read in place
body_mesh = view%surface(body, color=[0.75_real64, 0.75_real64, 0.78_real64])
call view%set_camera(azimuth=-60.0_real64, elevation=35.0_real64)
call view%fit()
if (cut) call view%clip([-1.0_real64, 0.0_real64, 0.0_real64], [probe_x, 0.0_real64, 0.0_real64])
  • A view3d_object is a 3-D view drawn by FIGURE's software rasterizer, the same pixels in a window, in a headless test and in the browser (3-D views). view%init() creates it after app%init, and view%destroy() releases it before app%destroy.
  • view%surface(xyz, scalar=, colormap=, edges=) adds a structured surface: node (i, j) at xyz(:, i, j), joined to its neighbours, coloured by a value per node through a colormap. The cylinder is a surface too, two rings of nodes, in one colour.
  • The arrays are read in place, at every render: carpet and field are target variables of the program, and they stay allocated while the meshes exist. So view%clear() removes the meshes before the arrays are reallocated for a new field (Meshes).
  • set_camera sets the direction (azimuth around z, elevation above the plate) and fit the distance that shows everything.

Turning, cutting, picking ​

fortran
if (ig_button('Top')) call view%view('+z')        ! the standard views are fitted
call ig_same_line()
if (ig_button('Side')) call view%view('-y')
call ig_same_line()
if (ig_button('Fit')) call view%fit()
call ig_same_line()
if (ig_checkbox('grid', grid3d)) call view%set_mesh(carpet_mesh, edges=grid3d)
call ig_same_line()
if (ig_checkbox('cut along the probe line', cut)) then
   call view%unclip()
   if (cut) call view%clip([-1.0_real64, 0.0_real64, 0.0_real64], [probe_x, 0.0_real64, 0.0_real64]) ! keeps x < probe
endif
call ig_same_line()
call ig_text(trim(picked))
  • The mouse turns the view (left drag), moves it (right drag), zooms (wheel) and fits it (double click), with no code of the studio's. view%view('+z') and view%view('-y') are standard views, seen from above and from the side (The camera).
  • view%set_mesh(mesh, edges=) changes a mesh's style: here its grid lines; the Result panel's colormap list calls it too, with colormap=, so the two panels show the same map.
  • view%clip(normal, point) keeps what lies on the side the normal points to; view%unclip() removes the cuts (Clipping planes). The cut follows the probe line when it is dragged.
fortran
call view%draw('view')                            ! the rest of the panel; the mouse orbits, pans, zooms
picked = ''
if (view%hovered(mesh, element, p)) then          ! no cost: the view knows what each pixel shows
   if (mesh == carpet_mesh) picked = 'T = '//num(temperature(p(1), p(2)))//' C at ('//num(p(1))//', '// &
                                     num(p(2))//') m'
endif
if (view%clicked(mesh, element, p)) then
   if (mesh == carpet_mesh) then
      point = p(1:2)                              ! the Result panel's probe point moves there
      call app%notify('The probe point is now at the point clicked', kind='info', seconds=3.0_real64)
   endif
endif
  • view%draw('view') draws the view in the rest of the panel. It renders only when something changed (a mesh, the camera, the panel's size), so a still view costs nothing.
  • view%hovered(mesh, element, point) tells which mesh is under the pointer and the point hit, in the plate's metres; view%clicked the same on a click. The view keeps which triangle covers each pixel, so picking does not search the mesh (Picking).

What the view does not do (lines one pixel wide, one light, no axes inside the view) is in its limits.

What you learned

A 3-D view of meshes read in place from Fortran arrays, coloured through a colormap; the camera, standard views and the mouse; a clipping plane; picking under the pointer and on a click. Reference: 3-D views.

Run it: build it as chapter 7 shows (with tut12_heat beside it), then ./tut14_view3d --examples /tmp/studio --case /tmp/studio/plate.

Next: 15. Heavy work off the frame: a large result read by a task, while the window keeps drawing.

Released under the MIT licence.