source: git/src/gfxcore.cc @ b96edeb

RELEASE/1.2debug-cidebug-ci-sanitisersstereowalls-data
Last change on this file since b96edeb was b96edeb, checked in by Olly Betts <olly@…>, 7 years ago

Partition traverses into list by flag combinations

Instead of a list for surface and one for underground, we now have
8 lists of traverses, one for each combination of img_FLAG_SURFACE,
img_FLAG_SPLAY and img_FLAG_DUPLICATE.

Also allow "Dashed" for splays and "Faded" for duplicate legs.

  • Property mode set to 100644
File size: 110.6 KB
RevLine 
[5809313]1//
[156dc16]2//  gfxcore.cc
[5809313]3//
[33b2094]4//  Core drawing code for Aven.
[5809313]5//
[b72f4b5]6//  Copyright (C) 2000-2003,2005,2006 Mark R. Shinwell
[8b0a077]7//  Copyright (C) 2001-2003,2004,2005,2006,2007,2010,2011,2012,2014,2015,2016 Olly Betts
[887c26e]8//  Copyright (C) 2005 Martin Green
[5809313]9//
10//  This program is free software; you can redistribute it and/or modify
11//  it under the terms of the GNU General Public License as published by
12//  the Free Software Foundation; either version 2 of the License, or
13//  (at your option) any later version.
14//
15//  This program is distributed in the hope that it will be useful,
16//  but WITHOUT ANY WARRANTY; without even the implied warranty of
17//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18//  GNU General Public License for more details.
19//
20//  You should have received a copy of the GNU General Public License
21//  along with this program; if not, write to the Free Software
[5940815]22//  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
[5809313]23//
24
[cbfa50d]25#ifdef HAVE_CONFIG_H
26#include <config.h>
27#endif
28
[892a40c]29#include <assert.h>
[7aa15c0]30#include <float.h>
31
[7a89dc2]32#include "aven.h"
[1ee204e]33#include "date.h"
[112f80c]34#include "filename.h"
[5809313]35#include "gfxcore.h"
[137bf99]36#include "mainfrm.h"
[93c3f97]37#include "message.h"
[7a89dc2]38#include "useful.h"
[4283d6f]39#include "printing.h"
[5876fcb]40#include "guicontrol.h"
[6a4cdcb6]41#include "moviemaker.h"
[8000d8f]42
43#include <wx/confbase.h>
[be98901]44#include <wx/wfstream.h>
[8000d8f]45#include <wx/image.h>
[be98901]46#include <wx/zipstrm.h>
[5809313]47
[22b0a8f]48#include <proj_api.h>
49
[9df33bc]50const unsigned long DEFAULT_HGT_DIM = 3601;
51const unsigned long DEFAULT_HGT_SIZE = sqrd(DEFAULT_HGT_DIM) * 2;
52
[3d00693]53// Values for m_SwitchingTo
54#define PLAN 1
55#define ELEVATION 2
[3ddd351]56#define NORTH 3
57#define EAST 4
58#define SOUTH 5
59#define WEST 6
[3d00693]60
[c61aa79]61// Any error value higher than this is clamped to this.
62#define MAX_ERROR 12.0
63
[af50685]64// Any length greater than pow(10, LOG_LEN_MAX) will be clamped to this.
65const Double LOG_LEN_MAX = 1.5;
66
[86cdcf2]67// How many bins per letter height to use when working out non-overlapping
68// labels.
69const unsigned int QUANTISE_FACTOR = 2;
70
[0e69efe]71#include "avenpal.h"
72
[6606406]73static const int INDICATOR_BOX_SIZE = 60;
[c300a04]74static const int INDICATOR_GAP = 2;
[6606406]75static const int INDICATOR_MARGIN = 5;
76static const int INDICATOR_OFFSET_X = 15;
[c300a04]77static const int INDICATOR_OFFSET_Y = 15;
[fe665c4]78static const int INDICATOR_RADIUS = INDICATOR_BOX_SIZE / 2 - INDICATOR_MARGIN;
[62da267]79static const int KEY_OFFSET_X = 10;
80static const int KEY_OFFSET_Y = 10;
81static const int KEY_EXTRA_LEFT_MARGIN = 2;
82static const int KEY_BLOCK_WIDTH = 20;
83static const int KEY_BLOCK_HEIGHT = 16;
[b56df45]84static const int TICK_LENGTH = 4;
[42adb19]85static const int SCALE_BAR_OFFSET_X = 15;
86static const int SCALE_BAR_OFFSET_Y = 12;
87static const int SCALE_BAR_HEIGHT = 12;
[aa048c3]88
89static const gla_colour TEXT_COLOUR = col_GREEN;
90static const gla_colour HERE_COLOUR = col_WHITE;
91static const gla_colour NAME_COLOUR = col_GREEN;
[dd6af8b]92static const gla_colour SEL_COLOUR = col_WHITE;
[33b2094]93
[0b0520c]94// Number of entries across and down the hit-test grid:
[39e460c9]95#define HITTEST_SIZE 20
96
[395c3f8]97// How close the pointer needs to be to a station to be considered:
98#define MEASURE_THRESHOLD 7
99
[dde4fe7]100// vector for lighting angle
101static const Vector3 light(.577, .577, .577);
102
[9071cf5]103BEGIN_EVENT_TABLE(GfxCore, GLACanvas)
[5809313]104    EVT_PAINT(GfxCore::OnPaint)
105    EVT_LEFT_DOWN(GfxCore::OnLButtonDown)
106    EVT_LEFT_UP(GfxCore::OnLButtonUp)
107    EVT_MIDDLE_DOWN(GfxCore::OnMButtonDown)
108    EVT_MIDDLE_UP(GfxCore::OnMButtonUp)
109    EVT_RIGHT_DOWN(GfxCore::OnRButtonDown)
110    EVT_RIGHT_UP(GfxCore::OnRButtonUp)
[34d8d1a]111    EVT_MOUSEWHEEL(GfxCore::OnMouseWheel)
[5809313]112    EVT_MOTION(GfxCore::OnMouseMove)
[887c26e]113    EVT_LEAVE_WINDOW(GfxCore::OnLeaveWindow)
[5809313]114    EVT_SIZE(GfxCore::OnSize)
[a8e9fde]115    EVT_IDLE(GfxCore::OnIdle)
[4b1fc48]116    EVT_CHAR(GfxCore::OnKeyPress)
[5809313]117END_EVENT_TABLE()
118
[5876fcb]119GfxCore::GfxCore(MainFrm* parent, wxWindow* parent_win, GUIControl* control) :
[88707e0b]120    GLACanvas(parent_win, 100),
121    m_Scale(0.0),
[39bd2ef]122    initial_scale(1.0),
[88707e0b]123    m_ScaleBarWidth(0),
124    m_Control(control),
125    m_LabelGrid(NULL),
126    m_Parent(parent),
127    m_DoneFirstShow(false),
128    m_TiltAngle(0.0),
129    m_PanAngle(0.0),
130    m_Rotating(false),
131    m_RotationStep(0.0),
132    m_SwitchingTo(0),
133    m_Crosses(false),
134    m_Legs(true),
[cca3cee]135    m_Splays(SHOW_FADED),
136    m_Dupes(SHOW_DASHED),
[88707e0b]137    m_Names(false),
138    m_Scalebar(true),
[97ea48d]139    m_ColourKey(true),
[88707e0b]140    m_OverlappingNames(false),
141    m_Compass(true),
142    m_Clino(true),
143    m_Tubes(false),
144    m_ColourBy(COLOUR_BY_DEPTH),
145    m_HaveData(false),
[22b0a8f]146    m_HaveTerrain(true),
[88707e0b]147    m_MouseOutsideCompass(false),
148    m_MouseOutsideElev(false),
149    m_Surface(false),
150    m_Entrances(false),
151    m_FixedPts(false),
152    m_ExportedPts(false),
153    m_Grid(false),
154    m_BoundingBox(false),
[4938bcd]155    m_Terrain(false),
[88707e0b]156    m_Degrees(false),
157    m_Metric(false),
[d171c0c]158    m_Percent(false),
[7171240]159    m_HitTestDebug(false),
[11169cb]160    m_RenderStats(false),
[7171240]161    m_PointGrid(NULL),
[88707e0b]162    m_HitTestGridValid(false),
[381ae6e]163    m_here(NULL),
164    m_there(NULL),
[88707e0b]165    presentation_mode(0),
166    pres_reverse(false),
167    pres_speed(0.0),
[75d4a2b]168    movie(NULL),
[d96c95c]169    current_cursor(GfxCore::CURSOR_DEFAULT),
[6388423]170    sqrd_measure_threshold(sqrd(MEASURE_THRESHOLD)),
[9df33bc]171    dem(NULL),
[11169cb]172    last_time(0),
[6388423]173    n_tris(0)
[5809313]174{
[da6c802]175    AddQuad = &GfxCore::AddQuadrilateralDepth;
176    AddPoly = &GfxCore::AddPolylineDepth;
[5627cbb]177    wxConfigBase::Get()->Read(wxT("metric"), &m_Metric, true);
178    wxConfigBase::Get()->Read(wxT("degrees"), &m_Degrees, true);
[82277dd]179    wxConfigBase::Get()->Read(wxT("percent"), &m_Percent, false);
[5809313]180
[97ea48d]181    for (int pen = 0; pen < NUM_COLOUR_BANDS + 1; ++pen) {
[4a1cede]182        m_Pens[pen].SetColour(REDS[pen] / 255.0,
[0e69efe]183                              GREENS[pen] / 255.0,
184                              BLUES[pen] / 255.0);
185    }
[5455bb2]186
187    timer.Start();
[5809313]188}
189
190GfxCore::~GfxCore()
191{
192    TryToFreeArrays();
[156dc16]193
[39e460c9]194    delete[] m_PointGrid;
[5809313]195}
196
197void GfxCore::TryToFreeArrays()
198{
199    // Free up any memory allocated for arrays.
[81f1266]200    delete[] m_LabelGrid;
201    m_LabelGrid = NULL;
[5809313]202}
203
204//
205//  Initialisation methods
206//
207
[0c6bf5e8]208void GfxCore::Initialise(bool same_file)
[5809313]209{
210    // Initialise the view from the parent holding the survey data.
211
212    TryToFreeArrays();
213
[33b2094]214    m_DoneFirstShow = false;
215
[dfe4454c]216    m_HitTestGridValid = false;
[381ae6e]217    m_here = NULL;
218    m_there = NULL;
[dfe4454c]219
[d35144d]220    m_MouseOutsideCompass = m_MouseOutsideElev = false;
221
[0c6bf5e8]222    if (!same_file) {
223        // Apply default parameters unless reloading the same file.
224        DefaultParameters();
225    }
[9eb58d0]226
227    m_HaveData = true;
[936a197]228
[92cf7a8]229    // Clear any cached OpenGL lists which depend on the data.
230    InvalidateList(LIST_SCALE_BAR);
231    InvalidateList(LIST_DEPTH_KEY);
232    InvalidateList(LIST_DATE_KEY);
233    InvalidateList(LIST_ERROR_KEY);
[cc9e2c65]234    InvalidateList(LIST_GRADIENT_KEY);
[af50685]235    InvalidateList(LIST_LENGTH_KEY);
[92cf7a8]236    InvalidateList(LIST_UNDERGROUND_LEGS);
237    InvalidateList(LIST_TUBES);
238    InvalidateList(LIST_SURFACE_LEGS);
239    InvalidateList(LIST_BLOBS);
240    InvalidateList(LIST_CROSSES);
241    InvalidateList(LIST_GRID);
242    InvalidateList(LIST_SHADOW);
[22b0a8f]243    InvalidateList(LIST_TERRAIN);
[92cf7a8]244
[39bd2ef]245    // Set diameter of the viewing volume.
246    double cave_diameter = sqrt(sqrd(m_Parent->GetXExtent()) +
247                                sqrd(m_Parent->GetYExtent()) +
248                                sqrd(m_Parent->GetZExtent()));
249
250    // Allow for terrain.
251    double diameter = max(1000.0 * 2, cave_diameter * 2);
252
253    if (!same_file) {
254        SetVolumeDiameter(diameter);
255
256        // Set initial scale based on the size of the cave.
257        initial_scale = diameter / cave_diameter;
258        SetScale(initial_scale);
259    } else {
[8b0a077]260        // Adjust the position when restricting the view to a subsurvey (or
261        // expanding the view to show the whole survey).
262        AddTranslation(m_Parent->GetOffset() - offsets);
263
[39bd2ef]264        // Try to keep the same scale, allowing for the
265        // cave having grown (or shrunk).
266        double rescale = GetVolumeDiameter() / diameter;
267        SetVolumeDiameter(diameter);
[8b0a077]268        SetScale(GetScale() / rescale); // ?
[39bd2ef]269        initial_scale = initial_scale * rescale;
270    }
271
[8b0a077]272    offsets = m_Parent->GetOffset();
273
[936a197]274    ForceRefresh();
[9eb58d0]275}
276
277void GfxCore::FirstShow()
278{
279    GLACanvas::FirstShow();
280
[8bd480e]281    const unsigned int quantise(GetFontSize() / QUANTISE_FACTOR);
[86cdcf2]282    list<LabelInfo*>::iterator pos = m_Parent->GetLabelsNC();
283    while (pos != m_Parent->GetLabelsNCEnd()) {
284        LabelInfo* label = *pos++;
285        // Calculate and set the label width for use when plotting
286        // none-overlapping labels.
287        int ext_x;
288        GLACanvas::GetTextExtent(label->GetText(), &ext_x, NULL);
[5a24583]289        label->set_width(unsigned(ext_x) / quantise + 1);
[86cdcf2]290    }
291
[5809313]292    m_DoneFirstShow = true;
293}
294
295//
296//  Recalculating methods
297//
298
[cd6ea75]299void GfxCore::SetScale(Double scale)
[5047c53]300{
[8b0d57f]301    if (scale < 0.05) {
302        scale = 0.05;
[70acad9]303    } else if (scale > GetVolumeDiameter()) {
304        scale = GetVolumeDiameter();
[5047c53]305    }
306
[5b7164d]307    m_Scale = scale;
[00a68e0]308    m_HitTestGridValid = false;
[381ae6e]309    if (m_here && m_here == &temp_here) SetHere();
[33b2094]310
311    GLACanvas::SetScale(scale);
[d9b3270]312}
313
[f433fda]314bool GfxCore::HasUndergroundLegs() const
315{
316    return m_Parent->HasUndergroundLegs();
317}
318
[5fe7292]319bool GfxCore::HasSplays() const
320{
321    return m_Parent->HasSplays();
322}
323
[e5c5f3c]324bool GfxCore::HasDupes() const
325{
326    return m_Parent->HasDupes();
327}
328
[f433fda]329bool GfxCore::HasSurfaceLegs() const
330{
331    return m_Parent->HasSurfaceLegs();
332}
333
[50e8979]334bool GfxCore::HasTubes() const
335{
336    return m_Parent->HasTubes();
337}
338
[d9b3270]339void GfxCore::UpdateBlobs()
340{
[d2fcc9b]341    InvalidateList(LIST_BLOBS);
[33b2094]342}
343
[cd6ea75]344//
[5876fcb]345//  Event handlers
[cd6ea75]346//
[5047c53]347
[41f7a27]348void GfxCore::OnLeaveWindow(wxMouseEvent&) {
[887c26e]349    SetHere();
350    ClearCoords();
351}
352
[5876fcb]353void GfxCore::OnIdle(wxIdleEvent& event)
354{
355    // Handle an idle event.
[5455bb2]356    if (Animating()) {
357        Animate();
358        // If still animating, we want more idle events.
359        if (Animating())
360            event.RequestMore();
[11169cb]361    } else {
362        // If we're idle, don't show a bogus FPS next time we render.
363        last_time = 0;
[b72f4b5]364    }
[5809313]365}
366
[b4fe9fb]367void GfxCore::OnPaint(wxPaintEvent&)
[5809313]368{
369    // Redraw the window.
[b462168]370
371    // Get a graphics context.
[1b12b82]372    wxPaintDC dc(this);
[5809313]373
[815eab2]374    if (m_HaveData) {
[01d91fd]375        // Make sure we're initialised.
376        bool first_time = !m_DoneFirstShow;
377        if (first_time) {
378            FirstShow();
379        }
380
[58dfdd21]381        StartDrawing();
382
383        // Clear the background.
384        Clear();
385
[429465a]386        // Set up model transformation matrix.
387        SetDataTransform();
388
389        if (m_Legs || m_Tubes) {
390            if (m_Tubes) {
[d67450e]391                EnableSmoothPolygons(true); // FIXME: allow false for wireframe view
[d2fcc9b]392                DrawList(LIST_TUBES);
[429465a]393                DisableSmoothPolygons();
394            }
[e4d40792]395
[50e8979]396            // Draw the underground legs.  Do this last so that anti-aliasing
[e4d40792]397            // works over polygons.
398            SetColour(col_GREEN);
[d2fcc9b]399            DrawList(LIST_UNDERGROUND_LEGS);
[429465a]400        }
[33b2094]401
[429465a]402        if (m_Surface) {
403            // Draw the surface legs.
[d2fcc9b]404            DrawList(LIST_SURFACE_LEGS);
[429465a]405        }
[bbc22ca]406
[f4c5932]407        if (m_BoundingBox) {
408            DrawShadowedBoundingBox();
409        }
[429465a]410        if (m_Grid) {
411            // Draw the grid.
[37d7084]412            DrawList(LIST_GRID);
[429465a]413        }
[b13aee4]414
[6a4c11b7]415        DrawList(LIST_BLOBS);
416
417        if (m_Crosses) {
418            DrawList(LIST_CROSSES);
419        }
420
[d347a2c]421        if (m_Terrain) {
[6a4c11b7]422            // This is needed if blobs and/or crosses are drawn using lines -
423            // otherwise the terrain doesn't appear when they are enabled.
424            SetDataTransform();
425
[11fe902]426            // We don't want to be able to see the terrain through itself, so
427            // do a "Z-prepass" - plot the terrain once only updating the
428            // Z-buffer, then again with Z-clipping only plotting where the
429            // depth matches the value in the Z-buffer.
430            DrawListZPrepass(LIST_TERRAIN);
[d347a2c]431        }
432
[429465a]433        SetIndicatorTransform();
434
[6adffadf]435        // Draw station names.
436        if (m_Names /*&& !m_Control->MouseDown() && !Animating()*/) {
437            SetColour(NAME_COLOUR);
438
439            if (m_OverlappingNames) {
440                SimpleDrawNames();
441            } else {
442                NattyDrawNames();
443            }
444        }
445
[5e0b3a13]446        if (m_HitTestDebug) {
447            // Show the hit test grid bucket sizes...
448            SetColour(m_HitTestGridValid ? col_LIGHT_GREY : col_DARK_GREY);
[7171240]449            if (m_PointGrid) {
450                for (int i = 0; i != HITTEST_SIZE; ++i) {
451                    int x = (GetXSize() + 1) * i / HITTEST_SIZE + 2;
452                    for (int j = 0; j != HITTEST_SIZE; ++j) {
453                        int square = i + j * HITTEST_SIZE;
[a49a023]454                        unsigned long bucket_size = m_PointGrid[square].size();
[7171240]455                        if (bucket_size) {
456                            int y = (GetYSize() + 1) * (HITTEST_SIZE - 1 - j) / HITTEST_SIZE;
[7b9b700]457                            DrawIndicatorText(x, y, wxString::Format(wxT("%lu"), bucket_size));
[7171240]458                        }
[5e0b3a13]459                    }
460                }
461            }
462
463            EnableDashedLines();
464            BeginLines();
465            for (int i = 0; i != HITTEST_SIZE; ++i) {
466                int x = (GetXSize() + 1) * i / HITTEST_SIZE;
467                PlaceIndicatorVertex(x, 0);
468                PlaceIndicatorVertex(x, GetYSize());
469            }
470            for (int j = 0; j != HITTEST_SIZE; ++j) {
471                int y = (GetYSize() + 1) * (HITTEST_SIZE - 1 - j) / HITTEST_SIZE;
472                PlaceIndicatorVertex(0, y);
473                PlaceIndicatorVertex(GetXSize(), y);
474            }
475            EndLines();
476            DisableDashedLines();
477        }
478
[11169cb]479        long now = timer.Time();
480        if (m_RenderStats) {
481            // Show stats about rendering.
482            SetColour(col_TURQUOISE);
483            int y = GetYSize() - GetFontSize();
484            if (last_time != 0.0) {
485                // timer.Time() measure in milliseconds.
486                double fps = 1000.0 / (now - last_time);
487                DrawIndicatorText(1, y, wxString::Format(wxT("FPS:% 5.1f"), fps));
488            }
489            y -= GetFontSize();
490            DrawIndicatorText(1, y, wxString::Format(wxT("▲:%lu"), (unsigned long)n_tris));
491        }
492        last_time = now;
493
[c091027]494        // Draw indicators.
495        //
496        // There's no advantage in generating an OpenGL list for the
497        // indicators since they change with almost every redraw (and
498        // sometimes several times between redraws).  This way we avoid
499        // the need to track when to update the indicator OpenGL list,
500        // and also avoid indicator update bugs when we don't quite get this
501        // right...
502        DrawIndicators();
503
[dd6af8b]504        if (zoombox.active()) {
505            SetColour(SEL_COLOUR);
506            EnableDashedLines();
507            BeginPolyline();
508            glaCoord Y = GetYSize();
509            PlaceIndicatorVertex(zoombox.x1, Y - zoombox.y1);
510            PlaceIndicatorVertex(zoombox.x1, Y - zoombox.y2);
511            PlaceIndicatorVertex(zoombox.x2, Y - zoombox.y2);
512            PlaceIndicatorVertex(zoombox.x2, Y - zoombox.y1);
513            PlaceIndicatorVertex(zoombox.x1, Y - zoombox.y1);
514            EndPolyline();
515            DisableDashedLines();
516        } else if (MeasuringLineActive()) {
[429465a]517            // Draw "here" and "there".
[f6d8375]518            double hx, hy;
[aa048c3]519            SetColour(HERE_COLOUR);
[381ae6e]520            if (m_here) {
[f6d8375]521                double dummy;
[381ae6e]522                Transform(*m_here, &hx, &hy, &dummy);
523                if (m_here != &temp_here) DrawRing(hx, hy);
[429465a]524            }
[381ae6e]525            if (m_there) {
[f6d8375]526                double tx, ty;
527                double dummy;
[381ae6e]528                Transform(*m_there, &tx, &ty, &dummy);
529                if (m_here) {
[429465a]530                    BeginLines();
531                    PlaceIndicatorVertex(hx, hy);
532                    PlaceIndicatorVertex(tx, ty);
[42d23c5]533                    EndLines();
[429465a]534                }
[e633bb1]535                BeginBlobs();
[81aea4e]536                DrawBlob(tx, ty);
[e633bb1]537                EndBlobs();
[429465a]538            }
539        }
[f433fda]540
[58dfdd21]541        FinishDrawing();
[5997ffd]542    } else {
543        dc.SetBackground(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWFRAME));
544        dc.Clear();
[58dfdd21]545    }
[5809313]546}
547
[f4c5932]548void GfxCore::DrawBoundingBox()
549{
[d67450e]550    const Vector3 v = 0.5 * m_Parent->GetExtent();
[f4c5932]551
552    SetColour(col_BLUE);
553    EnableDashedLines();
554    BeginPolyline();
[d67450e]555    PlaceVertex(-v.GetX(), -v.GetY(), v.GetZ());
556    PlaceVertex(-v.GetX(), v.GetY(), v.GetZ());
557    PlaceVertex(v.GetX(), v.GetY(), v.GetZ());
558    PlaceVertex(v.GetX(), -v.GetY(), v.GetZ());
559    PlaceVertex(-v.GetX(), -v.GetY(), v.GetZ());
[f4c5932]560    EndPolyline();
561    BeginPolyline();
[d67450e]562    PlaceVertex(-v.GetX(), -v.GetY(), -v.GetZ());
563    PlaceVertex(-v.GetX(), v.GetY(), -v.GetZ());
564    PlaceVertex(v.GetX(), v.GetY(), -v.GetZ());
565    PlaceVertex(v.GetX(), -v.GetY(), -v.GetZ());
566    PlaceVertex(-v.GetX(), -v.GetY(), -v.GetZ());
[f4c5932]567    EndPolyline();
568    BeginLines();
[d67450e]569    PlaceVertex(-v.GetX(), -v.GetY(), v.GetZ());
570    PlaceVertex(-v.GetX(), -v.GetY(), -v.GetZ());
571    PlaceVertex(-v.GetX(), v.GetY(), v.GetZ());
572    PlaceVertex(-v.GetX(), v.GetY(), -v.GetZ());
573    PlaceVertex(v.GetX(), v.GetY(), v.GetZ());
574    PlaceVertex(v.GetX(), v.GetY(), -v.GetZ());
575    PlaceVertex(v.GetX(), -v.GetY(), v.GetZ());
576    PlaceVertex(v.GetX(), -v.GetY(), -v.GetZ());
[f4c5932]577    EndLines();
578    DisableDashedLines();
579}
580
581void GfxCore::DrawShadowedBoundingBox()
582{
[d67450e]583    const Vector3 v = 0.5 * m_Parent->GetExtent();
[f4c5932]584
[f7dae86]585    DrawBoundingBox();
586
[f9ca87c]587    PolygonOffset(true);
[f4c5932]588    SetColour(col_DARK_GREY);
589    BeginQuadrilaterals();
[d67450e]590    PlaceVertex(-v.GetX(), -v.GetY(), -v.GetZ());
591    PlaceVertex(-v.GetX(), v.GetY(), -v.GetZ());
592    PlaceVertex(v.GetX(), v.GetY(), -v.GetZ());
593    PlaceVertex(v.GetX(), -v.GetY(), -v.GetZ());
[f4c5932]594    EndQuadrilaterals();
[f9ca87c]595    PolygonOffset(false);
[f4c5932]596
[d2fcc9b]597    DrawList(LIST_SHADOW);
[f4c5932]598}
599
[c1cf79d]600void GfxCore::DrawGrid()
601{
602    // Draw the grid.
[156dc16]603    SetColour(col_RED);
[c1cf79d]604
605    // Calculate the extent of the survey, in metres across the screen plane.
[b81eee2]606    Double m_across_screen = SurveyUnitsAcrossViewport();
[c1cf79d]607    // Calculate the length of the scale bar in metres.
608    //--move this elsewhere
[c6d95d8]609    Double size_snap = pow(10.0, floor(log10(0.75 * m_across_screen)));
610    Double t = m_across_screen * 0.75 / size_snap;
[c1cf79d]611    if (t >= 5.0) {
[429465a]612        size_snap *= 5.0;
[c1cf79d]613    }
614    else if (t >= 2.0) {
[429465a]615        size_snap *= 2.0;
[c1cf79d]616    }
617
[d67450e]618    Double grid_size = size_snap * 0.1;
[c6d95d8]619    Double edge = grid_size * 2.0;
[d67450e]620    Double grid_z = -m_Parent->GetZExtent() * 0.5 - grid_size;
621    Double left = -m_Parent->GetXExtent() * 0.5 - edge;
622    Double right = m_Parent->GetXExtent() * 0.5 + edge;
623    Double bottom = -m_Parent->GetYExtent() * 0.5 - edge;
624    Double top = m_Parent->GetYExtent() * 0.5 + edge;
[c1cf79d]625    int count_x = (int) ceil((right - left) / grid_size);
626    int count_y = (int) ceil((top - bottom) / grid_size);
[c6d95d8]627    Double actual_right = left + count_x*grid_size;
628    Double actual_top = bottom + count_y*grid_size;
[c1cf79d]629
[b81eee2]630    BeginLines();
631
[c1cf79d]632    for (int xc = 0; xc <= count_x; xc++) {
[429465a]633        Double x = left + xc*grid_size;
[f433fda]634
[b81eee2]635        PlaceVertex(x, bottom, grid_z);
636        PlaceVertex(x, actual_top, grid_z);
[c1cf79d]637    }
638
639    for (int yc = 0; yc <= count_y; yc++) {
[429465a]640        Double y = bottom + yc*grid_size;
[b81eee2]641        PlaceVertex(left, y, grid_z);
642        PlaceVertex(actual_right, y, grid_z);
[c1cf79d]643    }
[b81eee2]644
645    EndLines();
[c1cf79d]646}
647
[1eeb55a]648int GfxCore::GetClinoOffset() const
[c300a04]649{
[2a26b45]650    int result = INDICATOR_OFFSET_X;
651    if (m_Compass) {
652        result += 6 + GetCompassWidth() + INDICATOR_GAP;
653    }
654    return result;
[c300a04]655}
656
[fe665c4]657void GfxCore::DrawTick(int angle_cw)
[6606406]658{
[fe665c4]659    const Double theta = rad(angle_cw);
660    const wxCoord length1 = INDICATOR_RADIUS;
661    const wxCoord length0 = length1 + TICK_LENGTH;
662    wxCoord x0 = wxCoord(length0 * sin(theta));
663    wxCoord y0 = wxCoord(length0 * cos(theta));
664    wxCoord x1 = wxCoord(length1 * sin(theta));
665    wxCoord y1 = wxCoord(length1 * cos(theta));
[6606406]666
[fe665c4]667    PlaceIndicatorVertex(x0, y0);
668    PlaceIndicatorVertex(x1, y1);
[6606406]669}
670
[d67450e]671void GfxCore::DrawArrow(gla_colour col1, gla_colour col2) {
672    Vector3 p1(0, INDICATOR_RADIUS, 0);
673    Vector3 p2(INDICATOR_RADIUS/2, INDICATOR_RADIUS*-.866025404, 0); // 150deg
674    Vector3 p3(-INDICATOR_RADIUS/2, INDICATOR_RADIUS*-.866025404, 0); // 210deg
675    Vector3 pc(0, 0, 0);
676
677    DrawTriangle(col_LIGHT_GREY, col1, p2, p1, pc);
678    DrawTriangle(col_LIGHT_GREY, col2, p3, p1, pc);
679}
680
[fe665c4]681void GfxCore::DrawCompass() {
682    // Ticks.
683    BeginLines();
684    for (int angle = 315; angle > 0; angle -= 45) {
685        DrawTick(angle);
686    }
687    SetColour(col_GREEN);
688    DrawTick(0);
689    EndLines();
690
691    // Compass background.
692    DrawCircle(col_LIGHT_GREY_2, col_GREY, 0, 0, INDICATOR_RADIUS);
[6606406]693
[fe665c4]694    // Compass arrow.
[d67450e]695    DrawArrow(col_INDICATOR_1, col_INDICATOR_2);
[6606406]696}
697
[fe665c4]698// Draw the non-rotating background to the clino.
699void GfxCore::DrawClinoBack() {
700    BeginLines();
701    for (int angle = 0; angle <= 180; angle += 90) {
702        DrawTick(angle);
703    }
704
705    SetColour(col_GREY);
706    PlaceIndicatorVertex(0, INDICATOR_RADIUS);
707    PlaceIndicatorVertex(0, -INDICATOR_RADIUS);
708    PlaceIndicatorVertex(0, 0);
709    PlaceIndicatorVertex(INDICATOR_RADIUS, 0);
[b56df45]710
[fe665c4]711    EndLines();
712}
713
714void GfxCore::DrawClino() {
715    // Ticks.
716    SetColour(col_GREEN);
[33b2094]717    BeginLines();
[fe665c4]718    DrawTick(0);
[33b2094]719    EndLines();
[fe665c4]720
721    // Clino background.
722    DrawSemicircle(col_LIGHT_GREY_2, col_GREY, 0, 0, INDICATOR_RADIUS, 0);
723
724    // Elevation arrow.
[d67450e]725    DrawArrow(col_INDICATOR_2, col_INDICATOR_1);
[b56df45]726}
727
[6606406]728void GfxCore::Draw2dIndicators()
729{
[76dd228]730    // Draw the compass and elevation indicators.
731
732    const int centre_y = INDICATOR_BOX_SIZE / 2 + INDICATOR_OFFSET_Y;
733
[fe665c4]734    const int comp_centre_x = GetCompassXPosition();
[6606406]735
[eef68f9]736    if (m_Compass && !m_Parent->IsExtendedElevation()) {
[fe665c4]737        // If the user is dragging the compass with the pointer outside the
738        // compass, we snap to 45 degree multiples, and the ticks go white.
[76dd228]739        SetColour(m_MouseOutsideCompass ? col_WHITE : col_LIGHT_GREY_2);
[fe665c4]740        DrawList2D(LIST_COMPASS, comp_centre_x, centre_y, -m_PanAngle);
[c300a04]741    }
[76dd228]742
[fe665c4]743    const int elev_centre_x = GetClinoXPosition();
[76dd228]744
[eef68f9]745    if (m_Clino) {
[fe665c4]746        // If the user is dragging the clino with the pointer outside the
747        // clino, we snap to 90 degree multiples, and the ticks go white.
748        SetColour(m_MouseOutsideElev ? col_WHITE : col_LIGHT_GREY_2);
749        DrawList2D(LIST_CLINO_BACK, elev_centre_x, centre_y, 0);
[7a57dc7]750        DrawList2D(LIST_CLINO, elev_centre_x, centre_y, 90 - m_TiltAngle);
[c300a04]751    }
[6606406]752
[aa048c3]753    SetColour(TEXT_COLOUR);
[6606406]754
[21958ec]755    static int triple_zero_width = 0;
756    static int height = 0;
757    if (!triple_zero_width) {
[5627cbb]758        GetTextExtent(wxT("000"), &triple_zero_width, &height);
[21958ec]759    }
760    const int y_off = INDICATOR_OFFSET_Y + INDICATOR_BOX_SIZE + height / 2;
[421b7d2]761
[eef68f9]762    if (m_Compass && !m_Parent->IsExtendedElevation()) {
[21958ec]763        wxString str;
764        int value;
[d171c0c]765        int brg_unit;
[429465a]766        if (m_Degrees) {
[21958ec]767            value = int(m_PanAngle);
[736f7df]768            /* TRANSLATORS: degree symbol - probably should be translated to
769             * itself. */
[d171c0c]770            brg_unit = /*°*/344;
[429465a]771        } else {
[21958ec]772            value = int(m_PanAngle * 200.0 / 180.0);
[736f7df]773            /* TRANSLATORS: symbol for grad (400 grad = 360 degrees = full
774             * circle). */
[d171c0c]775            brg_unit = /*ᵍ*/76;
[f433fda]776        }
[5627cbb]777        str.Printf(wxT("%03d"), value);
[d171c0c]778        str += wmsg(brg_unit);
[21958ec]779        DrawIndicatorText(comp_centre_x - triple_zero_width / 2, y_off, str);
780
[1c507cf]781        // TRANSLATORS: Used in aven above the compass indicator at the lower
782        // right of the display, with a bearing below "Facing".  This indicates the
783        // direction the viewer is "facing" in.
784        //
785        // Try to keep this translation short - ideally at most 10 characters -
786        // as otherwise the compass and clino will be moved further apart to
787        // make room. */
[5627cbb]788        str = wmsg(/*Facing*/203);
[21958ec]789        int w;
[d92d282]790        GetTextExtent(str, &w, NULL);
[21958ec]791        DrawIndicatorText(comp_centre_x - w / 2, y_off + height, str);
[c300a04]792    }
793
[eef68f9]794    if (m_Clino) {
[c13d121f]795        if (m_TiltAngle == -90.0) {
796            // TRANSLATORS: Label used for "clino" in Aven when the view is
797            // from directly above.
[1c507cf]798            //
799            // Try to keep this translation short - ideally at most 10
800            // characters - as otherwise the compass and clino will be moved
801            // further apart to make room. */
[c13d121f]802            wxString str = wmsg(/*Plan*/432);
803            static int width = 0;
804            if (!width) {
805                GetTextExtent(str, &width, NULL);
[d171c0c]806            }
[c13d121f]807            int x = elev_centre_x - width / 2;
808            DrawIndicatorText(x, y_off + height / 2, str);
809        } else if (m_TiltAngle == 90.0) {
810            // TRANSLATORS: Label used for "clino" in Aven when the view is
811            // from directly below.
[1c507cf]812            //
813            // Try to keep this translation short - ideally at most 10
814            // characters - as otherwise the compass and clino will be moved
815            // further apart to make room. */
[c13d121f]816            wxString str = wmsg(/*Kiwi Plan*/433);
817            static int width = 0;
818            if (!width) {
819                GetTextExtent(str, &width, NULL);
[d171c0c]820            }
[c13d121f]821            int x = elev_centre_x - width / 2;
822            DrawIndicatorText(x, y_off + height / 2, str);
823        } else {
824            int angle;
825            wxString str;
826            int width;
827            int unit;
828            if (m_Percent) {
829                static int zero_width = 0;
830                if (!zero_width) {
831                    GetTextExtent(wxT("0"), &zero_width, NULL);
832                }
833                width = zero_width;
834                if (m_TiltAngle > 89.99) {
835                    angle = 1000000;
836                } else if (m_TiltAngle < -89.99) {
837                    angle = -1000000;
838                } else {
839                    angle = int(100 * tan(rad(m_TiltAngle)));
840                }
841                if (angle > 99999 || angle < -99999) {
842                    str = angle > 0 ? wxT("+") : wxT("-");
[0b8c321]843                    /* TRANSLATORS: infinity symbol - used for the percentage gradient on
[feefc6a]844                     * vertical angles. */
[c13d121f]845                    str += wmsg(/*∞*/431);
846                } else {
847                    str = angle ? wxString::Format(wxT("%+03d"), angle) : wxT("0");
848                }
[feefc6a]849                /* TRANSLATORS: symbol for percentage gradient (100% = 45
850                 * degrees = 50 grad). */
[c13d121f]851                unit = /*%*/96;
852            } else if (m_Degrees) {
853                static int zero_zero_width = 0;
854                if (!zero_zero_width) {
855                    GetTextExtent(wxT("00"), &zero_zero_width, NULL);
856                }
857                width = zero_zero_width;
858                angle = int(m_TiltAngle);
859                str = angle ? wxString::Format(wxT("%+03d"), angle) : wxT("00");
860                unit = /*°*/344;
[d171c0c]861            } else {
[c13d121f]862                width = triple_zero_width;
863                angle = int(m_TiltAngle * 200.0 / 180.0);
864                str = angle ? wxString::Format(wxT("%+04d"), angle) : wxT("000");
865                unit = /*ᵍ*/76;
[21958ec]866            }
867
[c13d121f]868            int sign_offset = 0;
869            if (unit == /*%*/96) {
870                // Right align % since the width changes so much.
871                GetTextExtent(str, &sign_offset, NULL);
872                sign_offset -= width;
873            } else if (angle < 0) {
874                // Adjust horizontal position so the left of the first digit is
875                // always in the same place.
876                static int minus_width = 0;
877                if (!minus_width) {
878                    GetTextExtent(wxT("-"), &minus_width, NULL);
879                }
880                sign_offset = minus_width;
881            } else if (angle > 0) {
882                // Adjust horizontal position so the left of the first digit is
883                // always in the same place.
884                static int plus_width = 0;
885                if (!plus_width) {
886                    GetTextExtent(wxT("+"), &plus_width, NULL);
887                }
888                sign_offset = plus_width;
[21958ec]889            }
[d171c0c]890
[c13d121f]891            str += wmsg(unit);
892            DrawIndicatorText(elev_centre_x - sign_offset - width / 2, y_off, str);
[21958ec]893
[1c507cf]894            // TRANSLATORS: Label used for "clino" in Aven when the view is
895            // neither from directly above nor from directly below.  It is
896            // also used in the dialog for editing a marked position in a
897            // presentation.
898            //
899            // Try to keep this translation short - ideally at most 10
900            // characters - as otherwise the compass and clino will be moved
901            // further apart to make room. */
[c13d121f]902            str = wmsg(/*Elevation*/118);
903            static int elevation_width = 0;
904            if (!elevation_width) {
905                GetTextExtent(str, &elevation_width, NULL);
906            }
907            int x = elev_centre_x - elevation_width / 2;
908            DrawIndicatorText(x, y_off + height, str);
[d171c0c]909        }
[c300a04]910    }
[5809313]911}
912
913void GfxCore::NattyDrawNames()
914{
[84cab34]915    // Draw station names, without overlapping.
[f433fda]916
[d92d282]917    const unsigned int quantise(GetFontSize() / QUANTISE_FACTOR);
[90430f2]918    const unsigned int quantised_x = GetXSize() / quantise;
919    const unsigned int quantised_y = GetYSize() / quantise;
[84cab34]920    const size_t buffer_size = quantised_x * quantised_y;
[f433fda]921
[69463a0]922    if (!m_LabelGrid) m_LabelGrid = new char[buffer_size];
[5809313]923
[33b2094]924    memset((void*) m_LabelGrid, 0, buffer_size);
[156dc16]925
[33b2094]926    list<LabelInfo*>::const_iterator label = m_Parent->GetLabels();
[36c3285]927    for ( ; label != m_Parent->GetLabelsEnd(); ++label) {
[ece003f]928        if (!((m_Surface && (*label)->IsSurface()) ||
929              (m_Legs && (*label)->IsUnderground()) ||
930              (!(*label)->IsSurface() && !(*label)->IsUnderground()))) {
931            // if this station isn't to be displayed, skip to the next
932            // (last case is for stns with no legs attached)
933            continue;
934        }
935
[f6d8375]936        double x, y, z;
[f433fda]937
[d67450e]938        Transform(**label, &x, &y, &z);
[36c3285]939        // Check if the label is behind us (in perspective view).
[d92d282]940        if (z <= 0.0 || z >= 1.0) continue;
[421b7d2]941
[1eeb55a]942        // Apply a small shift so that translating the view doesn't make which
943        // labels are displayed change as the resulting twinkling effect is
944        // distracting.
[f6d8375]945        double tx, ty, tz;
[d67450e]946        Transform(Vector3(), &tx, &ty, &tz);
[429465a]947        tx -= floor(tx / quantise) * quantise;
948        ty -= floor(ty / quantise) * quantise;
[5809313]949
[f12e8cc]950        tx = x - tx;
951        if (tx < 0) continue;
952
953        ty = y - ty;
954        if (ty < 0) continue;
[421b7d2]955
[f12e8cc]956        unsigned int iy = unsigned(ty) / quantise;
[d92d282]957        if (iy >= quantised_y) continue;
[5a24583]958        unsigned int width = (*label)->get_width();
[f12e8cc]959        unsigned int ix = unsigned(tx) / quantise;
960        if (ix + width >= quantised_x) continue;
[33b2094]961
[f12e8cc]962        char * test = m_LabelGrid + ix + iy * quantised_x;
[d92d282]963        if (memchr(test, 1, width)) continue;
[33b2094]964
[8bd480e]965        x += 3;
966        y -= GetFontSize() / 2;
[d92d282]967        DrawIndicatorText((int)x, (int)y, (*label)->GetText());
968
969        if (iy > QUANTISE_FACTOR) iy = QUANTISE_FACTOR;
970        test -= quantised_x * iy;
[f12e8cc]971        iy += 4;
972        while (--iy && test < m_LabelGrid + buffer_size) {
[d92d282]973            memset(test, 1, width);
974            test += quantised_x;
[429465a]975        }
[84cab34]976    }
[5809313]977}
978
979void GfxCore::SimpleDrawNames()
980{
[a8aedf4]981    // Draw all station names, without worrying about overlaps
[d5de678]982    list<LabelInfo*>::const_iterator label = m_Parent->GetLabels();
[01d91fd]983    for ( ; label != m_Parent->GetLabelsEnd(); ++label) {
[ece003f]984        if (!((m_Surface && (*label)->IsSurface()) ||
985              (m_Legs && (*label)->IsUnderground()) ||
986              (!(*label)->IsSurface() && !(*label)->IsUnderground()))) {
987            // if this station isn't to be displayed, skip to the next
988            // (last case is for stns with no legs attached)
989            continue;
990        }
991
[f6d8375]992        double x, y, z;
[d67450e]993        Transform(**label, &x, &y, &z);
[d92d282]994
995        // Check if the label is behind us (in perspective view).
996        if (z <= 0) continue;
997
[8bd480e]998        x += 3;
999        y -= GetFontSize() / 2;
[d92d282]1000        DrawIndicatorText((int)x, (int)y, (*label)->GetText());
[84cab34]1001    }
[5809313]1002}
1003
[825bdff]1004void GfxCore::DrawColourKey(int num_bands, const wxString & other, const wxString & units)
[5809313]1005{
[7cdb1c3]1006    int total_block_height =
[62da267]1007        KEY_BLOCK_HEIGHT * (num_bands == 1 ? num_bands : num_bands - 1);
1008    if (!other.empty()) total_block_height += KEY_BLOCK_HEIGHT * 2;
[825bdff]1009    if (!units.empty()) total_block_height += KEY_BLOCK_HEIGHT;
[5809313]1010
[3585243]1011    const int bottom = -total_block_height;
[a74b014]1012
[d4650b3]1013    int size = 0;
[7cdb1c3]1014    if (!other.empty()) GetTextExtent(other, &size, NULL);
[d4650b3]1015    int band;
[2043961]1016    for (band = 0; band < num_bands; ++band) {
[a74b014]1017        int x;
[7cdb1c3]1018        GetTextExtent(key_legends[band], &x, NULL);
[d4650b3]1019        if (x > size) size = x;
1020    }
1021
[3585243]1022    int left = -KEY_BLOCK_WIDTH - size;
[d4650b3]1023
[fcc3741]1024    key_lowerleft[m_ColourBy].x = left - KEY_EXTRA_LEFT_MARGIN;
1025    key_lowerleft[m_ColourBy].y = bottom;
[d4650b3]1026
[d43fa84]1027    int y = bottom;
[825bdff]1028    if (!units.empty()) y += KEY_BLOCK_HEIGHT;
[a74b014]1029
[7cdb1c3]1030    if (!other.empty()) {
[a74b014]1031        DrawShadedRectangle(GetSurfacePen(), GetSurfacePen(), left, y,
[62da267]1032                KEY_BLOCK_WIDTH, KEY_BLOCK_HEIGHT);
[2779338f]1033        SetColour(col_BLACK);
1034        BeginPolyline();
1035        PlaceIndicatorVertex(left, y);
1036        PlaceIndicatorVertex(left + KEY_BLOCK_WIDTH, y);
1037        PlaceIndicatorVertex(left + KEY_BLOCK_WIDTH, y + KEY_BLOCK_HEIGHT);
1038        PlaceIndicatorVertex(left, y + KEY_BLOCK_HEIGHT);
1039        PlaceIndicatorVertex(left, y);
1040        EndPolyline();
[62da267]1041        y += KEY_BLOCK_HEIGHT * 2;
[a74b014]1042    }
1043
[2779338f]1044    int start = y;
[2043961]1045    if (num_bands == 1) {
1046        DrawShadedRectangle(GetPen(0), GetPen(0), left, y,
[62da267]1047                            KEY_BLOCK_WIDTH, KEY_BLOCK_HEIGHT);
[2779338f]1048        y += KEY_BLOCK_HEIGHT;
[2043961]1049    } else {
1050        for (band = 0; band < num_bands - 1; ++band) {
1051            DrawShadedRectangle(GetPen(band), GetPen(band + 1), left, y,
[62da267]1052                                KEY_BLOCK_WIDTH, KEY_BLOCK_HEIGHT);
1053            y += KEY_BLOCK_HEIGHT;
[2043961]1054        }
[d4650b3]1055    }
1056
[2779338f]1057    SetColour(col_BLACK);
1058    BeginPolyline();
1059    PlaceIndicatorVertex(left, y);
1060    PlaceIndicatorVertex(left + KEY_BLOCK_WIDTH, y);
1061    PlaceIndicatorVertex(left + KEY_BLOCK_WIDTH, start);
1062    PlaceIndicatorVertex(left, start);
1063    PlaceIndicatorVertex(left, y);
1064    EndPolyline();
1065
[825bdff]1066    SetColour(TEXT_COLOUR);
1067
1068    y = bottom;
1069    if (!units.empty()) {
1070        GetTextExtent(units, &size, NULL);
1071        DrawIndicatorText(left + (KEY_BLOCK_WIDTH - size) / 2, y, units);
1072        y += KEY_BLOCK_HEIGHT;
1073    }
1074    y -= GetFontSize() / 2;
[62da267]1075    left += KEY_BLOCK_WIDTH + 5;
[d4650b3]1076
[7cdb1c3]1077    if (!other.empty()) {
[62da267]1078        y += KEY_BLOCK_HEIGHT / 2;
[7cdb1c3]1079        DrawIndicatorText(left, y, other);
[62da267]1080        y += KEY_BLOCK_HEIGHT * 2 - KEY_BLOCK_HEIGHT / 2;
[a74b014]1081    }
1082
[2043961]1083    if (num_bands == 1) {
[62da267]1084        y += KEY_BLOCK_HEIGHT / 2;
[7cdb1c3]1085        DrawIndicatorText(left, y, key_legends[0]);
[2043961]1086    } else {
[62c5fc6]1087        for (band = 0; band < num_bands; ++band) {
[7cdb1c3]1088            DrawIndicatorText(left, y, key_legends[band]);
[62da267]1089            y += KEY_BLOCK_HEIGHT;
[2043961]1090        }
[d4650b3]1091    }
1092}
1093
[e2ea75a]1094void GfxCore::DrawDepthKey()
[c61aa79]1095{
[62c5fc6]1096    Double z_ext = m_Parent->GetDepthExtent();
[825bdff]1097    int num_bands = 1;
1098    int sf = 0;
1099    if (z_ext > 0.0) {
[62c5fc6]1100        num_bands = GetNumColourBands();
[825bdff]1101        Double z_range = z_ext;
1102        if (!m_Metric) z_range /= METRES_PER_FOOT;
1103        sf = max(0, 1 - (int)floor(log10(z_range)));
[62c5fc6]1104    }
1105
[825bdff]1106    Double z_min = m_Parent->GetDepthMin() + m_Parent->GetOffset().GetZ();
1107    for (int band = 0; band < num_bands; ++band) {
1108        Double z = z_min;
[62c5fc6]1109        if (band)
1110            z += z_ext * band / (num_bands - 1);
[825bdff]1111
1112        if (!m_Metric)
1113            z /= METRES_PER_FOOT;
1114
1115        key_legends[band].Printf(wxT("%.*f"), sf, z);
[c61aa79]1116    }
1117
[ccb83b7]1118    DrawColourKey(num_bands, wxString(), wmsg(m_Metric ? /*m*/424: /*ft*/428));
[7cdb1c3]1119}
[a74b014]1120
[e2ea75a]1121void GfxCore::DrawDateKey()
[7cdb1c3]1122{
1123    int num_bands;
[62c5fc6]1124    if (!HasDateInformation()) {
1125        num_bands = 0;
[7cdb1c3]1126    } else {
[62c5fc6]1127        int date_ext = m_Parent->GetDateExtent();
1128        if (date_ext == 0) {
1129            num_bands = 1;
1130        } else {
1131            num_bands = GetNumColourBands();
1132        }
1133        for (int band = 0; band < num_bands; ++band) {
1134            int y, m, d;
1135            int days = m_Parent->GetDateMin();
1136            if (band)
1137                days += date_ext * band / (num_bands - 1);
1138            ymd_from_days_since_1900(days, &y, &m, &d);
1139            key_legends[band].Printf(wxT("%04d-%02d-%02d"), y, m, d);
1140        }
[7cdb1c3]1141    }
[62c5fc6]1142
[7cdb1c3]1143    wxString other;
1144    if (!m_Parent->HasCompleteDateInfo()) {
[736f7df]1145        /* TRANSLATORS: Used in the "colour key" for "colour by date" if there
[0b8c321]1146         * are surveys without date information.  Try to keep this fairly short.
1147         */
[7cdb1c3]1148        other = wmsg(/*Undated*/221);
[c61aa79]1149    }
1150
[825bdff]1151    DrawColourKey(num_bands, other, wxString());
[7cdb1c3]1152}
[a74b014]1153
[e2ea75a]1154void GfxCore::DrawErrorKey()
[7cdb1c3]1155{
[62c5fc6]1156    int num_bands;
1157    if (HasErrorInformation()) {
1158        // Use fixed colours for each error factor so it's directly visually
1159        // comparable between surveys.
1160        num_bands = GetNumColourBands();
1161        for (int band = 0; band < num_bands; ++band) {
1162            double E = MAX_ERROR * band / (num_bands - 1);
1163            key_legends[band].Printf(wxT("%.2f"), E);
1164        }
1165    } else {
1166        num_bands = 0;
[c61aa79]1167    }
1168
[7cdb1c3]1169    // Always show the "Not in loop" legend for now (FIXME).
[736f7df]1170    /* TRANSLATORS: Used in the "colour key" for "colour by error" for surveys
[0b8c321]1171     * which aren’t part of a loop and so have no error information. Try to keep
1172     * this fairly short. */
[825bdff]1173    DrawColourKey(num_bands, wmsg(/*Not in loop*/290), wxString());
[ac537e9]1174}
1175
[cc9e2c65]1176void GfxCore::DrawGradientKey()
1177{
1178    int num_bands;
1179    // Use fixed colours for each gradient so it's directly visually comparable
1180    // between surveys.
1181    num_bands = GetNumColourBands();
1182    wxString units = wmsg(m_Degrees ? /*°*/344 : /*ᵍ*/76);
1183    for (int band = 0; band < num_bands; ++band) {
1184        double gradient = double(band) / (num_bands - 1);
1185        if (m_Degrees) {
1186            gradient *= 90.0;
1187        } else {
1188            gradient *= 100.0;
1189        }
1190        key_legends[band].Printf(wxT("%.f%s"), gradient, units);
1191    }
1192
1193    DrawColourKey(num_bands, wxString(), wxString());
1194}
1195
[af50685]1196void GfxCore::DrawLengthKey()
1197{
1198    int num_bands;
1199    // Use fixed colours for each length so it's directly visually comparable
1200    // between surveys.
1201    num_bands = GetNumColourBands();
1202    for (int band = 0; band < num_bands; ++band) {
1203        double len = pow(10, LOG_LEN_MAX * band / (num_bands - 1));
1204        if (!m_Metric) {
1205            len /= METRES_PER_FOOT;
1206        }
[355809f]1207        key_legends[band].Printf(wxT("%.1f"), len);
[af50685]1208    }
1209
[355809f]1210    DrawColourKey(num_bands, wxString(), wmsg(m_Metric ? /*m*/424: /*ft*/428));
[af50685]1211}
1212
[9c37beb]1213void GfxCore::DrawScaleBar()
[5809313]1214{
[84cab34]1215    // Draw the scalebar.
[eef68f9]1216    if (GetPerspective()) return;
[5809313]1217
[37bc1f5]1218    // Calculate how many metres of survey are currently displayed across the
1219    // screen.
[087bc72]1220    Double across_screen = SurveyUnitsAcrossViewport();
[156dc16]1221
[90430f2]1222    double f = double(GetClinoXPosition() - INDICATOR_BOX_SIZE / 2 - SCALE_BAR_OFFSET_X) / GetXSize();
[5f50488]1223    if (f > 0.75) {
1224        f = 0.75;
1225    } else if (f < 0.5) {
1226        // Stop it getting squeezed to nothing.
1227        // FIXME: In this case we should probably move the compass and clino up
1228        // to make room rather than letting stuff overlap.
1229        f = 0.5;
1230    }
1231
[087bc72]1232    // Convert to imperial measurements if required.
[7a89dc2]1233    Double multiplier = 1.0;
1234    if (!m_Metric) {
[429465a]1235        across_screen /= METRES_PER_FOOT;
1236        multiplier = METRES_PER_FOOT;
[5f50488]1237        if (across_screen >= 5280.0 / f) {
[429465a]1238            across_screen /= 5280.0;
1239            multiplier *= 5280.0;
1240        }
[7a89dc2]1241    }
[5757725]1242
[7a89dc2]1243    // Calculate the length of the scale bar.
[5f50488]1244    Double size_snap = pow(10.0, floor(log10(f * across_screen)));
1245    Double t = across_screen * f / size_snap;
[98860c5]1246    if (t >= 5.0) {
[429465a]1247        size_snap *= 5.0;
[7a89dc2]1248    } else if (t >= 2.0) {
[429465a]1249        size_snap *= 2.0;
[98860c5]1250    }
1251
[7a89dc2]1252    if (!m_Metric) size_snap *= multiplier;
1253
[84cab34]1254    // Actual size of the thing in pixels:
[90430f2]1255    int size = int((size_snap / SurveyUnitsAcrossViewport()) * GetXSize());
[e2c1671]1256    m_ScaleBarWidth = size;
[421b7d2]1257
[5809313]1258    // Draw it...
[e2c1671]1259    const int end_y = SCALE_BAR_OFFSET_Y + SCALE_BAR_HEIGHT;
[5809313]1260    int interval = size / 10;
1261
[aa048c3]1262    gla_colour col = col_WHITE;
[5809313]1263    for (int ix = 0; ix < 10; ix++) {
[e2c1671]1264        int x = SCALE_BAR_OFFSET_X + int(ix * ((Double) size / 10.0));
[421b7d2]1265
[e2c1671]1266        DrawRectangle(col, col, x, end_y, interval + 2, SCALE_BAR_HEIGHT);
[421b7d2]1267
[aa048c3]1268        col = (col == col_WHITE) ? col_GREY : col_WHITE;
[5809313]1269    }
1270
[84cab34]1271    // Add labels.
[825bdff]1272    wxString str;
[ccb83b7]1273    int units;
[825bdff]1274    if (m_Metric) {
[ccb83b7]1275        Double km = size_snap * 1e-3;
1276        if (km >= 1.0) {
1277            size_snap = km;
[736f7df]1278            /* TRANSLATORS: abbreviation for "kilometres" (unit of length),
1279             * used e.g.  "5km".
1280             *
1281             * If there should be a space between the number and this, include
1282             * one in the translation. */
[ccb83b7]1283            units = /*km*/423;
[825bdff]1284        } else if (size_snap >= 1.0) {
[736f7df]1285            /* TRANSLATORS: abbreviation for "metres" (unit of length), used
1286             * e.g. "10m".
[b49ac56]1287             *
[736f7df]1288             * If there should be a space between the number and this, include
1289             * one in the translation. */
[ccb83b7]1290            units = /*m*/424;
[825bdff]1291        } else {
[ccb83b7]1292            size_snap *= 1e2;
[736f7df]1293            /* TRANSLATORS: abbreviation for "centimetres" (unit of length),
1294             * used e.g.  "50cm".
[b49ac56]1295             *
[736f7df]1296             * If there should be a space between the number and this, include
1297             * one in the translation. */
[ccb83b7]1298            units = /*cm*/425;
[825bdff]1299        }
1300    } else {
1301        size_snap /= METRES_PER_FOOT;
1302        Double miles = size_snap / 5280.0;
[ccb83b7]1303        if (miles >= 1.0) {
1304            size_snap = miles;
1305            if (size_snap >= 2.0) {
[736f7df]1306                /* TRANSLATORS: abbreviation for "miles" (unit of length,
1307                 * plural), used e.g.  "2 miles".
[b49ac56]1308                 *
[736f7df]1309                 * If there should be a space between the number and this,
1310                 * include one in the translation. */
[ccb83b7]1311                units = /* miles*/426;
[825bdff]1312            } else {
[736f7df]1313                /* TRANSLATORS: abbreviation for "mile" (unit of length,
1314                 * singular), used e.g.  "1 mile".
[b49ac56]1315                 *
[736f7df]1316                 * If there should be a space between the number and this,
1317                 * include one in the translation. */
[ccb83b7]1318                units = /* mile*/427;
[825bdff]1319            }
[ccb83b7]1320        } else if (size_snap >= 1.0) {
[736f7df]1321            /* TRANSLATORS: abbreviation for "feet" (unit of length), used e.g.
1322             * as "10ft".
[b49ac56]1323             *
[736f7df]1324             * If there should be a space between the number and this, include
1325             * one in the translation. */
[ccb83b7]1326            units = /*ft*/428;
1327        } else {
1328            size_snap *= 12.0;
[736f7df]1329            /* TRANSLATORS: abbreviation for "inches" (unit of length), used
1330             * e.g. as "6in".
[b49ac56]1331             *
[736f7df]1332             * If there should be a space between the number and this, include
1333             * one in the translation. */
[ccb83b7]1334            units = /*in*/429;
[825bdff]1335        }
1336    }
[ccb83b7]1337    if (size_snap >= 1.0) {
1338        str.Printf(wxT("%.f%s"), size_snap, wmsg(units).c_str());
1339    } else {
1340        int sf = -(int)floor(log10(size_snap));
1341        str.Printf(wxT("%.*f%s"), sf, size_snap, wmsg(units).c_str());
1342    }
[84cab34]1343
[1eeb55a]1344    int text_width, text_height;
[56da40e]1345    GetTextExtent(str, &text_width, &text_height);
[8bd480e]1346    const int text_y = end_y - text_height + 1;
1347    SetColour(TEXT_COLOUR);
[5627cbb]1348    DrawIndicatorText(SCALE_BAR_OFFSET_X, text_y, wxT("0"));
[8bd480e]1349    DrawIndicatorText(SCALE_BAR_OFFSET_X + size - text_width, text_y, str);
[5809313]1350}
[56da40e]1351
[2072157]1352bool GfxCore::CheckHitTestGrid(const wxPoint& point, bool centre)
[2effbf1]1353{
[0874c07e]1354    if (Animating()) return false;
1355
[90430f2]1356    if (point.x < 0 || point.x >= GetXSize() ||
1357        point.y < 0 || point.y >= GetYSize()) {
[429465a]1358        return false;
[137e31b]1359    }
[421b7d2]1360
[156f645]1361    SetDataTransform();
[00a68e0]1362
[69463a0]1363    if (!m_HitTestGridValid) CreateHitTestGrid();
[fa42426]1364
[0b0520c]1365    int grid_x = point.x * HITTEST_SIZE / (GetXSize() + 1);
1366    int grid_y = point.y * HITTEST_SIZE / (GetYSize() + 1);
[137e31b]1367
[fa42426]1368    LabelInfo *best = NULL;
[d96c95c]1369    int dist_sqrd = sqrd_measure_threshold;
[2effbf1]1370    int square = grid_x + grid_y * HITTEST_SIZE;
[fa42426]1371    list<LabelInfo*>::iterator iter = m_PointGrid[square].begin();
[00a68e0]1372
[fa42426]1373    while (iter != m_PointGrid[square].end()) {
[429465a]1374        LabelInfo *pt = *iter++;
[fa42426]1375
[f6d8375]1376        double cx, cy, cz;
[00a68e0]1377
[d67450e]1378        Transform(*pt, &cx, &cy, &cz);
[00a68e0]1379
[90430f2]1380        cy = GetYSize() - cy;
[00a68e0]1381
[429465a]1382        int dx = point.x - int(cx);
1383        int ds = dx * dx;
1384        if (ds >= dist_sqrd) continue;
1385        int dy = point.y - int(cy);
[fa42426]1386
[429465a]1387        ds += dy * dy;
1388        if (ds >= dist_sqrd) continue;
[f433fda]1389
[429465a]1390        dist_sqrd = ds;
1391        best = pt;
[f433fda]1392
[429465a]1393        if (ds == 0) break;
[2effbf1]1394    }
[f433fda]1395
[fa42426]1396    if (best) {
[381ae6e]1397        m_Parent->ShowInfo(best, m_there);
[429465a]1398        if (centre) {
[e67ed1b]1399            // FIXME: allow Ctrl-Click to not set there or something?
[82c3731]1400            CentreOn(*best);
[90430f2]1401            WarpPointer(GetXSize() / 2, GetYSize() / 2);
[381ae6e]1402            SetThere(best);
[429465a]1403            m_Parent->SelectTreeItem(best);
1404        }
[e67ed1b]1405    } else {
1406        // Left-clicking not on a survey cancels the measuring line.
[0633bcc]1407        if (centre) {
1408            ClearTreeSelection();
1409        } else {
[381ae6e]1410            m_Parent->ShowInfo(best, m_there);
[f6d8375]1411            double x, y, z;
[90430f2]1412            ReverseTransform(point.x, GetYSize() - point.y, &x, &y, &z);
[381ae6e]1413            temp_here.assign(Vector3(x, y, z));
1414            SetHere(&temp_here);
[0633bcc]1415        }
[2effbf1]1416    }
[203d2a7]1417
1418    return best;
[2effbf1]1419}
1420
[5876fcb]1421void GfxCore::OnSize(wxSizeEvent& event)
[5809313]1422{
[5876fcb]1423    // Handle a change in window size.
1424    wxSize size = event.GetSize();
[5809313]1425
[78beaf1]1426    if (size.GetWidth() <= 0 || size.GetHeight() <= 0) {
[0580c6a]1427        // Before things are fully initialised, we sometimes get a bogus
1428        // resize message...
[6ef8bd73]1429        // FIXME have changes in MainFrm cured this?  It still happens with
[880b954]1430        // 1.0.32 and wxGTK 2.5.2 (load a file from the command line).
1431        // With 1.1.6 and wxGTK 2.4.2 we only get negative sizes if MainFrm
[78beaf1]1432        // is resized such that the GfxCore window isn't visible.
1433        //printf("OnSize(%d,%d)\n", size.GetWidth(), size.GetHeight());
[0580c6a]1434        return;
1435    }
[b72f4b5]1436
[9071cf5]1437    event.Skip();
[39e460c9]1438
[5876fcb]1439    if (m_DoneFirstShow) {
[81f1266]1440        TryToFreeArrays();
[6ebc0ce]1441
[69463a0]1442        m_HitTestGridValid = false;
[33b2094]1443
[d67450e]1444        ForceRefresh();
[5809313]1445    }
1446}
1447
[de7a879]1448void GfxCore::DefaultParameters()
[5809313]1449{
[33b2094]1450    // Set default viewing parameters.
[b462168]1451
[f433fda]1452    m_Surface = false;
1453    if (!m_Parent->HasUndergroundLegs()) {
1454        if (m_Parent->HasSurfaceLegs()) {
1455            // If there are surface legs, but no underground legs, turn
1456            // surface surveys on.
1457            m_Surface = true;
1458        } else {
1459            // If there are no legs (e.g. after loading a .pos file), turn
1460            // crosses on.
1461            m_Crosses = true;
1462        }
[b462168]1463    }
1464
[714daae]1465    m_PanAngle = 0.0;
[eef68f9]1466    if (m_Parent->IsExtendedElevation()) {
1467        m_TiltAngle = 0.0;
1468    } else {
[7a57dc7]1469        m_TiltAngle = -90.0;
[b462168]1470    }
[714daae]1471
[08253d9]1472    SetRotation(m_PanAngle, m_TiltAngle);
[d67450e]1473    SetTranslation(Vector3());
[33b2094]1474
[e577f89]1475    m_RotationStep = 30.0;
[5809313]1476    m_Rotating = false;
[3d00693]1477    m_SwitchingTo = 0;
[fe444b8]1478    m_Entrances = false;
1479    m_FixedPts = false;
1480    m_ExportedPts = false;
[c1cf79d]1481    m_Grid = false;
[f4c5932]1482    m_BoundingBox = false;
[33b2094]1483    m_Tubes = false;
[1eeb55a]1484    if (GetPerspective()) TogglePerspective();
[39bd2ef]1485
1486    // Set the initial scale.
1487    SetScale(initial_scale);
[de7a879]1488}
[5809313]1489
[de7a879]1490void GfxCore::Defaults()
1491{
1492    // Restore default scale, rotation and translation parameters.
1493    DefaultParameters();
[ba358fc]1494
1495    // Invalidate all the cached lists.
1496    GLACanvas::FirstShow();
1497
[fa42426]1498    ForceRefresh();
[5809313]1499}
[84cab34]1500
[5455bb2]1501void GfxCore::Animate()
[5809313]1502{
[2a3d328]1503    // Don't show pointer coordinates while animating.
[4b031c0]1504    // FIXME : only do this when we *START* animating!  Use a static copy
1505    // of the value of "Animating()" last time we were here to track this?
1506    // MainFrm now checks if we're trying to clear already cleared labels
1507    // and just returns, but it might be simpler to check here!
[2a3d328]1508    ClearCoords();
[381ae6e]1509    m_Parent->ShowInfo();
[5809313]1510
[5455bb2]1511    long t;
[75d4a2b]1512    if (movie) {
[aea4f8b]1513        ReadPixels(movie->GetWidth(), movie->GetHeight(), movie->GetBuffer());
[98fd937]1514        if (!movie->AddFrame()) {
1515            wxGetApp().ReportError(wxString(movie->get_error_string(), wxConvUTF8));
1516            delete movie;
1517            movie = NULL;
1518            presentation_mode = 0;
[5455bb2]1519            return;
[98fd937]1520        }
[5455bb2]1521        t = 1000 / 25; // 25 frames per second
[6a4cdcb6]1522    } else {
[5455bb2]1523        static long t_prev = 0;
1524        t = timer.Time();
1525        // Avoid redrawing twice in the same frame.
[ff1601e]1526        long delta_t = (t_prev == 0 ? 1000 / MAX_FRAMERATE : t - t_prev);
1527        if (delta_t < 1000 / MAX_FRAMERATE)
[6987d2a]1528            return;
[5455bb2]1529        t_prev = t;
[6987d2a]1530        if (presentation_mode == PLAYING && pres_speed != 0.0)
1531            t = delta_t;
[6a4cdcb6]1532    }
[5809313]1533
[128fac4]1534    if (presentation_mode == PLAYING && pres_speed != 0.0) {
[6987d2a]1535        // FIXME: It would probably be better to work relative to the time we
1536        // passed the last mark, but that's complicated by the speed
1537        // potentially changing (or even the direction of playback reversing)
1538        // at any point during playback.
1539        Double tick = t * 0.001 * fabs(pres_speed);
1540        while (tick >= next_mark_time) {
1541            tick -= next_mark_time;
[128fac4]1542            this_mark_total = 0;
[58dfdd21]1543            PresentationMark prev_mark = next_mark;
[e577f89]1544            if (prev_mark.angle < 0) prev_mark.angle += 360.0;
1545            else if (prev_mark.angle >= 360.0) prev_mark.angle -= 360.0;
[128fac4]1546            if (pres_reverse)
1547                next_mark = m_Parent->GetPresMark(MARK_PREV);
1548            else
1549                next_mark = m_Parent->GetPresMark(MARK_NEXT);
[1690fa9]1550            if (!next_mark.is_valid()) {
[128fac4]1551                SetView(prev_mark);
[1690fa9]1552                presentation_mode = 0;
[387babf]1553                if (movie && !movie->Close()) {
[98fd937]1554                    wxGetApp().ReportError(wxString(movie->get_error_string(), wxConvUTF8));
1555                }
[81f1266]1556                delete movie;
1557                movie = NULL;
[1690fa9]1558                break;
1559            }
[58dfdd21]1560
[128fac4]1561            double tmp = (pres_reverse ? prev_mark.time : next_mark.time);
1562            if (tmp > 0) {
1563                next_mark_time = tmp;
[58dfdd21]1564            } else {
[d67450e]1565                double d = (next_mark - prev_mark).magnitude();
[49ce5b0]1566                // FIXME: should ignore component of d which is unseen in
1567                // non-perspective mode?
[8674eea]1568                next_mark_time = sqrd(d / 30.0);
[49ce5b0]1569                double a = next_mark.angle - prev_mark.angle;
1570                if (a > 180.0) {
1571                    next_mark.angle -= 360.0;
1572                    a = 360.0 - a;
1573                } else if (a < -180.0) {
1574                    next_mark.angle += 360.0;
1575                    a += 360.0;
1576                } else {
1577                    a = fabs(a);
1578                }
1579                next_mark_time += sqrd(a / 60.0);
1580                double ta = fabs(next_mark.tilt_angle - prev_mark.tilt_angle);
1581                next_mark_time += sqrd(ta / 60.0);
1582                double s = fabs(log(next_mark.scale) - log(prev_mark.scale));
[8674eea]1583                next_mark_time += sqrd(s / 2.0);
[49ce5b0]1584                next_mark_time = sqrt(next_mark_time);
[8674eea]1585                // was: next_mark_time = max(max(d / 30, s / 2), max(a, ta) / 60);
[49ce5b0]1586                //printf("*** %.6f from (\nd: %.6f\ns: %.6f\na: %.6f\nt: %.6f )\n",
[8674eea]1587                //       next_mark_time, d/30.0, s/2.0, a/60.0, ta/60.0);
1588                if (tmp < 0) next_mark_time /= -tmp;
[58dfdd21]1589            }
[1690fa9]1590        }
1591
1592        if (presentation_mode) {
1593            // Advance position towards next_mark
[6987d2a]1594            double p = tick / next_mark_time;
[1690fa9]1595            double q = 1 - p;
1596            PresentationMark here = GetView();
[d877aa2]1597            if (next_mark.angle < 0) {
[e577f89]1598                if (here.angle >= next_mark.angle + 360.0)
1599                    here.angle -= 360.0;
1600            } else if (next_mark.angle >= 360.0) {
1601                if (here.angle <= next_mark.angle - 360.0)
1602                    here.angle += 360.0;
[d877aa2]1603            }
[8674eea]1604            here.assign(q * here + p * next_mark);
[1690fa9]1605            here.angle = q * here.angle + p * next_mark.angle;
[e577f89]1606            if (here.angle < 0) here.angle += 360.0;
1607            else if (here.angle >= 360.0) here.angle -= 360.0;
[1690fa9]1608            here.tilt_angle = q * here.tilt_angle + p * next_mark.tilt_angle;
[58dfdd21]1609            here.scale = exp(q * log(here.scale) + p * log(next_mark.scale));
[1690fa9]1610            SetView(here);
[6987d2a]1611            this_mark_total += tick;
1612            next_mark_time -= tick;
[1690fa9]1613        }
[6987d2a]1614
1615        ForceRefresh();
1616        return;
[1690fa9]1617    }
1618
[5876fcb]1619    // When rotating...
1620    if (m_Rotating) {
[5455bb2]1621        Double step = base_pan + (t - base_pan_time) * 1e-3 * m_RotationStep - m_PanAngle;
1622        TurnCave(step);
[5876fcb]1623    }
[5809313]1624
[5876fcb]1625    if (m_SwitchingTo == PLAN) {
[429465a]1626        // When switching to plan view...
[5455bb2]1627        Double step = base_tilt - (t - base_tilt_time) * 1e-3 * 90.0 - m_TiltAngle;
1628        TiltCave(step);
[7a57dc7]1629        if (m_TiltAngle == -90.0) {
[429465a]1630            m_SwitchingTo = 0;
1631        }
[1690fa9]1632    } else if (m_SwitchingTo == ELEVATION) {
[429465a]1633        // When switching to elevation view...
[5455bb2]1634        Double step;
1635        if (m_TiltAngle > 0.0) {
1636            step = base_tilt - (t - base_tilt_time) * 1e-3 * 90.0 - m_TiltAngle;
[7a57dc7]1637        } else {
[5455bb2]1638            step = base_tilt + (t - base_tilt_time) * 1e-3 * 90.0 - m_TiltAngle;
[429465a]1639        }
[5455bb2]1640        if (fabs(step) >= fabs(m_TiltAngle)) {
[3ddd351]1641            m_SwitchingTo = 0;
[5455bb2]1642            step = -m_TiltAngle;
1643        }
1644        TiltCave(step);
1645    } else if (m_SwitchingTo) {
1646        // Rotate the shortest way around to the destination angle.  If we're
1647        // 180 off, we favour turning anticlockwise, as auto-rotation does by
1648        // default.
1649        Double target = (m_SwitchingTo - NORTH) * 90;
1650        Double diff = target - m_PanAngle;
1651        diff = fmod(diff, 360);
1652        if (diff <= -180)
1653            diff += 360;
1654        else if (diff > 180)
1655            diff -= 360;
1656        if (m_RotationStep < 0 && diff == 180.0)
1657            diff = -180.0;
1658        Double step = base_pan - m_PanAngle;
1659        Double delta = (t - base_pan_time) * 1e-3 * fabs(m_RotationStep);
1660        if (diff > 0) {
1661            step += delta;
[3ddd351]1662        } else {
[5455bb2]1663            step -= delta;
[3ddd351]1664        }
[5455bb2]1665        step = fmod(step, 360);
1666        if (step <= -180)
1667            step += 360;
1668        else if (step > 180)
1669            step -= 360;
1670        if (fabs(step) >= fabs(diff)) {
1671            m_SwitchingTo = 0;
1672            step = diff;
1673        }
1674        TurnCave(step);
[5876fcb]1675    }
[5809313]1676
[5455bb2]1677    ForceRefresh();
[5809313]1678}
[84cab34]1679
[0580c6a]1680// How much to allow around the box - this is because of the ring shape
1681// at one end of the line.
1682static const int HIGHLIGHTED_PT_SIZE = 2; // FIXME: tie in to blob and ring size
1683#define MARGIN (HIGHLIGHTED_PT_SIZE * 2 + 1)
[381ae6e]1684void GfxCore::RefreshLine(const Point *a, const Point *b, const Point *c)
[7a89dc2]1685{
[0f86024]1686#ifdef __WXMSW__
1687    (void)a;
1688    (void)b;
1689    (void)c;
1690    // FIXME: We get odd redraw artifacts if we just update the line, and
1691    // redrawing the whole scene doesn't actually seem to be measurably
1692    // slower.  That may not be true with software rendering though...
1693    ForceRefresh();
1694#else
[06d367d]1695    // Best of all might be to copy the window contents before we draw the
1696    // line, then replace each time we redraw.
[796d7bf]1697
[5876fcb]1698    // Calculate the minimum rectangle which includes the old and new
1699    // measuring lines to minimise the redraw time
1700    int l = INT_MAX, r = INT_MIN, u = INT_MIN, d = INT_MAX;
[f6d8375]1701    double X, Y, Z;
[381ae6e]1702    if (a) {
1703        if (!Transform(*a, &X, &Y, &Z)) {
[796d7bf]1704            printf("oops\n");
1705        } else {
1706            int x = int(X);
[90430f2]1707            int y = GetYSize() - 1 - int(Y);
[1c448e1]1708            l = x;
1709            r = x;
1710            u = y;
1711            d = y;
[796d7bf]1712        }
[5876fcb]1713    }
[381ae6e]1714    if (b) {
1715        if (!Transform(*b, &X, &Y, &Z)) {
[796d7bf]1716            printf("oops\n");
1717        } else {
1718            int x = int(X);
[90430f2]1719            int y = GetYSize() - 1 - int(Y);
[1c448e1]1720            l = min(l, x);
1721            r = max(r, x);
1722            u = max(u, y);
1723            d = min(d, y);
[796d7bf]1724        }
[5876fcb]1725    }
[381ae6e]1726    if (c) {
1727        if (!Transform(*c, &X, &Y, &Z)) {
[796d7bf]1728            printf("oops\n");
1729        } else {
1730            int x = int(X);
[90430f2]1731            int y = GetYSize() - 1 - int(Y);
[1c448e1]1732            l = min(l, x);
1733            r = max(r, x);
1734            u = max(u, y);
1735            d = min(d, y);
[796d7bf]1736        }
[5876fcb]1737    }
[1c448e1]1738    l -= MARGIN;
1739    r += MARGIN;
1740    u += MARGIN;
1741    d -= MARGIN;
[0ca8fe0]1742    RefreshRect(wxRect(l, d, r - l, u - d), false);
[0f86024]1743#endif
[7a89dc2]1744}
1745
[381ae6e]1746void GfxCore::SetHereFromTree(const LabelInfo * p)
1747{
1748    SetHere(p);
1749    m_Parent->ShowInfo(m_here, m_there);
1750}
1751
1752void GfxCore::SetHere(const LabelInfo *p)
[c6d95d8]1753{
[5d18821]1754    if (p == m_here) return;
[6b061db]1755    bool line_active = MeasuringLineActive();
[381ae6e]1756    const LabelInfo * old = m_here;
[82c3731]1757    m_here = p;
[6b061db]1758    if (line_active || MeasuringLineActive())
1759        RefreshLine(old, m_there, m_here);
[156dc16]1760}
1761
[381ae6e]1762void GfxCore::SetThere(const LabelInfo * p)
[156dc16]1763{
[5d18821]1764    if (p == m_there) return;
[381ae6e]1765    const LabelInfo * old = m_there;
[82c3731]1766    m_there = p;
[0580c6a]1767    RefreshLine(m_here, old, m_there);
[156dc16]1768}
1769
[5876fcb]1770void GfxCore::CreateHitTestGrid()
[156dc16]1771{
[7171240]1772    if (!m_PointGrid) {
1773        // Initialise hit-test grid.
1774        m_PointGrid = new list<LabelInfo*>[HITTEST_SIZE * HITTEST_SIZE];
1775    } else {
1776        // Clear hit-test grid.
1777        for (int i = 0; i < HITTEST_SIZE * HITTEST_SIZE; i++) {
1778            m_PointGrid[i].clear();
1779        }
[5876fcb]1780    }
[156dc16]1781
[5876fcb]1782    // Fill the grid.
1783    list<LabelInfo*>::const_iterator pos = m_Parent->GetLabels();
1784    list<LabelInfo*>::const_iterator end = m_Parent->GetLabelsEnd();
1785    while (pos != end) {
[429465a]1786        LabelInfo* label = *pos++;
[33b2094]1787
[429465a]1788        if (!((m_Surface && label->IsSurface()) ||
[dc42b8e]1789              (m_Legs && label->IsUnderground()) ||
1790              (!label->IsSurface() && !label->IsUnderground()))) {
1791            // if this station isn't to be displayed, skip to the next
1792            // (last case is for stns with no legs attached)
[429465a]1793            continue;
1794        }
[33b2094]1795
[429465a]1796        // Calculate screen coordinates.
[f6d8375]1797        double cx, cy, cz;
[d67450e]1798        Transform(*label, &cx, &cy, &cz);
[90430f2]1799        if (cx < 0 || cx >= GetXSize()) continue;
1800        if (cy < 0 || cy >= GetYSize()) continue;
[33b2094]1801
[90430f2]1802        cy = GetYSize() - cy;
[00a68e0]1803
[429465a]1804        // On-screen, so add to hit-test grid...
[0b0520c]1805        int grid_x = int(cx * HITTEST_SIZE / (GetXSize() + 1));
1806        int grid_y = int(cy * HITTEST_SIZE / (GetYSize() + 1));
[33b2094]1807
[429465a]1808        m_PointGrid[grid_x + grid_y * HITTEST_SIZE].push_back(label);
[33b2094]1809    }
1810
[00a68e0]1811    m_HitTestGridValid = true;
[33b2094]1812}
[c6d95d8]1813
[2a02de2]1814//
[5876fcb]1815//  Methods for controlling the orientation of the survey
[2a02de2]1816//
1817
[5876fcb]1818void GfxCore::TurnCave(Double angle)
[156dc16]1819{
[5876fcb]1820    // Turn the cave around its z-axis by a given angle.
[156dc16]1821
[5876fcb]1822    m_PanAngle += angle;
[5a7597a]1823    // Wrap to range [0, 360):
1824    m_PanAngle = fmod(m_PanAngle, 360.0);
1825    if (m_PanAngle < 0.0) {
[e577f89]1826        m_PanAngle += 360.0;
[156dc16]1827    }
[33b2094]1828
[00a68e0]1829    m_HitTestGridValid = false;
[381ae6e]1830    if (m_here && m_here == &temp_here) SetHere();
[00a68e0]1831
[08253d9]1832    SetRotation(m_PanAngle, m_TiltAngle);
[156dc16]1833}
1834
[5876fcb]1835void GfxCore::TurnCaveTo(Double angle)
[d80805e]1836{
[5455bb2]1837    if (m_Rotating) {
1838        // If we're rotating, jump to the specified angle.
1839        TurnCave(angle - m_PanAngle);
1840        SetPanBase();
1841        return;
1842    }
1843
[3ddd351]1844    int new_switching_to = ((int)angle) / 90 + NORTH;
1845    if (new_switching_to == m_SwitchingTo) {
1846        // A second order to switch takes us there right away
1847        TurnCave(angle - m_PanAngle);
1848        m_SwitchingTo = 0;
1849        ForceRefresh();
1850    } else {
[5455bb2]1851        SetPanBase();
[3ddd351]1852        m_SwitchingTo = new_switching_to;
1853    }
[d80805e]1854}
1855
[5876fcb]1856void GfxCore::TiltCave(Double tilt_angle)
[156dc16]1857{
[5876fcb]1858    // Tilt the cave by a given angle.
[e577f89]1859    if (m_TiltAngle + tilt_angle > 90.0) {
[08253d9]1860        m_TiltAngle = 90.0;
[e577f89]1861    } else if (m_TiltAngle + tilt_angle < -90.0) {
[08253d9]1862        m_TiltAngle = -90.0;
1863    } else {
1864        m_TiltAngle += tilt_angle;
[d80805e]1865    }
1866
[00a68e0]1867    m_HitTestGridValid = false;
[381ae6e]1868    if (m_here && m_here == &temp_here) SetHere();
[00a68e0]1869
[08253d9]1870    SetRotation(m_PanAngle, m_TiltAngle);
[5ffa439]1871}
1872
[5876fcb]1873void GfxCore::TranslateCave(int dx, int dy)
[5ffa439]1874{
[33b2094]1875    AddTranslationScreenCoordinates(dx, dy);
[00a68e0]1876    m_HitTestGridValid = false;
1877
[381ae6e]1878    if (m_here && m_here == &temp_here) SetHere();
[c00c6713]1879
[33b2094]1880    ForceRefresh();
1881}
[5876fcb]1882
[33b2094]1883void GfxCore::DragFinished()
1884{
[76dd228]1885    m_MouseOutsideCompass = m_MouseOutsideElev = false;
[5876fcb]1886    ForceRefresh();
[2173dbd]1887}
1888
[d877aa2]1889void GfxCore::ClearCoords()
1890{
1891    m_Parent->ClearCoords();
1892}
1893
[5876fcb]1894void GfxCore::SetCoords(wxPoint point)
[fd6e0d5]1895{
[0874c07e]1896    // We can't work out 2D coordinates from a perspective view, and it
1897    // doesn't really make sense to show coordinates while we're animating.
1898    if (GetPerspective() || Animating()) return;
[0a811ab]1899
[5876fcb]1900    // Update the coordinate or altitude display, given the (x, y) position in
1901    // window coordinates.  The relevant display is updated depending on
1902    // whether we're in plan or elevation view.
1903
[f6d8375]1904    double cx, cy, cz;
[5876fcb]1905
[a2b3d62]1906    SetDataTransform();
[90430f2]1907    ReverseTransform(point.x, GetYSize() - 1 - point.y, &cx, &cy, &cz);
[5876fcb]1908
[0633bcc]1909    if (ShowingPlan()) {
[d67450e]1910        m_Parent->SetCoords(cx + m_Parent->GetOffset().GetX(),
[381ae6e]1911                            cy + m_Parent->GetOffset().GetY(),
1912                            m_there);
[0633bcc]1913    } else if (ShowingElevation()) {
[381ae6e]1914        m_Parent->SetAltitude(cz + m_Parent->GetOffset().GetZ(),
1915                              m_there);
[d479c15]1916    } else {
[429465a]1917        m_Parent->ClearCoords();
[a2b3d62]1918    }
[fd6e0d5]1919}
1920
[2a26b45]1921int GfxCore::GetCompassWidth() const
1922{
1923    static int result = 0;
1924    if (result == 0) {
1925        result = INDICATOR_BOX_SIZE;
1926        int width;
1927        const wxString & msg = wmsg(/*Facing*/203);
1928        GetTextExtent(msg, &width, NULL);
1929        if (width > result) result = width;
1930    }
1931    return result;
1932}
1933
1934int GfxCore::GetClinoWidth() const
1935{
1936    static int result = 0;
1937    if (result == 0) {
1938        result = INDICATOR_BOX_SIZE;
1939        int width;
1940        const wxString & msg1 = wmsg(/*Plan*/432);
1941        GetTextExtent(msg1, &width, NULL);
1942        if (width > result) result = width;
1943        const wxString & msg2 = wmsg(/*Kiwi Plan*/433);
1944        GetTextExtent(msg2, &width, NULL);
1945        if (width > result) result = width;
1946        const wxString & msg3 = wmsg(/*Elevation*/118);
1947        GetTextExtent(msg3, &width, NULL);
1948        if (width > result) result = width;
1949    }
1950    return result;
1951}
1952
[1eeb55a]1953int GfxCore::GetCompassXPosition() const
[1fd2edb]1954{
[f433fda]1955    // Return the x-coordinate of the centre of the compass in window
1956    // coordinates.
[2a26b45]1957    return GetXSize() - INDICATOR_OFFSET_X - GetCompassWidth() / 2;
[1fd2edb]1958}
1959
[1eeb55a]1960int GfxCore::GetClinoXPosition() const
[1fd2edb]1961{
[f433fda]1962    // Return the x-coordinate of the centre of the compass in window
1963    // coordinates.
[2a26b45]1964    return GetXSize() - GetClinoOffset() - GetClinoWidth() / 2;
[1fd2edb]1965}
1966
[1eeb55a]1967int GfxCore::GetIndicatorYPosition() const
[dfe4454c]1968{
[f433fda]1969    // Return the y-coordinate of the centre of the indicators in window
1970    // coordinates.
[90430f2]1971    return GetYSize() - INDICATOR_OFFSET_Y - INDICATOR_BOX_SIZE / 2;
[5876fcb]1972}
[fa42426]1973
[1eeb55a]1974int GfxCore::GetIndicatorRadius() const
[5876fcb]1975{
1976    // Return the radius of each indicator.
[1eeb55a]1977    return (INDICATOR_BOX_SIZE - INDICATOR_MARGIN * 2) / 2;
[5876fcb]1978}
[dfe4454c]1979
[14acdae]1980bool GfxCore::PointWithinCompass(wxPoint point) const
[5876fcb]1981{
[f433fda]1982    // Determine whether a point (in window coordinates) lies within the
1983    // compass.
[e2c1671]1984    if (!ShowingCompass()) return false;
1985
[33b2094]1986    glaCoord dx = point.x - GetCompassXPosition();
1987    glaCoord dy = point.y - GetIndicatorYPosition();
1988    glaCoord radius = GetIndicatorRadius();
[f433fda]1989
[5876fcb]1990    return (dx * dx + dy * dy <= radius * radius);
1991}
[fa42426]1992
[14acdae]1993bool GfxCore::PointWithinClino(wxPoint point) const
[5876fcb]1994{
1995    // Determine whether a point (in window coordinates) lies within the clino.
[e2c1671]1996    if (!ShowingClino()) return false;
1997
[33b2094]1998    glaCoord dx = point.x - GetClinoXPosition();
1999    glaCoord dy = point.y - GetIndicatorYPosition();
2000    glaCoord radius = GetIndicatorRadius();
[f433fda]2001
[5876fcb]2002    return (dx * dx + dy * dy <= radius * radius);
[dfe4454c]2003}
[8000d8f]2004
[14acdae]2005bool GfxCore::PointWithinScaleBar(wxPoint point) const
[5876fcb]2006{
[e2c1671]2007    // Determine whether a point (in window coordinates) lies within the scale
2008    // bar.
2009    if (!ShowingScaleBar()) return false;
[8000d8f]2010
[e2c1671]2011    return (point.x >= SCALE_BAR_OFFSET_X &&
2012            point.x <= SCALE_BAR_OFFSET_X + m_ScaleBarWidth &&
[90430f2]2013            point.y <= GetYSize() - SCALE_BAR_OFFSET_Y - SCALE_BAR_HEIGHT &&
2014            point.y >= GetYSize() - SCALE_BAR_OFFSET_Y - SCALE_BAR_HEIGHT*2);
[5876fcb]2015}
[8000d8f]2016
[d43fa84]2017bool GfxCore::PointWithinColourKey(wxPoint point) const
2018{
2019    // Determine whether a point (in window coordinates) lies within the key.
[62da267]2020    point.x -= GetXSize() - KEY_OFFSET_X;
2021    point.y = KEY_OFFSET_Y - point.y;
[fcc3741]2022    return (point.x >= key_lowerleft[m_ColourBy].x && point.x <= 0 &&
2023            point.y >= key_lowerleft[m_ColourBy].y && point.y <= 0);
[d43fa84]2024}
2025
[5876fcb]2026void GfxCore::SetCompassFromPoint(wxPoint point)
[8000d8f]2027{
[d877aa2]2028    // Given a point in window coordinates, set the heading of the survey.  If
2029    // the point is outside the compass, it snaps to 45 degree intervals;
2030    // otherwise it operates as normal.
[8000d8f]2031
[5876fcb]2032    wxCoord dx = point.x - GetCompassXPosition();
2033    wxCoord dy = point.y - GetIndicatorYPosition();
2034    wxCoord radius = GetIndicatorRadius();
[7aa15c0]2035
[0580c6a]2036    double angle = deg(atan2(double(dx), double(dy))) - 180.0;
[5876fcb]2037    if (dx * dx + dy * dy <= radius * radius) {
[3ddd351]2038        TurnCave(angle - m_PanAngle);
[429465a]2039        m_MouseOutsideCompass = false;
[e577f89]2040    } else {
[3ddd351]2041        TurnCave(int(angle / 45.0) * 45.0 - m_PanAngle);
[429465a]2042        m_MouseOutsideCompass = true;
[7aa15c0]2043    }
[8000d8f]2044
[5876fcb]2045    ForceRefresh();
2046}
2047
2048void GfxCore::SetClinoFromPoint(wxPoint point)
2049{
[d877aa2]2050    // Given a point in window coordinates, set the elevation of the survey.
2051    // If the point is outside the clino, it snaps to 90 degree intervals;
2052    // otherwise it operates as normal.
[8000d8f]2053
[33b2094]2054    glaCoord dx = point.x - GetClinoXPosition();
2055    glaCoord dy = point.y - GetIndicatorYPosition();
2056    glaCoord radius = GetIndicatorRadius();
[f433fda]2057
[5876fcb]2058    if (dx >= 0 && dx * dx + dy * dy <= radius * radius) {
[7a57dc7]2059        TiltCave(-deg(atan2(double(dy), double(dx))) - m_TiltAngle);
[429465a]2060        m_MouseOutsideElev = false;
[e577f89]2061    } else if (dy >= INDICATOR_MARGIN) {
[7a57dc7]2062        TiltCave(-90.0 - m_TiltAngle);
[429465a]2063        m_MouseOutsideElev = true;
[e577f89]2064    } else if (dy <= -INDICATOR_MARGIN) {
[7a57dc7]2065        TiltCave(90.0 - m_TiltAngle);
[429465a]2066        m_MouseOutsideElev = true;
[e577f89]2067    } else {
[429465a]2068        TiltCave(-m_TiltAngle);
2069        m_MouseOutsideElev = true;
[5876fcb]2070    }
[8000d8f]2071
[5876fcb]2072    ForceRefresh();
[8000d8f]2073}
2074
[5876fcb]2075void GfxCore::SetScaleBarFromOffset(wxCoord dx)
[8000d8f]2076{
[5876fcb]2077    // Set the scale of the survey, given an offset as to how much the mouse has
2078    // been dragged over the scalebar since the last scale change.
[8000d8f]2079
[5b7164d]2080    SetScale((m_ScaleBarWidth + dx) * m_Scale / m_ScaleBarWidth);
[5876fcb]2081    ForceRefresh();
2082}
[8000d8f]2083
[5876fcb]2084void GfxCore::RedrawIndicators()
2085{
2086    // Redraw the compass and clino indicators.
[8000d8f]2087
[2a26b45]2088    int total_width = GetCompassWidth() + INDICATOR_GAP + GetClinoWidth();
2089    RefreshRect(wxRect(GetXSize() - INDICATOR_OFFSET_X - total_width,
[e24b7fb]2090                       GetYSize() - INDICATOR_OFFSET_Y - INDICATOR_BOX_SIZE,
[2a26b45]2091                       total_width,
[e24b7fb]2092                       INDICATOR_BOX_SIZE), false);
[8000d8f]2093}
2094
[5876fcb]2095void GfxCore::StartRotation()
[8000d8f]2096{
[5876fcb]2097    // Start the survey rotating.
[f433fda]2098
[5455bb2]2099    if (m_SwitchingTo >= NORTH)
2100        m_SwitchingTo = 0;
[5876fcb]2101    m_Rotating = true;
[5455bb2]2102    SetPanBase();
[5876fcb]2103}
[8000d8f]2104
[5876fcb]2105void GfxCore::ToggleRotation()
2106{
2107    // Toggle the survey rotation on/off.
[f433fda]2108
[5876fcb]2109    if (m_Rotating) {
[2a3d328]2110        StopRotation();
2111    } else {
2112        StartRotation();
[5876fcb]2113    }
2114}
[8000d8f]2115
[5876fcb]2116void GfxCore::StopRotation()
2117{
2118    // Stop the survey rotating.
[8000d8f]2119
[5876fcb]2120    m_Rotating = false;
[33b2094]2121    ForceRefresh();
[5876fcb]2122}
[8000d8f]2123
[eef68f9]2124bool GfxCore::IsExtendedElevation() const
[5876fcb]2125{
[eef68f9]2126    return m_Parent->IsExtendedElevation();
[5876fcb]2127}
[8000d8f]2128
[5876fcb]2129void GfxCore::ReverseRotation()
2130{
2131    // Reverse the direction of rotation.
[8000d8f]2132
[5876fcb]2133    m_RotationStep = -m_RotationStep;
[5455bb2]2134    if (m_Rotating)
2135        SetPanBase();
[5876fcb]2136}
[8000d8f]2137
[5876fcb]2138void GfxCore::RotateSlower(bool accel)
2139{
2140    // Decrease the speed of rotation, optionally by an increased amount.
[5455bb2]2141    if (fabs(m_RotationStep) == 1.0)
2142        return;
[8000d8f]2143
[5455bb2]2144    m_RotationStep *= accel ? (1 / 1.44) : (1 / 1.2);
2145
2146    if (fabs(m_RotationStep) < 1.0) {
2147        m_RotationStep = (m_RotationStep > 0 ? 1.0 : -1.0);
[1690fa9]2148    }
[5455bb2]2149    if (m_Rotating)
2150        SetPanBase();
[8000d8f]2151}
2152
[5876fcb]2153void GfxCore::RotateFaster(bool accel)
2154{
2155    // Increase the speed of rotation, optionally by an increased amount.
[5455bb2]2156    if (fabs(m_RotationStep) == 180.0)
2157        return;
[5876fcb]2158
2159    m_RotationStep *= accel ? 1.44 : 1.2;
[5455bb2]2160    if (fabs(m_RotationStep) > 180.0) {
2161        m_RotationStep = (m_RotationStep > 0 ? 180.0 : -180.0);
[1690fa9]2162    }
[5455bb2]2163    if (m_Rotating)
2164        SetPanBase();
[5876fcb]2165}
[8000d8f]2166
[5876fcb]2167void GfxCore::SwitchToElevation()
[8000d8f]2168{
[5876fcb]2169    // Perform an animated switch to elevation view.
[8000d8f]2170
[5455bb2]2171    if (m_SwitchingTo != ELEVATION) {
2172        SetTiltBase();
2173        m_SwitchingTo = ELEVATION;
2174    } else {
2175        // A second order to switch takes us there right away
2176        TiltCave(-m_TiltAngle);
2177        m_SwitchingTo = 0;
2178        ForceRefresh();
[5876fcb]2179    }
[8000d8f]2180}
2181
[5876fcb]2182void GfxCore::SwitchToPlan()
[8000d8f]2183{
[5876fcb]2184    // Perform an animated switch to plan view.
[8000d8f]2185
[5455bb2]2186    if (m_SwitchingTo != PLAN) {
2187        SetTiltBase();
2188        m_SwitchingTo = PLAN;
2189    } else {
2190        // A second order to switch takes us there right away
2191        TiltCave(-90.0 - m_TiltAngle);
2192        m_SwitchingTo = 0;
2193        ForceRefresh();
[8000d8f]2194    }
[5876fcb]2195}
[8000d8f]2196
[d1628e8e]2197void GfxCore::SetViewTo(Double xmin, Double xmax, Double ymin, Double ymax, Double zmin, Double zmax)
2198{
2199
2200    SetTranslation(-Vector3((xmin + xmax) / 2, (ymin + ymax) / 2, (zmin + zmax) / 2));
[d0f5918]2201    Double scale = HUGE_VAL;
[d1628e8e]2202    const Vector3 ext = m_Parent->GetExtent();
2203    if (xmax > xmin) {
2204        Double s = ext.GetX() / (xmax - xmin);
2205        if (s < scale) scale = s;
2206    }
2207    if (ymax > ymin) {
2208        Double s = ext.GetY() / (ymax - ymin);
2209        if (s < scale) scale = s;
2210    }
2211    if (!ShowingPlan() && zmax > zmin) {
2212        Double s = ext.GetZ() / (zmax - zmin);
2213        if (s < scale) scale = s;
2214    }
[d0f5918]2215    if (scale != HUGE_VAL) SetScale(scale);
[d1628e8e]2216    ForceRefresh();
2217}
2218
[14acdae]2219bool GfxCore::CanRaiseViewpoint() const
[5876fcb]2220{
2221    // Determine if the survey can be viewed from a higher angle of elevation.
[f433fda]2222
[7a57dc7]2223    return GetPerspective() ? (m_TiltAngle < 90.0) : (m_TiltAngle > -90.0);
[8000d8f]2224}
2225
[14acdae]2226bool GfxCore::CanLowerViewpoint() const
[2effbf1]2227{
[5876fcb]2228    // Determine if the survey can be viewed from a lower angle of elevation.
[2effbf1]2229
[7a57dc7]2230    return GetPerspective() ? (m_TiltAngle > -90.0) : (m_TiltAngle < 90.0);
[5876fcb]2231}
[2effbf1]2232
[78c67a6]2233bool GfxCore::HasDepth() const
[bd21214]2234{
[78c67a6]2235    return m_Parent->GetDepthExtent() == 0.0;
[bd21214]2236}
2237
[843ee7b]2238bool GfxCore::HasErrorInformation() const
[d4650b3]2239{
[843ee7b]2240    return m_Parent->HasErrorInformation();
[d4650b3]2241}
2242
[843ee7b]2243bool GfxCore::HasDateInformation() const
[c61aa79]2244{
[843ee7b]2245    return m_Parent->GetDateMin() >= 0;
[c61aa79]2246}
2247
[14acdae]2248bool GfxCore::ShowingPlan() const
[5876fcb]2249{
2250    // Determine if the survey is in plan view.
[f433fda]2251
[7a57dc7]2252    return (m_TiltAngle == -90.0);
[2effbf1]2253}
2254
[14acdae]2255bool GfxCore::ShowingElevation() const
[8000d8f]2256{
[5876fcb]2257    // Determine if the survey is in elevation view.
[f433fda]2258
[5876fcb]2259    return (m_TiltAngle == 0.0);
[8000d8f]2260}
2261
[14acdae]2262bool GfxCore::ShowingMeasuringLine() const
[8000d8f]2263{
[0afefe6]2264    // Determine if the measuring line is being shown.  Only check if "there"
2265    // is valid, since that means the measuring line anchor is out.
[f433fda]2266
[381ae6e]2267    return m_there;
[8000d8f]2268}
2269
[eff44b9]2270void GfxCore::ToggleFlag(bool* flag, int update)
[5876fcb]2271{
2272    *flag = !*flag;
[6747314]2273    if (update == UPDATE_BLOBS) {
[eff44b9]2274        UpdateBlobs();
[ef1870d]2275    } else if (update == UPDATE_BLOBS_AND_CROSSES) {
2276        UpdateBlobs();
2277        InvalidateList(LIST_CROSSES);
[de43be7]2278        m_HitTestGridValid = false;
[6cd6bbe]2279    }
[eff44b9]2280    ForceRefresh();
[5876fcb]2281}
[93744a5]2282
[14acdae]2283int GfxCore::GetNumEntrances() const
[4b1fc48]2284{
[5876fcb]2285    return m_Parent->GetNumEntrances();
2286}
[bd7a61b]2287
[14acdae]2288int GfxCore::GetNumFixedPts() const
[5876fcb]2289{
2290    return m_Parent->GetNumFixedPts();
2291}
[7757a4ed]2292
[14acdae]2293int GfxCore::GetNumExportedPts() const
[5876fcb]2294{
2295    return m_Parent->GetNumExportedPts();
2296}
2297
[622460e]2298void GfxCore::ToggleTerrain()
2299{
2300    ToggleFlag(&m_Terrain);
2301    if (m_Terrain && !dem) {
2302        wxCommandEvent dummy;
2303        m_Parent->OnOpenTerrain(dummy);
2304    }
2305}
2306
[d96c95c]2307void GfxCore::ToggleFatFinger()
2308{
2309    if (sqrd_measure_threshold == sqrd(MEASURE_THRESHOLD)) {
2310        sqrd_measure_threshold = sqrd(5 * MEASURE_THRESHOLD);
[7b9b700]2311        wxMessageBox(wxT("Fat finger enabled"), wxT("Aven Debug"), wxOK | wxICON_INFORMATION);
[d96c95c]2312    } else {
2313        sqrd_measure_threshold = sqrd(MEASURE_THRESHOLD);
[7b9b700]2314        wxMessageBox(wxT("Fat finger disabled"), wxT("Aven Debug"), wxOK | wxICON_INFORMATION);
[d96c95c]2315    }
2316}
2317
[5876fcb]2318void GfxCore::ClearTreeSelection()
2319{
2320    m_Parent->ClearTreeSelection();
2321}
2322
[82c3731]2323void GfxCore::CentreOn(const Point &p)
[5876fcb]2324{
[d67450e]2325    SetTranslation(-p);
[00a68e0]2326    m_HitTestGridValid = false;
[f433fda]2327
[5876fcb]2328    ForceRefresh();
[4b1fc48]2329}
[5876fcb]2330
[33b2094]2331void GfxCore::ForceRefresh()
2332{
2333    Refresh(false);
2334}
2335
[d2fcc9b]2336void GfxCore::GenerateList(unsigned int l)
[33b2094]2337{
[9eb58d0]2338    assert(m_HaveData);
[3ddcad8]2339
[d2fcc9b]2340    switch (l) {
[fe665c4]2341        case LIST_COMPASS:
2342            DrawCompass();
2343            break;
2344        case LIST_CLINO:
2345            DrawClino();
2346            break;
2347        case LIST_CLINO_BACK:
2348            DrawClinoBack();
2349            break;
[9c37beb]2350        case LIST_SCALE_BAR:
2351            DrawScaleBar();
2352            break;
[e2ea75a]2353        case LIST_DEPTH_KEY:
2354            DrawDepthKey();
[252d759]2355            break;
[e2ea75a]2356        case LIST_DATE_KEY:
2357            DrawDateKey();
[d4650b3]2358            break;
[e2ea75a]2359        case LIST_ERROR_KEY:
2360            DrawErrorKey();
[c61aa79]2361            break;
[cc9e2c65]2362        case LIST_GRADIENT_KEY:
2363            DrawGradientKey();
2364            break;
[af50685]2365        case LIST_LENGTH_KEY:
2366            DrawLengthKey();
2367            break;
[d2fcc9b]2368        case LIST_UNDERGROUND_LEGS:
[b96edeb]2369            GenerateDisplayList(0);
[d2fcc9b]2370            break;
2371        case LIST_TUBES:
2372            GenerateDisplayListTubes();
2373            break;
2374        case LIST_SURFACE_LEGS:
[b96edeb]2375            GenerateDisplayList(true);
[d2fcc9b]2376            break;
[86fe6e4]2377        case LIST_BLOBS:
2378            GenerateBlobsDisplayList();
[37d7084]2379            break;
[86fe6e4]2380        case LIST_CROSSES: {
2381            BeginCrosses();
2382            SetColour(col_LIGHT_GREY);
2383            list<LabelInfo*>::const_iterator pos = m_Parent->GetLabels();
2384            while (pos != m_Parent->GetLabelsEnd()) {
2385                const LabelInfo* label = *pos++;
2386
2387                if ((m_Surface && label->IsSurface()) ||
2388                    (m_Legs && label->IsUnderground()) ||
2389                    (!label->IsSurface() && !label->IsUnderground())) {
2390                    // Check if this station should be displayed
2391                    // (last case is for stns with no legs attached)
2392                    DrawCross(label->GetX(), label->GetY(), label->GetZ());
2393                }
2394            }
2395            EndCrosses();
2396            break;
2397        }
[37d7084]2398        case LIST_GRID:
2399            DrawGrid();
[d2fcc9b]2400            break;
[86fe6e4]2401        case LIST_SHADOW:
2402            GenerateDisplayListShadow();
[d2fcc9b]2403            break;
[6388423]2404        case LIST_TERRAIN:
2405            DrawTerrain();
[22b0a8f]2406            break;
[d2fcc9b]2407        default:
2408            assert(false);
2409            break;
2410    }
2411}
2412
[d67450e]2413void GfxCore::ToggleSmoothShading()
2414{
2415    GLACanvas::ToggleSmoothShading();
2416    InvalidateList(LIST_TUBES);
2417    ForceRefresh();
2418}
2419
[b96edeb]2420void GfxCore::GenerateDisplayList(bool surface)
[d2fcc9b]2421{
[b96edeb]2422    unsigned surf_or_not = surface ? img_FLAG_SURFACE : 0;
2423    // Generate the display list for the surface or underground legs.
2424    for (int f = 0; f != 8; ++f) {
2425        if ((f & img_FLAG_SURFACE) != surf_or_not) continue;
2426        const unsigned SHOW_DASHED_AND_FADED = unsigned(-1);
2427        unsigned style = SHOW_NORMAL;
2428        if ((f & img_FLAG_SPLAY) && m_Splays != SHOW_NORMAL) {
2429            style = m_Splays;
2430        } else if (f & img_FLAG_DUPLICATE) {
2431            style = m_Dupes;
2432        }
2433        if (f & img_FLAG_SURFACE) {
2434            if (style == SHOW_FADED) {
2435                style = SHOW_DASHED_AND_FADED;
2436            } else {
2437                style = SHOW_DASHED;
2438            }
2439        }
[8666fc7]2440
[b96edeb]2441        switch (style) {
2442            case SHOW_HIDE:
2443                continue;
2444            case SHOW_FADED:
2445                SetAlpha(0.4);
2446                break;
2447            case SHOW_DASHED:
2448                EnableDashedLines();
2449                break;
2450            case SHOW_DASHED_AND_FADED:
2451                SetAlpha(0.4);
2452                EnableDashedLines();
2453                break;
[ad661cc]2454        }
[8666fc7]2455
[b96edeb]2456        void (GfxCore::* add_poly)(const traverse&);
2457        if (surface) {
2458            if (m_ColourBy == COLOUR_BY_ERROR) {
2459                add_poly = &GfxCore::AddPolylineError;
2460            } else {
2461                add_poly = &GfxCore::AddPolyline;
2462            }
2463        } else {
2464            add_poly = AddPoly;
2465        }
2466
2467        list<traverse>::const_iterator trav = m_Parent->traverses_begin(f);
2468        list<traverse>::const_iterator tend = m_Parent->traverses_end(f);
[e5c5f3c]2469        while (trav != tend) {
[b96edeb]2470            (this->*add_poly)(*trav);
[e5c5f3c]2471            ++trav;
2472        }
2473
[b96edeb]2474        switch (style) {
2475            case SHOW_FADED:
2476                SetAlpha(1.0);
2477                break;
2478            case SHOW_DASHED:
2479                DisableDashedLines();
2480                break;
2481            case SHOW_DASHED_AND_FADED:
2482                DisableDashedLines();
2483                SetAlpha(1.0);
2484                break;
2485        }
[3ddcad8]2486    }
[33b2094]2487}
2488
[9eb58d0]2489void GfxCore::GenerateDisplayListTubes()
[33b2094]2490{
[9eb58d0]2491    // Generate the display list for the tubes.
[fc68ad5]2492    list<vector<XSect> >::iterator trav = m_Parent->tubes_begin();
2493    list<vector<XSect> >::iterator tend = m_Parent->tubes_end();
[3ddcad8]2494    while (trav != tend) {
2495        SkinPassage(*trav);
2496        ++trav;
2497    }
[9eb58d0]2498}
[33b2094]2499
[37d7084]2500void GfxCore::GenerateDisplayListShadow()
[f4c5932]2501{
2502    SetColour(col_BLACK);
[b96edeb]2503    for (int f = 0; f != 8; ++f) {
2504        // Only include underground legs in the shadow.
2505        if ((f & img_FLAG_SURFACE) != 0) continue;
2506        list<traverse>::const_iterator trav = m_Parent->traverses_begin(f);
2507        list<traverse>::const_iterator tend = m_Parent->traverses_end(f);
2508        while (trav != tend) {
2509            AddPolylineShadow(*trav);
2510            ++trav;
2511        }
[f4c5932]2512    }
2513}
2514
[112f80c]2515void
2516GfxCore::parse_hgt_filename(const wxString & lc_name)
2517{
[8562abc]2518    char * leaf = leaf_from_fnm(lc_name.utf8_str());
[112f80c]2519    const char * p = leaf;
2520    char * q;
2521    char dirn = *p++;
2522    o_y = strtoul(p, &q, 10);
2523    p = q;
2524    if (dirn == 's')
2525        o_y = -o_y;
2526    ++o_y;
2527    dirn = *p++;
2528    o_x = strtoul(p, &q, 10);
2529    if (dirn == 'w')
2530        o_x = -o_x;
2531    bigendian = true;
2532    nodata_value = -32768;
2533    osfree(leaf);
2534}
2535
2536size_t
2537GfxCore::parse_hdr(wxInputStream & is, unsigned long & skipbytes)
2538{
[9260793]2539    // ESRI docs say NBITS defaults to 8.
2540    unsigned long nbits = 8;
2541    // ESRI docs say NBANDS defaults to 1.
2542    unsigned long nbands = 1;
2543    unsigned long bandrowbytes = 0;
2544    unsigned long totalrowbytes = 0;
2545    // ESRI docs say ULXMAP defaults to 0.
2546    o_x = 0.0;
2547    // ESRI docs say ULYMAP defaults to NROWS - 1.
2548    o_y = HUGE_VAL;
2549    // ESRI docs say XDIM and YDIM default to 1.
2550    step_x = step_y = 1.0;
[112f80c]2551    while (!is.Eof()) {
2552        wxString line;
2553        int ch;
2554        while ((ch = is.GetC()) != wxEOF) {
2555            if (ch == '\n' || ch == '\r') break;
2556            line += wxChar(ch);
2557        }
[6388423]2558#define CHECK(X, COND) \
[1c55fb5]2559} else if (line.StartsWith(wxT(X " "))) { \
[6388423]2560size_t v = line.find_first_not_of(wxT(' '), sizeof(X)); \
2561if (v == line.npos || !(COND)) { \
[1c55fb5]2562err += wxT("Unexpected value for " X); \
[5314a0e]2563}
[112f80c]2564        wxString err;
2565        if (false) {
2566        // I = little-endian; M = big-endian
2567        CHECK("BYTEORDER", (bigendian = (line[v] == 'M')) || line[v] == 'I')
[9260793]2568        // ESRI docs say LAYOUT defaults to BIL if not specified.
[112f80c]2569        CHECK("LAYOUT", line.substr(v) == wxT("BIL"))
[1a381ae]2570        CHECK("NROWS", line.substr(v).ToCULong(&dem_height))
2571        CHECK("NCOLS", line.substr(v).ToCULong(&dem_width))
[9260793]2572        // ESRI docs say NBANDS defaults to 1 if not specified.
2573        CHECK("NBANDS", line.substr(v).ToCULong(&nbands) && nbands == 1)
[112f80c]2574        CHECK("NBITS", line.substr(v).ToCULong(&nbits) && nbits == 16)
[9260793]2575        CHECK("BANDROWBYTES", line.substr(v).ToCULong(&bandrowbytes))
2576        CHECK("TOTALROWBYTES", line.substr(v).ToCULong(&totalrowbytes))
[112f80c]2577        // PIXELTYPE is a GDAL extension, so may not be present.
2578        CHECK("PIXELTYPE", line.substr(v) == wxT("SIGNEDINT"))
2579        CHECK("ULXMAP", line.substr(v).ToCDouble(&o_x))
2580        CHECK("ULYMAP", line.substr(v).ToCDouble(&o_y))
2581        CHECK("XDIM", line.substr(v).ToCDouble(&step_x))
2582        CHECK("YDIM", line.substr(v).ToCDouble(&step_y))
2583        CHECK("NODATA", line.substr(v).ToCLong(&nodata_value))
2584        CHECK("SKIPBYTES", line.substr(v).ToCULong(&skipbytes))
2585        }
2586        if (!err.empty()) {
2587            wxMessageBox(err);
[6388423]2588        }
[c2fa50c]2589    }
[9260793]2590    if (o_y == HUGE_VAL) {
2591        o_y = dem_height - 1;
[0456466]2592    }
[9260793]2593    if (bandrowbytes != 0) {
2594        if (nbits * dem_width != bandrowbytes * 8) {
2595            wxMessageBox("BANDROWBYTES setting indicates unused bits after each band - not currently supported");
2596        }
2597    }
2598    if (totalrowbytes != 0) {
2599        // This is the ESRI default for BIL, for BIP it would be
2600        // nbands * bandrowbytes.
2601        if (nbands * nbits * dem_width != totalrowbytes * 8) {
2602            wxMessageBox("TOTALROWBYTES setting indicates unused bits after "
2603                         "each row - not currently supported");
2604        }
2605    }
2606    return ((nbits * dem_width + 7) / 8) * dem_height;
[112f80c]2607}
[c2fa50c]2608
[112f80c]2609bool
2610GfxCore::read_bil(wxInputStream & is, size_t size, unsigned long skipbytes)
2611{
2612    bool know_size = true;
2613    if (!size) {
2614        // If the stream doesn't know its size, GetSize() returns 0.
2615        size = is.GetSize();
2616        if (!size) {
2617            size = DEFAULT_HGT_SIZE;
2618            know_size = false;
2619        }
[c2fa50c]2620    }
2621    dem = new unsigned short[size / 2];
2622    if (skipbytes) {
[112f80c]2623        if (is.SeekI(skipbytes, wxFromStart) == ::wxInvalidOffset) {
[c2fa50c]2624            while (skipbytes) {
2625                unsigned long to_read = skipbytes;
2626                if (size < to_read) to_read = size;
[112f80c]2627                is.Read(reinterpret_cast<char *>(dem), to_read);
2628                size_t c = is.LastRead();
[c2fa50c]2629                if (c == 0) {
2630                    wxMessageBox(wxT("Failed to skip terrain data header"));
2631                    break;
[bfb3ab0]2632                }
[c2fa50c]2633                skipbytes -= c;
[bfb3ab0]2634            }
[5314a0e]2635        }
[c2fa50c]2636    }
[bfb3ab0]2637
[6388423]2638#if wxCHECK_VERSION(2,9,5)
[112f80c]2639    if (!is.ReadAll(dem, size)) {
2640        if (know_size) {
2641            // FIXME: On __WXMSW__ currently we fail to
2642            // read any data from files in zips.
2643            delete [] dem;
2644            dem = NULL;
2645            wxMessageBox(wxT("Failed to read terrain data"));
2646            return false;
[c2fa50c]2647        }
[112f80c]2648        size = is.LastRead();
[c2fa50c]2649    }
2650#else
2651    char * p = reinterpret_cast<char *>(dem);
2652    while (size) {
[112f80c]2653        is.Read(p, size);
2654        size_t c = is.LastRead();
[c2fa50c]2655        if (c == 0) {
[112f80c]2656            if (!know_size) {
[c2fa50c]2657                size = DEFAULT_HGT_SIZE - size;
[112f80c]2658                if (size)
[c2fa50c]2659                    break;
[9df33bc]2660            }
[112f80c]2661            delete [] dem;
2662            dem = NULL;
[5314a0e]2663            wxMessageBox(wxT("Failed to read terrain data"));
[f60efe4]2664            return false;
[6388423]2665        }
[c2fa50c]2666        p += c;
2667        size -= c;
2668    }
[5314a0e]2669#endif
[112f80c]2670
2671    if (dem_width == 0 && dem_height == 0) {
2672        dem_width = dem_height = sqrt(size / 2);
2673        if (dem_width * dem_height * 2 != size) {
2674            delete [] dem;
2675            dem = NULL;
2676            wxMessageBox(wxT("HGT format data doesn't form a square"));
2677            return false;
2678        }
2679        step_x = step_y = 1.0 / dem_width;
2680    }
2681
2682    return true;
2683}
2684
2685bool GfxCore::LoadDEM(const wxString & file)
2686{
2687    if (m_Parent->m_cs_proj.empty()) {
2688        wxMessageBox(wxT("No coordinate system specified in survey data"));
2689        return false;
2690    }
2691
2692    delete [] dem;
2693    dem = NULL;
2694
2695    size_t size = 0;
2696    // Default is to not skip any bytes.
2697    unsigned long skipbytes = 0;
2698    // For .hgt files, default to using filesize to determine.
2699    dem_width = dem_height = 0;
2700    // ESRI say "The default byte order is the same as that of the host machine
2701    // executing the software", but that's stupid so we default to
2702    // little-endian.
2703    bigendian = false;
2704
[522fb4c]2705    wxFileInputStream fs(file);
2706    if (!fs.IsOk()) {
[112f80c]2707        wxMessageBox(wxT("Failed to open DEM file"));
2708        return false;
[6388423]2709    }
[c2fa50c]2710
[112f80c]2711    const wxString & lc_file = file.Lower();
2712    if (lc_file.EndsWith(wxT(".hgt"))) {
2713        parse_hgt_filename(lc_file);
2714        read_bil(fs, size, skipbytes);
2715    } else if (lc_file.EndsWith(wxT(".bil"))) {
2716        wxString hdr_file = file;
2717        hdr_file.replace(file.size() - 4, 4, wxT(".hdr"));
[522fb4c]2718        wxFileInputStream hdr_is(hdr_file);
2719        if (!hdr_is.IsOk()) {
[112f80c]2720            wxMessageBox(wxT("Failed to open HDR file '") + hdr_file + wxT("'"));
2721            return false;
2722        }
2723        size = parse_hdr(hdr_is, skipbytes);
2724        read_bil(fs, size, skipbytes);
2725    } else if (lc_file.EndsWith(wxT(".zip"))) {
2726        wxZipEntry * ze_data = NULL;
2727        wxZipInputStream zs(fs);
2728        wxZipEntry * ze;
2729        while ((ze = zs.GetNextEntry()) != NULL) {
2730            if (!ze->IsDir()) {
2731                const wxString & lc_name = ze->GetName().Lower();
2732                if (!ze_data && lc_name.EndsWith(wxT(".hgt"))) {
2733                    // SRTM .hgt files are raw binary data, with the filename
2734                    // encoding the coordinates.
2735                    parse_hgt_filename(lc_name);
2736                    read_bil(zs, size, skipbytes);
2737                    delete ze;
2738                    break;
2739                }
2740
2741                if (!ze_data && lc_name.EndsWith(wxT(".bil"))) {
2742                    if (size) {
2743                        read_bil(zs, size, skipbytes);
2744                        break;
2745                    }
2746                    ze_data = ze;
2747                    continue;
2748                }
2749
2750                if (lc_name.EndsWith(wxT(".hdr"))) {
2751                    size = parse_hdr(zs, skipbytes);
2752                    if (ze_data) {
2753                        if (!zs.OpenEntry(*ze_data)) {
2754                            wxMessageBox(wxT("Couldn't read DEM data from .zip file"));
2755                            break;
2756                        }
2757                        read_bil(zs, size, skipbytes);
2758                    }
2759                } else if (lc_name.EndsWith(wxT(".prj"))) {
2760                    //FIXME: check this matches the datum string we use
2761                    //Projection    GEOGRAPHIC
2762                    //Datum         WGS84
2763                    //Zunits        METERS
2764                    //Units         DD
2765                    //Spheroid      WGS84
2766                    //Xshift        0.0000000000
2767                    //Yshift        0.0000000000
2768                    //Parameters
2769                }
2770            }
2771            delete ze;
2772        }
2773        delete ze_data;
2774    }
2775
2776    if (!dem) {
2777        return false;
2778    }
[622460e]2779
2780    InvalidateList(LIST_TERRAIN);
[74869a7]2781    ForceRefresh();
[5314a0e]2782    return true;
2783}
2784
2785void GfxCore::DrawTerrainTriangle(const Vector3 & a, const Vector3 & b, const Vector3 & c)
2786{
2787    Vector3 n = (b - a) * (c - a);
2788    n.normalise();
2789    Double factor = dot(n, light) * .95 + .05;
2790    SetColour(col_WHITE, factor);
2791    PlaceVertex(a);
2792    PlaceVertex(b);
2793    PlaceVertex(c);
2794    ++n_tris;
2795}
2796
[7685ae3]2797// Like wxBusyCursor, but you can cancel it early.
2798class AvenBusyCursor {
2799    bool active;
2800
2801  public:
2802    AvenBusyCursor() : active(true) {
2803        wxBeginBusyCursor();
2804    }
2805
2806    void stop() {
2807        if (active) {
2808            active = false;
2809            wxEndBusyCursor();
2810        }
2811    }
2812
2813    ~AvenBusyCursor() {
2814        stop();
2815    }
2816};
2817
[5314a0e]2818void GfxCore::DrawTerrain()
2819{
[622460e]2820    if (!dem) return;
2821
[7685ae3]2822    AvenBusyCursor hourglass;
[c26455e]2823
[6388423]2824    // Draw terrain to twice the extent, or at least 1km.
2825    double r_sqrd = sqrd(max(m_Parent->GetExtent().magnitude(), 1000.0));
2826#define WGS84_DATUM_STRING "+proj=longlat +ellps=WGS84 +datum=WGS84"
2827    static projPJ pj_in = pj_init_plus(WGS84_DATUM_STRING);
2828    if (!pj_in) {
[9df33bc]2829        ToggleTerrain();
[7685ae3]2830        delete [] dem;
2831        dem = NULL;
2832        hourglass.stop();
[9df33bc]2833        error(/*Failed to initialise input coordinate system “%s”*/287, WGS84_DATUM_STRING);
2834        return;
[6388423]2835    }
2836    static projPJ pj_out = pj_init_plus(m_Parent->m_cs_proj.c_str());
2837    if (!pj_out) {
[9df33bc]2838        ToggleTerrain();
[7685ae3]2839        delete [] dem;
2840        dem = NULL;
2841        hourglass.stop();
[9df33bc]2842        error(/*Failed to initialise output coordinate system “%s”*/288, (const char *)m_Parent->m_cs_proj.c_str());
2843        return;
[6388423]2844    }
2845    n_tris = 0;
2846    SetAlpha(0.3);
2847    BeginTriangles();
2848    const Vector3 & off = m_Parent->GetOffset();
[5314a0e]2849    vector<Vector3> prevcol(dem_height + 1);
2850    for (size_t x = 0; x < dem_width; ++x) {
[6388423]2851        double X_ = (o_x + x * step_x) * DEG_TO_RAD;
2852        Vector3 prev;
[5314a0e]2853        for (size_t y = 0; y < dem_height; ++y) {
[9df33bc]2854            unsigned short elev = dem[x + y * dem_width];
[194503c]2855#ifdef WORDS_BIGENDIAN
[9df33bc]2856            const bool MACHINE_BIGENDIAN = true;
2857#else
2858            const bool MACHINE_BIGENDIAN = false;
2859#endif
2860            if (bigendian != MACHINE_BIGENDIAN) {
2861#if defined __GNUC__ && (__GNUC__ * 100 + __GNUC_MINOR__ >= 408)
2862                elev = __builtin_bswap16(elev);
2863#else
2864                elev = (elev >> 8) | (elev << 8);
[194503c]2865#endif
[9df33bc]2866            }
[194503c]2867            double Z = (short)elev;
[6388423]2868            Vector3 pt;
2869            if (Z == nodata_value) {
2870                pt = Vector3(DBL_MAX, DBL_MAX, DBL_MAX);
2871            } else {
2872                double X = X_;
2873                double Y = (o_y - y * step_y) * DEG_TO_RAD;
2874                pj_transform(pj_in, pj_out, 1, 1, &X, &Y, &Z);
2875                pt = Vector3(X, Y, Z) - off;
2876                double dist_2 = sqrd(pt.GetX()) + sqrd(pt.GetY());
2877                if (dist_2 > r_sqrd) {
2878                    pt = Vector3(DBL_MAX, DBL_MAX, DBL_MAX);
2879                }
2880            }
2881            if (x > 0 && y > 0) {
2882                const Vector3 & a = prevcol[y - 1];
2883                const Vector3 & b = prevcol[y];
2884                // If all points are valid, split the quadrilateral into
2885                // triangles along the shorter 3D diagonal, which typically
2886                // looks better:
2887                //
2888                //               ----->
2889                //     prev---a    x     prev---a
2890                //   |   |P  /|            |\  S|
2891                // y |   |  / |    or      | \  |
2892                //   V   | /  |            |  \ |
2893                //       |/  Q|            |R  \|
2894                //       b----pt           b----pt
2895                //
2896                //       FORWARD           BACKWARD
2897                enum { NONE = 0, P = 1, Q = 2, R = 4, S = 8, ALL = P|Q|R|S };
2898                int valid =
2899                    ((prev.GetZ() != DBL_MAX)) |
2900                    ((a.GetZ() != DBL_MAX) << 1) |
2901                    ((b.GetZ() != DBL_MAX) << 2) |
2902                    ((pt.GetZ() != DBL_MAX) << 3);
2903                static const int tris_map[16] = {
2904                    NONE, // nothing valid
2905                    NONE, // prev
2906                    NONE, // a
2907                    NONE, // a, prev
2908                    NONE, // b
2909                    NONE, // b, prev
2910                    NONE, // b, a
2911                    P, // b, a, prev
2912                    NONE, // pt
2913                    NONE, // pt, prev
2914                    NONE, // pt, a
2915                    S, // pt, a, prev
2916                    NONE, // pt, b
2917                    R, // pt, b, prev
2918                    Q, // pt, b, a
2919                    ALL, // pt, b, a, prev
2920                };
2921                int tris = tris_map[valid];
2922                if (tris == ALL) {
2923                    // All points valid.
2924                    if ((a - b).magnitude() < (prev - pt).magnitude()) {
2925                        tris = P | Q;
2926                    } else {
2927                        tris = R | S;
2928                    }
2929                }
2930                if (tris & P)
2931                    DrawTerrainTriangle(a, prev, b);
2932                if (tris & Q)
2933                    DrawTerrainTriangle(a, b, pt);
2934                if (tris & R)
2935                    DrawTerrainTriangle(pt, prev, b);
2936                if (tris & S)
2937                    DrawTerrainTriangle(a, prev, pt);
2938            }
2939            prev = prevcol[y];
2940            prevcol[y].assign(pt);
2941        }
2942    }
2943    EndTriangles();
2944    SetAlpha(1.0);
[7685ae3]2945    if (n_tris == 0) {
2946        ToggleTerrain();
2947        delete [] dem;
2948        dem = NULL;
2949        hourglass.stop();
2950        /* TRANSLATORS: Aven shows a circle of terrain covering the area
2951         * of the survey plus a bit, but the terrain data file didn't
2952         * contain any data inside that circle.
2953         */
2954        error(/*No terrain data near area of survey*/161);
2955    }
[6388423]2956}
2957
[d2fcc9b]2958// Plot blobs.
[d9b3270]2959void GfxCore::GenerateBlobsDisplayList()
2960{
[e633bb1]2961    if (!(m_Entrances || m_FixedPts || m_ExportedPts ||
2962          m_Parent->GetNumHighlightedPts()))
2963        return;
[429465a]2964
[e633bb1]2965    // Plot blobs.
2966    gla_colour prev_col = col_BLACK; // not a colour used for blobs
2967    list<LabelInfo*>::const_iterator pos = m_Parent->GetLabels();
2968    BeginBlobs();
2969    while (pos != m_Parent->GetLabelsEnd()) {
[429465a]2970        const LabelInfo* label = *pos++;
2971
2972        // When more than one flag is set on a point:
2973        // search results take priority over entrance highlighting
2974        // which takes priority over fixed point
2975        // highlighting, which in turn takes priority over exported
2976        // point highlighting.
2977
2978        if (!((m_Surface && label->IsSurface()) ||
2979              (m_Legs && label->IsUnderground()) ||
2980              (!label->IsSurface() && !label->IsUnderground()))) {
2981            // if this station isn't to be displayed, skip to the next
2982            // (last case is for stns with no legs attached)
2983            continue;
2984        }
2985
[e633bb1]2986        gla_colour col;
2987
[429465a]2988        if (label->IsHighLighted()) {
2989            col = col_YELLOW;
2990        } else if (m_Entrances && label->IsEntrance()) {
2991            col = col_GREEN;
2992        } else if (m_FixedPts && label->IsFixedPt()) {
2993            col = col_RED;
2994        } else if (m_ExportedPts && label->IsExportedPt()) {
2995            col = col_TURQUOISE;
2996        } else {
2997            continue;
2998        }
2999
[e633bb1]3000        // Stations are sorted by blob type, so colour changes are infrequent.
3001        if (col != prev_col) {
[aa048c3]3002            SetColour(col);
[e633bb1]3003            prev_col = col;
[429465a]3004        }
[e633bb1]3005        DrawBlob(label->GetX(), label->GetY(), label->GetZ());
[d9b3270]3006    }
[e633bb1]3007    EndBlobs();
[33b2094]3008}
3009
[6747314]3010void GfxCore::DrawIndicators()
[33b2094]3011{
[97ea48d]3012    // Draw colour key.
3013    if (m_ColourKey) {
[47c62d04]3014        drawing_list key_list = LIST_LIMIT_;
3015        switch (m_ColourBy) {
3016            case COLOUR_BY_DEPTH:
3017                key_list = LIST_DEPTH_KEY; break;
3018            case COLOUR_BY_DATE:
3019                key_list = LIST_DATE_KEY; break;
3020            case COLOUR_BY_ERROR:
3021                key_list = LIST_ERROR_KEY; break;
[cc9e2c65]3022            case COLOUR_BY_GRADIENT:
3023                key_list = LIST_GRADIENT_KEY; break;
[47c62d04]3024            case COLOUR_BY_LENGTH:
3025                key_list = LIST_LENGTH_KEY; break;
3026        }
3027        if (key_list != LIST_LIMIT_) {
3028            DrawList2D(key_list, GetXSize() - KEY_OFFSET_X,
[af50685]3029                       GetYSize() - KEY_OFFSET_Y, 0);
[1b164a0]3030        }
[33b2094]3031    }
[56da40e]3032
[203d2a7]3033    // Draw compass or elevation/heading indicators.
[eef68f9]3034    if (m_Compass || m_Clino) {
3035        if (!m_Parent->IsExtendedElevation()) Draw2dIndicators();
[203d2a7]3036    }
[f433fda]3037
[56da40e]3038    // Draw scalebar.
3039    if (m_Scalebar) {
[9c37beb]3040        DrawList2D(LIST_SCALE_BAR, 0, 0, 0);
[56da40e]3041    }
[33b2094]3042}
3043
[f336ab9]3044void GfxCore::PlaceVertexWithColour(const Vector3 & v,
3045                                    glaTexCoord tex_x, glaTexCoord tex_y,
[b839829]3046                                    Double factor)
[f383708]3047{
[d1ce9bd]3048    SetColour(col_WHITE, factor);
[b839829]3049    PlaceVertex(v, tex_x, tex_y);
[da6c802]3050}
[f433fda]3051
[b839829]3052void GfxCore::SetDepthColour(Double z, Double factor) {
[da6c802]3053    // Set the drawing colour based on the altitude.
[78c67a6]3054    Double z_ext = m_Parent->GetDepthExtent();
[f383708]3055
[b839829]3056    z -= m_Parent->GetDepthMin();
[f383708]3057    // points arising from tubes may be slightly outside the limits...
[78c67a6]3058    if (z < 0) z = 0;
3059    if (z > z_ext) z = z_ext;
[a6f081c]3060
[2a9d2fa]3061    if (z == 0) {
3062        SetColour(GetPen(0), factor);
3063        return;
3064    }
3065
3066    assert(z_ext > 0.0);
[78c67a6]3067    Double how_far = z / z_ext;
[f383708]3068    assert(how_far >= 0.0);
3069    assert(how_far <= 1.0);
3070
[97ea48d]3071    int band = int(floor(how_far * (GetNumColourBands() - 1)));
[0e69efe]3072    GLAPen pen1 = GetPen(band);
[97ea48d]3073    if (band < GetNumColourBands() - 1) {
[d4650b3]3074        const GLAPen& pen2 = GetPen(band + 1);
[f433fda]3075
[97ea48d]3076        Double interval = z_ext / (GetNumColourBands() - 1);
[78c67a6]3077        Double into_band = z / interval - band;
[f433fda]3078
[d4650b3]3079//      printf("%g z_offset=%g interval=%g band=%d\n", into_band,
3080//             z_offset, interval, band);
3081        // FIXME: why do we need to clamp here?  Is it because the walls can
3082        // extend further up/down than the centre-line?
3083        if (into_band < 0.0) into_band = 0.0;
3084        if (into_band > 1.0) into_band = 1.0;
3085        assert(into_band >= 0.0);
3086        assert(into_band <= 1.0);
[f433fda]3087
[d4650b3]3088        pen1.Interpolate(pen2, into_band);
3089    }
[aa048c3]3090    SetColour(pen1, factor);
[b839829]3091}
[f383708]3092
[b839829]3093void GfxCore::PlaceVertexWithDepthColour(const Vector3 &v, Double factor)
3094{
3095    SetDepthColour(v.GetZ(), factor);
[d67450e]3096    PlaceVertex(v);
[f383708]3097}
3098
[b839829]3099void GfxCore::PlaceVertexWithDepthColour(const Vector3 &v,
[f336ab9]3100                                         glaTexCoord tex_x, glaTexCoord tex_y,
[b839829]3101                                         Double factor)
3102{
3103    SetDepthColour(v.GetZ(), factor);
3104    PlaceVertex(v, tex_x, tex_y);
3105}
3106
[82f584f]3107void GfxCore::SplitLineAcrossBands(int band, int band2,
[4a0e6b35]3108                                   const Vector3 &p, const Vector3 &q,
[82f584f]3109                                   Double factor)
[b5d64e6]3110{
[4a0e6b35]3111    const int step = (band < band2) ? 1 : -1;
[b5d64e6]3112    for (int i = band; i != band2; i += step) {
[4a0e6b35]3113        const Double z = GetDepthBoundaryBetweenBands(i, i + step);
3114
3115        // Find the intersection point of the line p -> q
3116        // with the plane parallel to the xy-plane with z-axis intersection z.
[d67450e]3117        assert(q.GetZ() - p.GetZ() != 0.0);
[4a0e6b35]3118
[d67450e]3119        const Double t = (z - p.GetZ()) / (q.GetZ() - p.GetZ());
[4a0e6b35]3120//      assert(0.0 <= t && t <= 1.0);           FIXME: rounding problems!
3121
[d67450e]3122        const Double x = p.GetX() + t * (q.GetX() - p.GetX());
3123        const Double y = p.GetY() + t * (q.GetY() - p.GetY());
[4a0e6b35]3124
[d67450e]3125        PlaceVertexWithDepthColour(Vector3(x, y, z), factor);
[b5d64e6]3126    }
3127}
3128
[14acdae]3129int GfxCore::GetDepthColour(Double z) const
[b5d64e6]3130{
[82f584f]3131    // Return the (0-based) depth colour band index for a z-coordinate.
[78c67a6]3132    Double z_ext = m_Parent->GetDepthExtent();
3133    z -= m_Parent->GetDepthMin();
[2ba3882]3134    // We seem to get rounding differences causing z to sometimes be slightly
[0a2aab8]3135    // less than GetDepthMin() here, and it can certainly be true for passage
3136    // tubes, so just clamp the value to 0.
[2ba3882]3137    if (z <= 0) return 0;
[6027220]3138    // We seem to get rounding differences causing z to sometimes exceed z_ext
[2c1c52e]3139    // by a small amount here (see: https://trac.survex.com/ticket/26) and it
[0a2aab8]3140    // can certainly be true for passage tubes, so just clamp the value.
3141    if (z >= z_ext) return GetNumColourBands() - 1;
[97ea48d]3142    return int(z / z_ext * (GetNumColourBands() - 1));
[b5d64e6]3143}
3144
[14acdae]3145Double GfxCore::GetDepthBoundaryBetweenBands(int a, int b) const
[b5d64e6]3146{
[82f584f]3147    // Return the z-coordinate of the depth colour boundary between
3148    // two adjacent depth colour bands (specified by 0-based indices).
3149
3150    assert((a == b - 1) || (a == b + 1));
[97ea48d]3151    if (GetNumColourBands() == 1) return 0;
[82f584f]3152
3153    int band = (a > b) ? a : b; // boundary N lies on the bottom of band N.
[78c67a6]3154    Double z_ext = m_Parent->GetDepthExtent();
[2a9d2fa]3155    return (z_ext * band / (GetNumColourBands() - 1)) + m_Parent->GetDepthMin();
[b5d64e6]3156}
3157
[c61aa79]3158void GfxCore::AddPolyline(const traverse & centreline)
[da6c802]3159{
3160    BeginPolyline();
[d1ce9bd]3161    SetColour(col_WHITE);
[d4650b3]3162    vector<PointInfo>::const_iterator i = centreline.begin();
[d67450e]3163    PlaceVertex(*i);
[da6c802]3164    ++i;
3165    while (i != centreline.end()) {
[d67450e]3166        PlaceVertex(*i);
[da6c802]3167        ++i;
3168    }
3169    EndPolyline();
3170}
[2a3d328]3171
[c61aa79]3172void GfxCore::AddPolylineShadow(const traverse & centreline)
[f4c5932]3173{
3174    BeginPolyline();
[78c67a6]3175    const double z = -0.5 * m_Parent->GetZExtent();
[d4650b3]3176    vector<PointInfo>::const_iterator i = centreline.begin();
[78c67a6]3177    PlaceVertex(i->GetX(), i->GetY(), z);
[f4c5932]3178    ++i;
3179    while (i != centreline.end()) {
[78c67a6]3180        PlaceVertex(i->GetX(), i->GetY(), z);
[f4c5932]3181        ++i;
3182    }
3183    EndPolyline();
3184}
3185
[c61aa79]3186void GfxCore::AddPolylineDepth(const traverse & centreline)
[da6c802]3187{
3188    BeginPolyline();
[d4650b3]3189    vector<PointInfo>::const_iterator i, prev_i;
[da6c802]3190    i = centreline.begin();
[ee7af72]3191    int band0 = GetDepthColour(i->GetZ());
[d67450e]3192    PlaceVertexWithDepthColour(*i);
[da6c802]3193    prev_i = i;
3194    ++i;
3195    while (i != centreline.end()) {
[ee7af72]3196        int band = GetDepthColour(i->GetZ());
[da6c802]3197        if (band != band0) {
[d67450e]3198            SplitLineAcrossBands(band0, band, *prev_i, *i);
[da6c802]3199            band0 = band;
3200        }
[d67450e]3201        PlaceVertexWithDepthColour(*i);
[da6c802]3202        prev_i = i;
3203        ++i;
3204    }
3205    EndPolyline();
3206}
3207
[f433fda]3208void GfxCore::AddQuadrilateral(const Vector3 &a, const Vector3 &b,
[14acdae]3209                               const Vector3 &c, const Vector3 &d)
[da6c802]3210{
3211    Vector3 normal = (a - c) * (d - b);
3212    normal.normalise();
3213    Double factor = dot(normal, light) * .3 + .7;
[f336ab9]3214    glaTexCoord w(ceil(((b - a).magnitude() + (d - c).magnitude()) * .5));
3215    glaTexCoord h(ceil(((b - c).magnitude() + (d - a).magnitude()) * .5));
[9b57c71b]3216    // FIXME: should plot triangles instead to avoid rendering glitches.
[da6c802]3217    BeginQuadrilaterals();
[b839829]3218    PlaceVertexWithColour(a, 0, 0, factor);
3219    PlaceVertexWithColour(b, w, 0, factor);
3220    PlaceVertexWithColour(c, w, h, factor);
3221    PlaceVertexWithColour(d, 0, h, factor);
[da6c802]3222    EndQuadrilaterals();
3223}
3224
3225void GfxCore::AddQuadrilateralDepth(const Vector3 &a, const Vector3 &b,
3226                                    const Vector3 &c, const Vector3 &d)
[2b02270]3227{
3228    Vector3 normal = (a - c) * (d - b);
3229    normal.normalise();
3230    Double factor = dot(normal, light) * .3 + .7;
3231    int a_band, b_band, c_band, d_band;
[d67450e]3232    a_band = GetDepthColour(a.GetZ());
[97ea48d]3233    a_band = min(max(a_band, 0), GetNumColourBands());
[d67450e]3234    b_band = GetDepthColour(b.GetZ());
[97ea48d]3235    b_band = min(max(b_band, 0), GetNumColourBands());
[d67450e]3236    c_band = GetDepthColour(c.GetZ());
[97ea48d]3237    c_band = min(max(c_band, 0), GetNumColourBands());
[d67450e]3238    d_band = GetDepthColour(d.GetZ());
[97ea48d]3239    d_band = min(max(d_band, 0), GetNumColourBands());
[97fb83a]3240    // All this splitting is incorrect - we need to make a separate polygon
3241    // for each depth band...
[f336ab9]3242    glaTexCoord w(ceil(((b - a).magnitude() + (d - c).magnitude()) * .5));
3243    glaTexCoord h(ceil(((b - c).magnitude() + (d - a).magnitude()) * .5));
[b5d64e6]3244    BeginPolygon();
[d67450e]3245////    PlaceNormal(normal);
[b839829]3246    PlaceVertexWithDepthColour(a, 0, 0, factor);
[2b02270]3247    if (a_band != b_band) {
[b5d64e6]3248        SplitLineAcrossBands(a_band, b_band, a, b, factor);
[2b02270]3249    }
[b839829]3250    PlaceVertexWithDepthColour(b, w, 0, factor);
[2b02270]3251    if (b_band != c_band) {
[b5d64e6]3252        SplitLineAcrossBands(b_band, c_band, b, c, factor);
[2b02270]3253    }
[b839829]3254    PlaceVertexWithDepthColour(c, w, h, factor);
[2b02270]3255    if (c_band != d_band) {
[b5d64e6]3256        SplitLineAcrossBands(c_band, d_band, c, d, factor);
[2b02270]3257    }
[b839829]3258    PlaceVertexWithDepthColour(d, 0, h, factor);
[2b02270]3259    if (d_band != a_band) {
[b5d64e6]3260        SplitLineAcrossBands(d_band, a_band, d, a, factor);
[2b02270]3261    }
[b5d64e6]3262    EndPolygon();
[2b02270]3263}
3264
[1ee204e]3265void GfxCore::SetColourFromDate(int date, Double factor)
[d4650b3]3266{
3267    // Set the drawing colour based on a date.
3268
[1ee204e]3269    if (date == -1) {
[2043961]3270        // Undated.
[d1ce9bd]3271        SetColour(col_WHITE, factor);
[d4650b3]3272        return;
3273    }
3274
[1ee204e]3275    int date_offset = date - m_Parent->GetDateMin();
[2043961]3276    if (date_offset == 0) {
3277        // Earliest date - handle as a special case for the single date case.
3278        SetColour(GetPen(0), factor);
3279        return;
3280    }
[d4650b3]3281
[2043961]3282    int date_ext = m_Parent->GetDateExtent();
[d4650b3]3283    Double how_far = (Double)date_offset / date_ext;
3284    assert(how_far >= 0.0);
3285    assert(how_far <= 1.0);
[371f9ed]3286    SetColourFrom01(how_far, factor);
[d4650b3]3287}
3288
[c61aa79]3289void GfxCore::AddPolylineDate(const traverse & centreline)
[d4650b3]3290{
3291    BeginPolyline();
3292    vector<PointInfo>::const_iterator i, prev_i;
3293    i = centreline.begin();
[1ee204e]3294    int date = i->GetDate();
[d4650b3]3295    SetColourFromDate(date, 1.0);
[d67450e]3296    PlaceVertex(*i);
[d4650b3]3297    prev_i = i;
3298    while (++i != centreline.end()) {
[1ee204e]3299        int newdate = i->GetDate();
[d4650b3]3300        if (newdate != date) {
3301            EndPolyline();
3302            BeginPolyline();
3303            date = newdate;
3304            SetColourFromDate(date, 1.0);
[d67450e]3305            PlaceVertex(*prev_i);
[d4650b3]3306        }
[d67450e]3307        PlaceVertex(*i);
[d4650b3]3308        prev_i = i;
3309    }
3310    EndPolyline();
3311}
3312
[1ee204e]3313static int static_date_hack; // FIXME
[d4650b3]3314
3315void GfxCore::AddQuadrilateralDate(const Vector3 &a, const Vector3 &b,
3316                                   const Vector3 &c, const Vector3 &d)
3317{
3318    Vector3 normal = (a - c) * (d - b);
3319    normal.normalise();
3320    Double factor = dot(normal, light) * .3 + .7;
3321    int w = int(ceil(((b - a).magnitude() + (d - c).magnitude()) / 2));
3322    int h = int(ceil(((b - c).magnitude() + (d - a).magnitude()) / 2));
3323    // FIXME: should plot triangles instead to avoid rendering glitches.
[b839829]3324    BeginQuadrilaterals();
[d67450e]3325////    PlaceNormal(normal);
[d4650b3]3326    SetColourFromDate(static_date_hack, factor);
[b839829]3327    PlaceVertex(a, 0, 0);
3328    PlaceVertex(b, w, 0);
3329    PlaceVertex(c, w, h);
3330    PlaceVertex(d, 0, h);
3331    EndQuadrilaterals();
[d4650b3]3332}
3333
[c61aa79]3334static double static_E_hack; // FIXME
3335
3336void GfxCore::SetColourFromError(double E, Double factor)
3337{
3338    // Set the drawing colour based on an error value.
3339
3340    if (E < 0) {
[d1ce9bd]3341        SetColour(col_WHITE, factor);
[c61aa79]3342        return;
3343    }
3344
3345    Double how_far = E / MAX_ERROR;
3346    assert(how_far >= 0.0);
3347    if (how_far > 1.0) how_far = 1.0;
[371f9ed]3348    SetColourFrom01(how_far, factor);
[c61aa79]3349}
3350
3351void GfxCore::AddQuadrilateralError(const Vector3 &a, const Vector3 &b,
3352                                    const Vector3 &c, const Vector3 &d)
3353{
3354    Vector3 normal = (a - c) * (d - b);
3355    normal.normalise();
3356    Double factor = dot(normal, light) * .3 + .7;
3357    int w = int(ceil(((b - a).magnitude() + (d - c).magnitude()) / 2));
3358    int h = int(ceil(((b - c).magnitude() + (d - a).magnitude()) / 2));
3359    // FIXME: should plot triangles instead to avoid rendering glitches.
[b839829]3360    BeginQuadrilaterals();
[c61aa79]3361////    PlaceNormal(normal);
3362    SetColourFromError(static_E_hack, factor);
[b839829]3363    PlaceVertex(a, 0, 0);
3364    PlaceVertex(b, w, 0);
3365    PlaceVertex(c, w, h);
3366    PlaceVertex(d, 0, h);
3367    EndQuadrilaterals();
[c61aa79]3368}
3369
3370void GfxCore::AddPolylineError(const traverse & centreline)
3371{
3372    BeginPolyline();
3373    SetColourFromError(centreline.E, 1.0);
3374    vector<PointInfo>::const_iterator i;
3375    for(i = centreline.begin(); i != centreline.end(); ++i) {
3376        PlaceVertex(*i);
3377    }
3378    EndPolyline();
3379}
3380
[cc9e2c65]3381// gradient is in *radians*.
3382void GfxCore::SetColourFromGradient(double gradient, Double factor)
3383{
3384    // Set the drawing colour based on the gradient of the leg.
3385
3386    const Double GRADIENT_MAX = M_PI_2;
3387    gradient = fabs(gradient);
3388    Double how_far = gradient / GRADIENT_MAX;
3389    SetColourFrom01(how_far, factor);
3390}
3391
3392void GfxCore::AddPolylineGradient(const traverse & centreline)
3393{
3394    vector<PointInfo>::const_iterator i, prev_i;
3395    i = centreline.begin();
3396    prev_i = i;
3397    while (++i != centreline.end()) {
3398        BeginPolyline();
3399        SetColourFromGradient((*i - *prev_i).gradient(), 1.0);
3400        PlaceVertex(*prev_i);
3401        PlaceVertex(*i);
3402        prev_i = i;
3403        EndPolyline();
3404    }
3405}
3406
3407static double static_gradient_hack; // FIXME
3408
3409void GfxCore::AddQuadrilateralGradient(const Vector3 &a, const Vector3 &b,
3410                                       const Vector3 &c, const Vector3 &d)
3411{
3412    Vector3 normal = (a - c) * (d - b);
3413    normal.normalise();
3414    Double factor = dot(normal, light) * .3 + .7;
3415    int w = int(ceil(((b - a).magnitude() + (d - c).magnitude()) / 2));
3416    int h = int(ceil(((b - c).magnitude() + (d - a).magnitude()) / 2));
3417    // FIXME: should plot triangles instead to avoid rendering glitches.
3418    BeginQuadrilaterals();
3419////    PlaceNormal(normal);
3420    SetColourFromGradient(static_gradient_hack, factor);
3421    PlaceVertex(a, 0, 0);
3422    PlaceVertex(b, w, 0);
3423    PlaceVertex(c, w, h);
3424    PlaceVertex(d, 0, h);
3425    EndQuadrilaterals();
3426}
3427
[af50685]3428void GfxCore::SetColourFromLength(double length, Double factor)
3429{
3430    // Set the drawing colour based on log(length_of_leg).
3431
3432    Double log_len = log10(length);
3433    Double how_far = log_len / LOG_LEN_MAX;
3434    how_far = max(how_far, 0.0);
3435    how_far = min(how_far, 1.0);
[371f9ed]3436    SetColourFrom01(how_far, factor);
3437}
[af50685]3438
[371f9ed]3439void GfxCore::SetColourFrom01(double how_far, Double factor)
3440{
3441    double b;
3442    double into_band = modf(how_far * (GetNumColourBands() - 1), &b);
3443    int band(b);
[af50685]3444    GLAPen pen1 = GetPen(band);
[371f9ed]3445    // With 24bit colour, interpolating by less than this can have no effect.
3446    if (into_band >= 1.0 / 512.0) {
[af50685]3447        const GLAPen& pen2 = GetPen(band + 1);
3448        pen1.Interpolate(pen2, into_band);
3449    }
3450    SetColour(pen1, factor);
3451}
3452
3453void GfxCore::AddPolylineLength(const traverse & centreline)
3454{
3455    vector<PointInfo>::const_iterator i, prev_i;
3456    i = centreline.begin();
3457    prev_i = i;
3458    while (++i != centreline.end()) {
3459        BeginPolyline();
[5afbd60]3460        SetColourFromLength((*i - *prev_i).magnitude(), 1.0);
[af50685]3461        PlaceVertex(*prev_i);
3462        PlaceVertex(*i);
3463        prev_i = i;
3464        EndPolyline();
3465    }
3466}
3467
3468static double static_length_hack; // FIXME
3469
3470void GfxCore::AddQuadrilateralLength(const Vector3 &a, const Vector3 &b,
3471                                     const Vector3 &c, const Vector3 &d)
3472{
3473    Vector3 normal = (a - c) * (d - b);
3474    normal.normalise();
3475    Double factor = dot(normal, light) * .3 + .7;
3476    int w = int(ceil(((b - a).magnitude() + (d - c).magnitude()) / 2));
3477    int h = int(ceil(((b - c).magnitude() + (d - a).magnitude()) / 2));
3478    // FIXME: should plot triangles instead to avoid rendering glitches.
3479    BeginQuadrilaterals();
3480////    PlaceNormal(normal);
3481    SetColourFromLength(static_length_hack, factor);
3482    PlaceVertex(a, 0, 0);
3483    PlaceVertex(b, w, 0);
3484    PlaceVertex(c, w, h);
3485    PlaceVertex(d, 0, h);
3486    EndQuadrilaterals();
3487}
3488
[da6c802]3489void
[384534c]3490GfxCore::SkinPassage(vector<XSect> & centreline, bool draw)
[3ddcad8]3491{
[b3852b5]3492    assert(centreline.size() > 1);
[3ddcad8]3493    Vector3 U[4];
[ee05463]3494    XSect prev_pt_v;
[3ddcad8]3495    Vector3 last_right(1.0, 0.0, 0.0);
3496
[c61aa79]3497//  FIXME: it's not simple to set the colour of a tube based on error...
3498//    static_E_hack = something...
[fc68ad5]3499    vector<XSect>::iterator i = centreline.begin();
[ee05463]3500    vector<XSect>::size_type segment = 0;
[3ddcad8]3501    while (i != centreline.end()) {
3502        // get the coordinates of this vertex
[fc68ad5]3503        XSect & pt_v = *i++;
[3ddcad8]3504
3505        bool cover_end = false;
3506
3507        Vector3 right, up;
3508
3509        const Vector3 up_v(0.0, 0.0, 1.0);
3510
3511        if (segment == 0) {
3512            assert(i != centreline.end());
3513            // first segment
3514
3515            // get the coordinates of the next vertex
[ee05463]3516            const XSect & next_pt_v = *i;
[3ddcad8]3517
3518            // calculate vector from this pt to the next one
[d67450e]3519            Vector3 leg_v = next_pt_v - pt_v;
[3ddcad8]3520
3521            // obtain a vector in the LRUD plane
3522            right = leg_v * up_v;
3523            if (right.magnitude() == 0) {
3524                right = last_right;
3525                // Obtain a second vector in the LRUD plane,
3526                // perpendicular to the first.
[760ad29d]3527                //up = right * leg_v;
3528                up = up_v;
[3ddcad8]3529            } else {
3530                last_right = right;
3531                up = up_v;
[da6c802]3532            }
3533
[3ddcad8]3534            cover_end = true;
[d4650b3]3535            static_date_hack = next_pt_v.GetDate();
[3ddcad8]3536        } else if (segment + 1 == centreline.size()) {
3537            // last segment
3538
3539            // Calculate vector from the previous pt to this one.
[d67450e]3540            Vector3 leg_v = pt_v - prev_pt_v;
[3ddcad8]3541
3542            // Obtain a horizontal vector in the LRUD plane.
3543            right = leg_v * up_v;
3544            if (right.magnitude() == 0) {
[d67450e]3545                right = Vector3(last_right.GetX(), last_right.GetY(), 0.0);
[3ddcad8]3546                // Obtain a second vector in the LRUD plane,
3547                // perpendicular to the first.
[760ad29d]3548                //up = right * leg_v;
3549                up = up_v;
[3ddcad8]3550            } else {
3551                last_right = right;
3552                up = up_v;
3553            }
[da6c802]3554
[3ddcad8]3555            cover_end = true;
[d4650b3]3556            static_date_hack = pt_v.GetDate();
[3ddcad8]3557        } else {
3558            assert(i != centreline.end());
3559            // Intermediate segment.
3560
3561            // Get the coordinates of the next vertex.
[ee05463]3562            const XSect & next_pt_v = *i;
[3ddcad8]3563
3564            // Calculate vectors from this vertex to the
3565            // next vertex, and from the previous vertex to
3566            // this one.
[d67450e]3567            Vector3 leg1_v = pt_v - prev_pt_v;
3568            Vector3 leg2_v = next_pt_v - pt_v;
[3ddcad8]3569
3570            // Obtain horizontal vectors perpendicular to
3571            // both legs, then normalise and average to get
3572            // a horizontal bisector.
3573            Vector3 r1 = leg1_v * up_v;
3574            Vector3 r2 = leg2_v * up_v;
3575            r1.normalise();
3576            r2.normalise();
3577            right = r1 + r2;
3578            if (right.magnitude() == 0) {
3579                // This is the "mid-pitch" case...
3580                right = last_right;
3581            }
3582            if (r1.magnitude() == 0) {
[760ad29d]3583                up = up_v;
[3ddcad8]3584
3585                // Rotate pitch section to minimise the
3586                // "tortional stress" - FIXME: use
3587                // triangles instead of rectangles?
3588                int shift = 0;
3589                Double maxdotp = 0;
3590
3591                // Scale to unit vectors in the LRUD plane.
3592                right.normalise();
3593                up.normalise();
3594                Vector3 vec = up - right;
3595                for (int orient = 0; orient <= 3; ++orient) {
[d67450e]3596                    Vector3 tmp = U[orient] - prev_pt_v;
[3ddcad8]3597                    tmp.normalise();
3598                    Double dotp = dot(vec, tmp);
3599                    if (dotp > maxdotp) {
3600                        maxdotp = dotp;
3601                        shift = orient;
3602                    }
3603                }
3604                if (shift) {
3605                    if (shift != 2) {
3606                        Vector3 temp(U[0]);
[b3852b5]3607                        U[0] = U[shift];
3608                        U[shift] = U[2];
3609                        U[2] = U[shift ^ 2];
3610                        U[shift ^ 2] = temp;
[ee7af72]3611                    } else {
[3ddcad8]3612                        swap(U[0], U[2]);
3613                        swap(U[1], U[3]);
[ee7af72]3614                    }
[3ddcad8]3615                }
3616#if 0
3617                // Check that the above code actually permuted
3618                // the vertices correctly.
3619                shift = 0;
3620                maxdotp = 0;
[b3852b5]3621                for (int j = 0; j <= 3; ++j) {
[d67450e]3622                    Vector3 tmp = U[j] - prev_pt_v;
[3ddcad8]3623                    tmp.normalise();
3624                    Double dotp = dot(vec, tmp);
3625                    if (dotp > maxdotp) {
3626                        maxdotp = dotp + 1e-6; // Add small tolerance to stop 45 degree offset cases being flagged...
[b3852b5]3627                        shift = j;
[da6c802]3628                    }
[3ddcad8]3629                }
3630                if (shift) {
3631                    printf("New shift = %d!\n", shift);
3632                    shift = 0;
3633                    maxdotp = 0;
[b3852b5]3634                    for (int j = 0; j <= 3; ++j) {
[d67450e]3635                        Vector3 tmp = U[j] - prev_pt_v;
[3ddcad8]3636                        tmp.normalise();
3637                        Double dotp = dot(vec, tmp);
[b3852b5]3638                        printf("    %d : %.8f\n", j, dotp);
[da6c802]3639                    }
3640                }
[3ddcad8]3641#endif
3642            } else {
3643                up = up_v;
[da6c802]3644            }
[3ddcad8]3645            last_right = right;
[d4650b3]3646            static_date_hack = pt_v.GetDate();
[da6c802]3647        }
3648
[3ddcad8]3649        // Scale to unit vectors in the LRUD plane.
3650        right.normalise();
3651        up.normalise();
[33b2094]3652
[57a3cd4]3653        Double l = fabs(pt_v.GetL());
3654        Double r = fabs(pt_v.GetR());
3655        Double u = fabs(pt_v.GetU());
3656        Double d = fabs(pt_v.GetD());
[3ddcad8]3657
3658        // Produce coordinates of the corners of the LRUD "plane".
3659        Vector3 v[4];
[d67450e]3660        v[0] = pt_v - right * l + up * u;
3661        v[1] = pt_v + right * r + up * u;
3662        v[2] = pt_v + right * r - up * d;
3663        v[3] = pt_v - right * l - up * d;
[3ddcad8]3664
[384534c]3665        if (draw) {
[cc9e2c65]3666            const Vector3 & delta = pt_v - prev_pt_v;
3667            static_length_hack = delta.magnitude();
3668            static_gradient_hack = delta.gradient();
[384534c]3669            if (segment > 0) {
3670                (this->*AddQuad)(v[0], v[1], U[1], U[0]);
3671                (this->*AddQuad)(v[2], v[3], U[3], U[2]);
3672                (this->*AddQuad)(v[1], v[2], U[2], U[1]);
3673                (this->*AddQuad)(v[3], v[0], U[0], U[3]);
3674            }
[9eb58d0]3675
[384534c]3676            if (cover_end) {
[8fc6473]3677                if (segment == 0) {
3678                    (this->*AddQuad)(v[0], v[1], v[2], v[3]);
3679                } else {
3680                    (this->*AddQuad)(v[3], v[2], v[1], v[0]);
3681                }
[384534c]3682            }
[3ddcad8]3683        }
[9eb58d0]3684
[3ddcad8]3685        prev_pt_v = pt_v;
3686        U[0] = v[0];
3687        U[1] = v[1];
3688        U[2] = v[2];
3689        U[3] = v[3];
[9eb58d0]3690
[fc68ad5]3691        pt_v.set_right_bearing(deg(atan2(right.GetY(), right.GetX())));
3692
[3ddcad8]3693        ++segment;
3694    }
[33b2094]3695}
[b13aee4]3696
3697void GfxCore::FullScreenMode()
3698{
[ea940373]3699    m_Parent->ViewFullScreen();
[b13aee4]3700}
[fdfa926]3701
3702bool GfxCore::IsFullScreen() const
3703{
3704    return m_Parent->IsFullScreen();
3705}
[1690fa9]3706
[b75a37d]3707bool GfxCore::FullScreenModeShowingMenus() const
3708{
3709    return m_Parent->FullScreenModeShowingMenus();
3710}
3711
3712void GfxCore::FullScreenModeShowMenus(bool show)
3713{
3714    m_Parent->FullScreenModeShowMenus(show);
3715}
3716
[46361bc]3717void
3718GfxCore::MoveViewer(double forward, double up, double right)
3719{
[e577f89]3720    double cT = cos(rad(m_TiltAngle));
3721    double sT = sin(rad(m_TiltAngle));
3722    double cP = cos(rad(m_PanAngle));
3723    double sP = sin(rad(m_PanAngle));
[7a57dc7]3724    Vector3 v_forward(cT * sP, cT * cP, sT);
3725    Vector3 v_up(sT * sP, sT * cP, -cT);
[867a1141]3726    Vector3 v_right(-cP, sP, 0);
[d4a5aaf]3727    assert(fabs(dot(v_forward, v_up)) < 1e-6);
3728    assert(fabs(dot(v_forward, v_right)) < 1e-6);
3729    assert(fabs(dot(v_right, v_up)) < 1e-6);
[46361bc]3730    Vector3 move = v_forward * forward + v_up * up + v_right * right;
[d67450e]3731    AddTranslation(-move);
[d877aa2]3732    // Show current position.
[d67450e]3733    m_Parent->SetCoords(m_Parent->GetOffset() - GetTranslation());
[46361bc]3734    ForceRefresh();
3735}
3736
[1690fa9]3737PresentationMark GfxCore::GetView() const
3738{
[d67450e]3739    return PresentationMark(GetTranslation() + m_Parent->GetOffset(),
[7a57dc7]3740                            m_PanAngle, -m_TiltAngle, m_Scale);
[1690fa9]3741}
3742
3743void GfxCore::SetView(const PresentationMark & p)
3744{
3745    m_SwitchingTo = 0;
[d67450e]3746    SetTranslation(p - m_Parent->GetOffset());
[1690fa9]3747    m_PanAngle = p.angle;
[7a57dc7]3748    m_TiltAngle = -p.tilt_angle; // FIXME: nasty reversed sense (and above)
[08253d9]3749    SetRotation(m_PanAngle, m_TiltAngle);
[1690fa9]3750    SetScale(p.scale);
3751    ForceRefresh();
3752}
3753
[128fac4]3754void GfxCore::PlayPres(double speed, bool change_speed) {
3755    if (!change_speed || presentation_mode == 0) {
3756        if (speed == 0.0) {
3757            presentation_mode = 0;
3758            return;
3759        }
3760        presentation_mode = PLAYING;
3761        next_mark = m_Parent->GetPresMark(MARK_FIRST);
3762        SetView(next_mark);
3763        next_mark_time = 0; // There already!
3764        this_mark_total = 0;
3765        pres_reverse = (speed < 0);
3766    }
3767
[d67450e]3768    if (change_speed) pres_speed = speed;
3769
[128fac4]3770    if (speed != 0.0) {
3771        bool new_pres_reverse = (speed < 0);
3772        if (new_pres_reverse != pres_reverse) {
3773            pres_reverse = new_pres_reverse;
3774            if (pres_reverse) {
3775                next_mark = m_Parent->GetPresMark(MARK_PREV);
3776            } else {
3777                next_mark = m_Parent->GetPresMark(MARK_NEXT);
3778            }
3779            swap(this_mark_total, next_mark_time);
3780        }
3781    }
[1690fa9]3782}
[6a4cdcb6]3783
[da6c802]3784void GfxCore::SetColourBy(int colour_by) {
3785    m_ColourBy = colour_by;
3786    switch (colour_by) {
3787        case COLOUR_BY_DEPTH:
3788            AddQuad = &GfxCore::AddQuadrilateralDepth;
3789            AddPoly = &GfxCore::AddPolylineDepth;
3790            break;
[d4650b3]3791        case COLOUR_BY_DATE:
3792            AddQuad = &GfxCore::AddQuadrilateralDate;
3793            AddPoly = &GfxCore::AddPolylineDate;
3794            break;
[c61aa79]3795        case COLOUR_BY_ERROR:
3796            AddQuad = &GfxCore::AddQuadrilateralError;
3797            AddPoly = &GfxCore::AddPolylineError;
3798            break;
[cc9e2c65]3799        case COLOUR_BY_GRADIENT:
3800            AddQuad = &GfxCore::AddQuadrilateralGradient;
3801            AddPoly = &GfxCore::AddPolylineGradient;
3802            break;
[af50685]3803        case COLOUR_BY_LENGTH:
3804            AddQuad = &GfxCore::AddQuadrilateralLength;
3805            AddPoly = &GfxCore::AddPolylineLength;
3806            break;
[da6c802]3807        default: // case COLOUR_BY_NONE:
3808            AddQuad = &GfxCore::AddQuadrilateral;
3809            AddPoly = &GfxCore::AddPolyline;
3810            break;
3811    }
3812
[d2fcc9b]3813    InvalidateList(LIST_UNDERGROUND_LEGS);
[c61aa79]3814    InvalidateList(LIST_SURFACE_LEGS);
[d2fcc9b]3815    InvalidateList(LIST_TUBES);
[da6c802]3816
3817    ForceRefresh();
3818}
3819
[6a4cdcb6]3820bool GfxCore::ExportMovie(const wxString & fnm)
3821{
[8f9bade6]3822    FILE* fh = wxFopen(fnm.fn_str(), wxT("wb"));
[f4e4b56]3823    if (fh == NULL) {
3824        wxGetApp().ReportError(wxString::Format(wmsg(/*Failed to open output file “%s”*/47), fnm.c_str()));
3825        return false;
3826    }
3827
3828    wxString ext;
3829    wxFileName::SplitPath(fnm, NULL, NULL, NULL, &ext, wxPATH_NATIVE);
3830
[6a4cdcb6]3831    int width;
3832    int height;
3833    GetSize(&width, &height);
[028829f]3834    // Round up to next multiple of 2 (required by ffmpeg).
3835    width += (width & 1);
[6a4cdcb6]3836    height += (height & 1);
3837
[75d4a2b]3838    movie = new MovieMaker();
[6a4cdcb6]3839
[f4e4b56]3840    // movie takes ownership of fh.
3841    if (!movie->Open(fh, ext.utf8_str(), width, height)) {
[091069f]3842        wxGetApp().ReportError(wxString(movie->get_error_string(), wxConvUTF8));
[75d4a2b]3843        delete movie;
[81f1266]3844        movie = NULL;
[6a4cdcb6]3845        return false;
3846    }
[f433fda]3847
[d10d369]3848    PlayPres(1);
[6a4cdcb6]3849    return true;
3850}
[223f1ad]3851
[ce403f1]3852void
3853GfxCore::OnPrint(const wxString &filename, const wxString &title,
[4ed8154]3854                 const wxString &datestamp, time_t datestamp_numeric,
[6d3938b]3855                 const wxString &cs_proj,
[4ed8154]3856                 bool close_after_print)
[ce403f1]3857{
3858    svxPrintDlg * p;
[6d3938b]3859    p = new svxPrintDlg(m_Parent, filename, title, cs_proj,
[f10cf8f]3860                        datestamp, datestamp_numeric,
[ce403f1]3861                        m_PanAngle, m_TiltAngle,
[ddcf585]3862                        m_Names, m_Crosses, m_Legs, m_Surface, m_Splays,
3863                        m_Tubes, m_Entrances, m_FixedPts, m_ExportedPts,
[4ed8154]3864                        true, close_after_print);
[6d1bc83]3865    p->Show(true);
[ce403f1]3866}
3867
[5940815]3868void
[70462c8]3869GfxCore::OnExport(const wxString &filename, const wxString &title,
[6d3938b]3870                  const wxString &datestamp, time_t datestamp_numeric,
3871                  const wxString &cs_proj)
[223f1ad]3872{
[384534c]3873    // Fill in "right_bearing" for each cross-section.
3874    list<vector<XSect> >::iterator trav = m_Parent->tubes_begin();
3875    list<vector<XSect> >::iterator tend = m_Parent->tubes_end();
3876    while (trav != tend) {
3877        SkinPassage(*trav, false);
3878        ++trav;
3879    }
3880
[5940815]3881    svxPrintDlg * p;
[6d3938b]3882    p = new svxPrintDlg(m_Parent, filename, title, cs_proj,
[f10cf8f]3883                        datestamp, datestamp_numeric,
[5940815]3884                        m_PanAngle, m_TiltAngle,
[ddcf585]3885                        m_Names, m_Crosses, m_Legs, m_Surface, m_Splays,
3886                        m_Tubes, m_Entrances, m_FixedPts, m_ExportedPts,
[5940815]3887                        false);
[6d1bc83]3888    p->Show(true);
[223f1ad]3889}
[e2c1671]3890
3891static wxCursor
3892make_cursor(const unsigned char * bits, const unsigned char * mask,
3893            int hotx, int hoty)
3894{
[b72f4b5]3895#if defined __WXMSW__ || defined __WXMAC__
[60adbce]3896# ifdef __WXMAC__
3897    // The default Mac cursor is black with a white edge, so
3898    // invert our custom cursors to match.
3899    char b[128];
3900    for (int i = 0; i < 128; ++i)
3901        b[i] = bits[i] ^ 0xff;
3902# else
3903    const char * b = reinterpret_cast<const char *>(bits);
3904# endif
3905    wxBitmap cursor_bitmap(b, 32, 32);
[7f3fe6d]3906    wxBitmap mask_bitmap(reinterpret_cast<const char *>(mask), 32, 32);
[4dc4384]3907    cursor_bitmap.SetMask(new wxMask(mask_bitmap, *wxWHITE));
[e2c1671]3908    wxImage cursor_image = cursor_bitmap.ConvertToImage();
3909    cursor_image.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_X, hotx);
3910    cursor_image.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y, hoty);
[7f3fe6d]3911    return wxCursor(cursor_image);
[e2c1671]3912#else
3913    return wxCursor((const char *)bits, 32, 32, hotx, hoty,
[4dc4384]3914                    (const char *)mask, wxBLACK, wxWHITE);
[e2c1671]3915#endif
3916}
3917
3918const
3919#include "hand.xbm"
3920const
3921#include "handmask.xbm"
3922
3923const
3924#include "brotate.xbm"
3925const
3926#include "brotatemask.xbm"
3927
3928const
3929#include "vrotate.xbm"
3930const
3931#include "vrotatemask.xbm"
3932
3933const
3934#include "rotate.xbm"
3935const
3936#include "rotatemask.xbm"
3937
[ecf2d23]3938const
3939#include "rotatezoom.xbm"
3940const
3941#include "rotatezoommask.xbm"
3942
[e2c1671]3943void
[242cb07]3944GfxCore::UpdateCursor(GfxCore::cursor new_cursor)
[e2c1671]3945{
3946    // Check if we're already showing that cursor.
3947    if (current_cursor == new_cursor) return;
3948
3949    current_cursor = new_cursor;
3950    switch (current_cursor) {
3951        case GfxCore::CURSOR_DEFAULT:
3952            GLACanvas::SetCursor(wxNullCursor);
3953            break;
3954        case GfxCore::CURSOR_POINTING_HAND:
3955            GLACanvas::SetCursor(wxCursor(wxCURSOR_HAND));
3956            break;
3957        case GfxCore::CURSOR_DRAGGING_HAND:
3958            GLACanvas::SetCursor(make_cursor(hand_bits, handmask_bits, 12, 18));
3959            break;
3960        case GfxCore::CURSOR_HORIZONTAL_RESIZE:
3961            GLACanvas::SetCursor(wxCursor(wxCURSOR_SIZEWE));
3962            break;
3963        case GfxCore::CURSOR_ROTATE_HORIZONTALLY:
3964            GLACanvas::SetCursor(make_cursor(rotate_bits, rotatemask_bits, 15, 15));
3965            break;
3966        case GfxCore::CURSOR_ROTATE_VERTICALLY:
3967            GLACanvas::SetCursor(make_cursor(vrotate_bits, vrotatemask_bits, 15, 15));
3968            break;
3969        case GfxCore::CURSOR_ROTATE_EITHER_WAY:
3970            GLACanvas::SetCursor(make_cursor(brotate_bits, brotatemask_bits, 15, 15));
3971            break;
3972        case GfxCore::CURSOR_ZOOM:
3973            GLACanvas::SetCursor(wxCursor(wxCURSOR_MAGNIFIER));
3974            break;
[ecf2d23]3975        case GfxCore::CURSOR_ZOOM_ROTATE:
3976            GLACanvas::SetCursor(make_cursor(rotatezoom_bits, rotatezoommask_bits, 15, 15));
3977            break;
[e2c1671]3978    }
3979}
[6b061db]3980
3981bool GfxCore::MeasuringLineActive() const
3982{
3983    if (Animating()) return false;
[381ae6e]3984    return HereIsReal() || m_there;
[6b061db]3985}
[acdb8aa]3986
3987bool GfxCore::HandleRClick(wxPoint point)
3988{
3989    if (PointWithinCompass(point)) {
3990        // Pop up menu.
3991        wxMenu menu;
[736f7df]3992        /* TRANSLATORS: View *looking* North */
[055bfc58]3993        menu.Append(menu_ORIENT_MOVE_NORTH, wmsg(/*View &North*/240));
[736f7df]3994        /* TRANSLATORS: View *looking* East */
[055bfc58]3995        menu.Append(menu_ORIENT_MOVE_EAST, wmsg(/*View &East*/241));
[736f7df]3996        /* TRANSLATORS: View *looking* South */
[055bfc58]3997        menu.Append(menu_ORIENT_MOVE_SOUTH, wmsg(/*View &South*/242));
[736f7df]3998        /* TRANSLATORS: View *looking* West */
[055bfc58]3999        menu.Append(menu_ORIENT_MOVE_WEST, wmsg(/*View &West*/243));
4000        menu.AppendSeparator();
[736f7df]4001        /* TRANSLATORS: Menu item which turns off the "north arrow" in aven. */
[4d2301e]4002        menu.AppendCheckItem(menu_IND_COMPASS, wmsg(/*&Hide Compass*/387));
[0b8c321]4003        /* TRANSLATORS: tickable menu item in View menu.
4004         *
4005         * Degrees are the angular measurement where there are 360 in a full
4006         * circle. */
[acdb8aa]4007        menu.AppendCheckItem(menu_CTL_DEGREES, wmsg(/*&Degrees*/343));
[ee3e284]4008        menu.Connect(wxEVT_COMMAND_MENU_SELECTED, (wxObjectEventFunction)&wxEvtHandler::ProcessEvent, NULL, m_Parent->GetEventHandler());
[acdb8aa]4009        PopupMenu(&menu);
4010        return true;
4011    }
4012
4013    if (PointWithinClino(point)) {
4014        // Pop up menu.
4015        wxMenu menu;
[055bfc58]4016        menu.Append(menu_ORIENT_PLAN, wmsg(/*&Plan View*/248));
4017        menu.Append(menu_ORIENT_ELEVATION, wmsg(/*Ele&vation*/249));
4018        menu.AppendSeparator();
[736f7df]4019        /* TRANSLATORS: Menu item which turns off the tilt indicator in aven. */
[acdb8aa]4020        menu.AppendCheckItem(menu_IND_CLINO, wmsg(/*&Hide Clino*/384));
[736f7df]4021        /* TRANSLATORS: tickable menu item in View menu.
4022         *
4023         * Degrees are the angular measurement where there are 360 in a full
4024         * circle. */
[acdb8aa]4025        menu.AppendCheckItem(menu_CTL_DEGREES, wmsg(/*&Degrees*/343));
[736f7df]4026        /* TRANSLATORS: tickable menu item in View menu.
4027         *
4028         * Show the tilt of the survey as a percentage gradient (100% = 45
4029         * degrees = 50 grad). */
[d171c0c]4030        menu.AppendCheckItem(menu_CTL_PERCENT, wmsg(/*&Percent*/430));
[ee3e284]4031        menu.Connect(wxEVT_COMMAND_MENU_SELECTED, (wxObjectEventFunction)&wxEvtHandler::ProcessEvent, NULL, m_Parent->GetEventHandler());
4032        PopupMenu(&menu);
[acdb8aa]4033        return true;
4034    }
4035
4036    if (PointWithinScaleBar(point)) {
4037        // Pop up menu.
4038        wxMenu menu;
[736f7df]4039        /* TRANSLATORS: Menu item which turns off the scale bar in aven. */
[acdb8aa]4040        menu.AppendCheckItem(menu_IND_SCALE_BAR, wmsg(/*&Hide scale bar*/385));
[0b8c321]4041        /* TRANSLATORS: tickable menu item in View menu.
4042         *
4043         * "Metric" here means metres, km, etc (rather than feet, miles, etc)
4044         */
[acdb8aa]4045        menu.AppendCheckItem(menu_CTL_METRIC, wmsg(/*&Metric*/342));
[ee3e284]4046        menu.Connect(wxEVT_COMMAND_MENU_SELECTED, (wxObjectEventFunction)&wxEvtHandler::ProcessEvent, NULL, m_Parent->GetEventHandler());
[acdb8aa]4047        PopupMenu(&menu);
4048        return true;
4049    }
4050
4051    if (PointWithinColourKey(point)) {
4052        // Pop up menu.
4053        wxMenu menu;
[46beda0]4054        menu.AppendCheckItem(menu_COLOUR_BY_DEPTH, wmsg(/*Colour by &Depth*/292));
4055        menu.AppendCheckItem(menu_COLOUR_BY_DATE, wmsg(/*Colour by D&ate*/293));
4056        menu.AppendCheckItem(menu_COLOUR_BY_ERROR, wmsg(/*Colour by &Error*/289));
4057        menu.AppendCheckItem(menu_COLOUR_BY_GRADIENT, wmsg(/*Colour by &Gradient*/85));
4058        menu.AppendCheckItem(menu_COLOUR_BY_LENGTH, wmsg(/*Colour by &Length*/82));
[d43fa84]4059        menu.AppendSeparator();
[736f7df]4060        /* TRANSLATORS: Menu item which turns off the colour key.
4061         * The "Colour Key" is the thing in aven showing which colour
4062         * corresponds to which depth, date, survey closure error, etc. */
[97ea48d]4063        menu.AppendCheckItem(menu_IND_COLOUR_KEY, wmsg(/*&Hide colour key*/386));
[391af6a]4064        if (m_ColourBy == COLOUR_BY_DEPTH || m_ColourBy == COLOUR_BY_LENGTH)
[d43fa84]4065            menu.AppendCheckItem(menu_CTL_METRIC, wmsg(/*&Metric*/342));
[cc9e2c65]4066        else if (m_ColourBy == COLOUR_BY_GRADIENT)
4067            menu.AppendCheckItem(menu_CTL_DEGREES, wmsg(/*&Degrees*/343));
[ee3e284]4068        menu.Connect(wxEVT_COMMAND_MENU_SELECTED, (wxObjectEventFunction)&wxEvtHandler::ProcessEvent, NULL, m_Parent->GetEventHandler());
[acdb8aa]4069        PopupMenu(&menu);
4070        return true;
4071    }
4072
4073    return false;
4074}
[dd6af8b]4075
4076void GfxCore::SetZoomBox(wxPoint p1, wxPoint p2, bool centred, bool aspect)
4077{
4078    if (centred) {
4079        p1.x = p2.x + (p1.x - p2.x) * 2;
4080        p1.y = p2.y + (p1.y - p2.y) * 2;
4081    }
4082    if (aspect) {
4083#if 0 // FIXME: This needs more work.
4084        int sx = GetXSize();
4085        int sy = GetYSize();
4086        int dx = p1.x - p2.x;
4087        int dy = p1.y - p2.y;
4088        int dy_new = dx * sy / sx;
4089        if (abs(dy_new) >= abs(dy)) {
4090            p1.y += (dy_new - dy) / 2;
4091            p2.y -= (dy_new - dy) / 2;
4092        } else {
4093            int dx_new = dy * sx / sy;
4094            p1.x += (dx_new - dx) / 2;
4095            p2.x -= (dx_new - dx) / 2;
4096        }
4097#endif
4098    }
4099    zoombox.set(p1, p2);
4100    ForceRefresh();
4101}
4102
4103void GfxCore::ZoomBoxGo()
4104{
4105    if (!zoombox.active()) return;
4106
4107    int width = GetXSize();
4108    int height = GetYSize();
4109
4110    TranslateCave(-0.5 * (zoombox.x1 + zoombox.x2 - width),
4111                  -0.5 * (zoombox.y1 + zoombox.y2 - height));
4112    int box_w = abs(zoombox.x1 - zoombox.x2);
4113    int box_h = abs(zoombox.y1 - zoombox.y2);
4114
4115    double factor = min(double(width) / box_w, double(height) / box_h);
4116
4117    zoombox.unset();
4118
4119    SetScale(GetScale() * factor);
4120}
Note: See TracBrowser for help on using the repository browser.