source: git/src/gfxcore.cc @ 69cacd1

RELEASE/1.2debug-cidebug-ci-sanitisersfaster-cavernloglog-selectstereostereo-2025walls-datawalls-data-hanging-as-warningwarn-only-for-hanging-survey
Last change on this file since 69cacd1 was f9ca87c, checked in by Olly Betts <olly@…>, 14 years ago

doc/TODO.htm,src/gfxcore.cc,src/gla-gl.cc,src/gla.h: Eliminate the
last few OpenGL calls from gfxcore.cc.

git-svn-id: file:///home/survex-svn/survex/trunk@3772 4b37db11-9a0c-4f06-9ece-9ab7cdaee568

  • Property mode set to 100644
File size: 80.1 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
[091069f]7//  Copyright (C) 2001-2003,2004,2005,2006,2007,2010,2011 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"
[5809313]34#include "gfxcore.h"
[137bf99]35#include "mainfrm.h"
[93c3f97]36#include "message.h"
[7a89dc2]37#include "useful.h"
[ce403f1]38#include "printwx.h"
[5876fcb]39#include "guicontrol.h"
[6a4cdcb6]40#include "moviemaker.h"
[8000d8f]41
42#include <wx/confbase.h>
43#include <wx/image.h>
[5809313]44
[db4b33c]45#define MAX(a, b) ((a) > (b) ? (a) : (b))
[003d953]46#define MAX3(a, b, c) ((a) > (b) ? MAX(a, c) : MAX(b, c))
[5809313]47
[3d00693]48// Values for m_SwitchingTo
49#define PLAN 1
50#define ELEVATION 2
[3ddd351]51#define NORTH 3
52#define EAST 4
53#define SOUTH 5
54#define WEST 6
[3d00693]55
[c61aa79]56// Any error value higher than this is clamped to this.
57#define MAX_ERROR 12.0
58
[86cdcf2]59// How many bins per letter height to use when working out non-overlapping
60// labels.
61const unsigned int QUANTISE_FACTOR = 2;
62
[0e69efe]63#include "avenpal.h"
64
[6606406]65static const int COMPASS_OFFSET_X = 60;
66static const int COMPASS_OFFSET_Y = 80;
67static const int INDICATOR_BOX_SIZE = 60;
[c300a04]68static const int INDICATOR_GAP = 2;
[6606406]69static const int INDICATOR_MARGIN = 5;
70static const int INDICATOR_OFFSET_X = 15;
[c300a04]71static const int INDICATOR_OFFSET_Y = 15;
[fe665c4]72static const int INDICATOR_RADIUS = INDICATOR_BOX_SIZE / 2 - INDICATOR_MARGIN;
[dde4fe7]73static const int CLINO_OFFSET_X = 6 + INDICATOR_OFFSET_X +
[429465a]74                                  INDICATOR_BOX_SIZE + INDICATOR_GAP;
[97ea48d]75static const int COLOUR_KEY_OFFSET_X = 16;
76static const int COLOUR_KEY_EXTRA_LEFT_MARGIN = 2;
77static const int COLOUR_KEY_BLOCK_WIDTH = 20;
78static const int COLOUR_KEY_BLOCK_HEIGHT = 16;
79static const int COLOUR_KEY_MARGIN = 6;
80static const int COLOUR_KEY_OFFSET_Y = 16 + COLOUR_KEY_MARGIN;
[b56df45]81static const int TICK_LENGTH = 4;
[42adb19]82static const int SCALE_BAR_OFFSET_X = 15;
83static const int SCALE_BAR_OFFSET_Y = 12;
84static const int SCALE_BAR_HEIGHT = 12;
[aa048c3]85
86static const gla_colour TEXT_COLOUR = col_GREEN;
87static const gla_colour HERE_COLOUR = col_WHITE;
88static const gla_colour NAME_COLOUR = col_GREEN;
[33b2094]89
[39e460c9]90#define HITTEST_SIZE 20
91
[dde4fe7]92// vector for lighting angle
93static const Vector3 light(.577, .577, .577);
94
[c6d95d8]95BEGIN_EVENT_TABLE(GfxCore, wxGLCanvas)
[5809313]96    EVT_PAINT(GfxCore::OnPaint)
97    EVT_LEFT_DOWN(GfxCore::OnLButtonDown)
98    EVT_LEFT_UP(GfxCore::OnLButtonUp)
99    EVT_MIDDLE_DOWN(GfxCore::OnMButtonDown)
100    EVT_MIDDLE_UP(GfxCore::OnMButtonUp)
101    EVT_RIGHT_DOWN(GfxCore::OnRButtonDown)
102    EVT_RIGHT_UP(GfxCore::OnRButtonUp)
[34d8d1a]103    EVT_MOUSEWHEEL(GfxCore::OnMouseWheel)
[5809313]104    EVT_MOTION(GfxCore::OnMouseMove)
[887c26e]105    EVT_LEAVE_WINDOW(GfxCore::OnLeaveWindow)
[5809313]106    EVT_SIZE(GfxCore::OnSize)
[a8e9fde]107    EVT_IDLE(GfxCore::OnIdle)
[4b1fc48]108    EVT_CHAR(GfxCore::OnKeyPress)
[5809313]109END_EVENT_TABLE()
110
[5876fcb]111GfxCore::GfxCore(MainFrm* parent, wxWindow* parent_win, GUIControl* control) :
[88707e0b]112    GLACanvas(parent_win, 100),
113    m_Scale(0.0),
114    m_ScaleBarWidth(0),
115    m_Control(control),
116    m_LabelGrid(NULL),
117    m_Parent(parent),
118    m_DoneFirstShow(false),
119    m_TiltAngle(0.0),
120    m_PanAngle(0.0),
121    m_Rotating(false),
122    m_RotationStep(0.0),
123    m_SwitchingTo(0),
124    m_Crosses(false),
125    m_Legs(true),
126    m_Names(false),
127    m_Scalebar(true),
[97ea48d]128    m_ColourKey(true),
[88707e0b]129    m_OverlappingNames(false),
130    m_Compass(true),
131    m_Clino(true),
132    m_Tubes(false),
133    m_XSize(0),
134    m_YSize(0),
135    m_ColourBy(COLOUR_BY_DEPTH),
136    m_HaveData(false),
137    m_MouseOutsideCompass(false),
138    m_MouseOutsideElev(false),
139    m_Surface(false),
140    m_Entrances(false),
141    m_FixedPts(false),
142    m_ExportedPts(false),
143    m_Grid(false),
144    m_BoundingBox(false),
145    m_Degrees(false),
146    m_Metric(false),
147    m_HitTestGridValid(false),
148    m_here_is_temporary(false),
149    presentation_mode(0),
150    pres_reverse(false),
151    pres_speed(0.0),
[75d4a2b]152    movie(NULL),
[88707e0b]153    current_cursor(GfxCore::CURSOR_DEFAULT)
[5809313]154{
[da6c802]155    AddQuad = &GfxCore::AddQuadrilateralDepth;
156    AddPoly = &GfxCore::AddPolylineDepth;
[5627cbb]157    wxConfigBase::Get()->Read(wxT("metric"), &m_Metric, true);
158    wxConfigBase::Get()->Read(wxT("degrees"), &m_Degrees, true);
[d67450e]159    m_here.Invalidate();
160    m_there.Invalidate();
[5809313]161
[39e460c9]162    // Initialise grid for hit testing.
[fa42426]163    m_PointGrid = new list<LabelInfo*>[HITTEST_SIZE * HITTEST_SIZE];
[0e69efe]164
[97ea48d]165    m_Pens = new GLAPen[NUM_COLOUR_BANDS + 1];
166    for (int pen = 0; pen < NUM_COLOUR_BANDS + 1; ++pen) {
[0e69efe]167        m_Pens[pen].SetColour(REDS[pen] / 255.0,
168                              GREENS[pen] / 255.0,
169                              BLUES[pen] / 255.0);
170    }
[5809313]171}
172
173GfxCore::~GfxCore()
174{
175    TryToFreeArrays();
[156dc16]176
[0e69efe]177    delete[] m_Pens;
[39e460c9]178    delete[] m_PointGrid;
[5809313]179}
180
181void GfxCore::TryToFreeArrays()
182{
183    // Free up any memory allocated for arrays.
184
[815eab2]185    if (m_LabelGrid) {
[9eb58d0]186        delete[] m_LabelGrid;
[815eab2]187        m_LabelGrid = NULL;
[5809313]188    }
189}
190
191//
192//  Initialisation methods
193//
194
[0c6bf5e8]195void GfxCore::Initialise(bool same_file)
[5809313]196{
197    // Initialise the view from the parent holding the survey data.
198
199    TryToFreeArrays();
200
[33b2094]201    m_DoneFirstShow = false;
202
[dfe4454c]203    m_HitTestGridValid = false;
[d35144d]204    m_here_is_temporary = true;
[d67450e]205    m_here.Invalidate();
206    m_there.Invalidate();
[dfe4454c]207
[d35144d]208    m_MouseOutsideCompass = m_MouseOutsideElev = false;
209
[0c6bf5e8]210    if (!same_file) {
211        // Apply default parameters unless reloading the same file.
212        DefaultParameters();
213
214        // Set the initial scale.
215        SetScale(1.0);
216    }
[9eb58d0]217
[125c9ea]218    switch (m_ColourBy) {
219        case COLOUR_BY_DEPTH:
[78c67a6]220            if (m_Parent->GetDepthExtent() == 0.0) {
[125c9ea]221                SetColourBy(COLOUR_BY_NONE);
222            }
223            break;
224        case COLOUR_BY_DATE:
[843ee7b]225            if (!HasDateInformation()) {
[125c9ea]226                SetColourBy(COLOUR_BY_NONE);
227            }
228            break;
[c61aa79]229        case COLOUR_BY_ERROR:
230            if (!m_Parent->HasErrorInformation()) {
231                SetColourBy(COLOUR_BY_NONE);
232            }
233            break;
[bd21214]234    }
235
[9eb58d0]236    m_HaveData = true;
[936a197]237
238    ForceRefresh();
[9eb58d0]239}
240
241void GfxCore::FirstShow()
242{
243    GLACanvas::FirstShow();
244
[8bd480e]245    const unsigned int quantise(GetFontSize() / QUANTISE_FACTOR);
[86cdcf2]246    list<LabelInfo*>::iterator pos = m_Parent->GetLabelsNC();
247    while (pos != m_Parent->GetLabelsNCEnd()) {
248        LabelInfo* label = *pos++;
249        // Calculate and set the label width for use when plotting
250        // none-overlapping labels.
251        int ext_x;
252        GLACanvas::GetTextExtent(label->GetText(), &ext_x, NULL);
[5a24583]253        label->set_width(unsigned(ext_x) / quantise + 1);
[86cdcf2]254    }
255
[9eb58d0]256    SetBackgroundColour(0.0, 0.0, 0.0);
[1690fa9]257
[c5fc8eb]258    // Set diameter of the viewing volume.
259    SetVolumeDiameter(sqrt(sqrd(m_Parent->GetXExtent()) +
260                           sqrd(m_Parent->GetYExtent()) +
261                           sqrd(m_Parent->GetZExtent())));
[9eb58d0]262
263    // Update our record of the client area size and centre.
264    GetClientSize(&m_XSize, &m_YSize);
265
[5809313]266    m_DoneFirstShow = true;
267}
268
269//
270//  Recalculating methods
271//
272
[cd6ea75]273void GfxCore::SetScale(Double scale)
[5047c53]274{
275    // Fill the plot data arrays with screen coordinates, scaling the survey
276    // to a particular absolute scale.
[421b7d2]277
[8b0d57f]278    if (scale < 0.05) {
279        scale = 0.05;
[8a03058]280    } else {
[429465a]281        if (scale > 1000.0) scale = 1000.0;
[5047c53]282    }
283
[5b7164d]284    m_Scale = scale;
[00a68e0]285    m_HitTestGridValid = false;
[c00c6713]286    if (m_here_is_temporary) SetHere();
[33b2094]287
288    GLACanvas::SetScale(scale);
[d9b3270]289}
290
[f433fda]291bool GfxCore::HasUndergroundLegs() const
292{
293    return m_Parent->HasUndergroundLegs();
294}
295
296bool GfxCore::HasSurfaceLegs() const
297{
298    return m_Parent->HasSurfaceLegs();
299}
300
[50e8979]301bool GfxCore::HasTubes() const
302{
303    return m_Parent->HasTubes();
304}
305
[d9b3270]306void GfxCore::UpdateBlobs()
307{
[d2fcc9b]308    InvalidateList(LIST_BLOBS);
[33b2094]309}
310
[cd6ea75]311//
[5876fcb]312//  Event handlers
[cd6ea75]313//
[5047c53]314
[887c26e]315void GfxCore::OnLeaveWindow(wxMouseEvent& event) {
316    SetHere();
317    ClearCoords();
318}
319
[5876fcb]320void GfxCore::OnIdle(wxIdleEvent& event)
321{
322    // Handle an idle event.
[b72f4b5]323    if (Animate()) {
324        ForceRefresh();
325        event.RequestMore();
326    }
[5809313]327}
328
[b4fe9fb]329void GfxCore::OnPaint(wxPaintEvent&)
[5809313]330{
331    // Redraw the window.
[b462168]332
333    // Get a graphics context.
[1b12b82]334    wxPaintDC dc(this);
[5809313]335
[8abf9bb]336    assert(GetContext());
337
[815eab2]338    if (m_HaveData) {
[01d91fd0]339        // Make sure we're initialised.
340        bool first_time = !m_DoneFirstShow;
341        if (first_time) {
342            FirstShow();
343        }
344
[58dfdd21]345        StartDrawing();
346
347        // Clear the background.
348        Clear();
349
[429465a]350        // Set up model transformation matrix.
351        SetDataTransform();
352
[9eb58d0]353        timer.Start(); // reset timer
[203d2a7]354
[429465a]355        if (m_Legs || m_Tubes) {
356            if (m_Tubes) {
[d67450e]357                EnableSmoothPolygons(true); // FIXME: allow false for wireframe view
[d2fcc9b]358                DrawList(LIST_TUBES);
[429465a]359                DisableSmoothPolygons();
360            }
[e4d40792]361
[50e8979]362            // Draw the underground legs.  Do this last so that anti-aliasing
[e4d40792]363            // works over polygons.
364            SetColour(col_GREEN);
[d2fcc9b]365            DrawList(LIST_UNDERGROUND_LEGS);
[429465a]366        }
[33b2094]367
[429465a]368        if (m_Surface) {
369            // Draw the surface legs.
[d2fcc9b]370            DrawList(LIST_SURFACE_LEGS);
[429465a]371        }
[bbc22ca]372
[db59b02]373        DrawList(LIST_BLOBS);
[6adffadf]374
[f4c5932]375        if (m_BoundingBox) {
376            DrawShadowedBoundingBox();
377        }
[429465a]378        if (m_Grid) {
379            // Draw the grid.
[37d7084]380            DrawList(LIST_GRID);
[429465a]381        }
[b13aee4]382
[86fe6e4]383        if (m_Crosses) {
384            DrawList(LIST_CROSSES);
385        }
386
[429465a]387        SetIndicatorTransform();
388
[6adffadf]389        // Draw station names.
390        if (m_Names /*&& !m_Control->MouseDown() && !Animating()*/) {
391            SetColour(NAME_COLOUR);
392
393            if (m_OverlappingNames) {
394                SimpleDrawNames();
395            } else {
396                NattyDrawNames();
397            }
398        }
399
[6b061db]400        if (MeasuringLineActive()) {
[429465a]401            // Draw "here" and "there".
[f6d8375]402            double hx, hy;
[aa048c3]403            SetColour(HERE_COLOUR);
[d67450e]404            if (m_here.IsValid()) {
[f6d8375]405                double dummy;
[d67450e]406                Transform(m_here, &hx, &hy, &dummy);
[570d62c3]407                if (!m_here_is_temporary) DrawRing(hx, hy);
[429465a]408            }
[d67450e]409            if (m_there.IsValid()) {
[f6d8375]410                double tx, ty;
411                double dummy;
[d67450e]412                Transform(m_there, &tx, &ty, &dummy);
413                if (m_here.IsValid()) {
[429465a]414                    BeginLines();
415                    PlaceIndicatorVertex(hx, hy);
416                    PlaceIndicatorVertex(tx, ty);
[42d23c5]417                    EndLines();
[429465a]418                }
[e633bb1]419                BeginBlobs();
[81aea4e]420                DrawBlob(tx, ty);
[e633bb1]421                EndBlobs();
[429465a]422            }
423        }
[f433fda]424
[429465a]425        // Draw indicators.
[6747314]426        //
427        // There's no advantage in generating an OpenGL list for the
428        // indicators since they change with almost every redraw (and
429        // sometimes several times between redraws).  This way we avoid
430        // the need to track when to update the indicator OpenGL list,
431        // and also avoid indicator update bugs when we don't quite get this
432        // right...
433        DrawIndicators();
[33b2094]434
[58dfdd21]435        FinishDrawing();
436
437        drawtime = timer.Time();
[5997ffdc]438    } else {
439        dc.SetBackground(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWFRAME));
440        dc.Clear();
[58dfdd21]441    }
[5809313]442}
443
[f4c5932]444void GfxCore::DrawBoundingBox()
445{
[d67450e]446    const Vector3 v = 0.5 * m_Parent->GetExtent();
[f4c5932]447
448    SetColour(col_BLUE);
449    EnableDashedLines();
450    BeginPolyline();
[d67450e]451    PlaceVertex(-v.GetX(), -v.GetY(), v.GetZ());
452    PlaceVertex(-v.GetX(), v.GetY(), v.GetZ());
453    PlaceVertex(v.GetX(), v.GetY(), v.GetZ());
454    PlaceVertex(v.GetX(), -v.GetY(), v.GetZ());
455    PlaceVertex(-v.GetX(), -v.GetY(), v.GetZ());
[f4c5932]456    EndPolyline();
457    BeginPolyline();
[d67450e]458    PlaceVertex(-v.GetX(), -v.GetY(), -v.GetZ());
459    PlaceVertex(-v.GetX(), v.GetY(), -v.GetZ());
460    PlaceVertex(v.GetX(), v.GetY(), -v.GetZ());
461    PlaceVertex(v.GetX(), -v.GetY(), -v.GetZ());
462    PlaceVertex(-v.GetX(), -v.GetY(), -v.GetZ());
[f4c5932]463    EndPolyline();
464    BeginLines();
[d67450e]465    PlaceVertex(-v.GetX(), -v.GetY(), v.GetZ());
466    PlaceVertex(-v.GetX(), -v.GetY(), -v.GetZ());
467    PlaceVertex(-v.GetX(), v.GetY(), v.GetZ());
468    PlaceVertex(-v.GetX(), v.GetY(), -v.GetZ());
469    PlaceVertex(v.GetX(), v.GetY(), v.GetZ());
470    PlaceVertex(v.GetX(), v.GetY(), -v.GetZ());
471    PlaceVertex(v.GetX(), -v.GetY(), v.GetZ());
472    PlaceVertex(v.GetX(), -v.GetY(), -v.GetZ());
[f4c5932]473    EndLines();
474    DisableDashedLines();
475}
476
477void GfxCore::DrawShadowedBoundingBox()
478{
[d67450e]479    const Vector3 v = 0.5 * m_Parent->GetExtent();
[f4c5932]480
[f7dae86]481    DrawBoundingBox();
482
[f9ca87c]483    PolygonOffset(true);
[f4c5932]484    SetColour(col_DARK_GREY);
485    BeginQuadrilaterals();
[d67450e]486    PlaceVertex(-v.GetX(), -v.GetY(), -v.GetZ());
487    PlaceVertex(-v.GetX(), v.GetY(), -v.GetZ());
488    PlaceVertex(v.GetX(), v.GetY(), -v.GetZ());
489    PlaceVertex(v.GetX(), -v.GetY(), -v.GetZ());
[f4c5932]490    EndQuadrilaterals();
[f9ca87c]491    PolygonOffset(false);
[f4c5932]492
[d2fcc9b]493    DrawList(LIST_SHADOW);
[f4c5932]494}
495
[c1cf79d]496void GfxCore::DrawGrid()
497{
498    // Draw the grid.
[156dc16]499    SetColour(col_RED);
[c1cf79d]500
501    // Calculate the extent of the survey, in metres across the screen plane.
[b81eee2]502    Double m_across_screen = SurveyUnitsAcrossViewport();
[c1cf79d]503    // Calculate the length of the scale bar in metres.
504    //--move this elsewhere
[c6d95d8]505    Double size_snap = pow(10.0, floor(log10(0.75 * m_across_screen)));
506    Double t = m_across_screen * 0.75 / size_snap;
[c1cf79d]507    if (t >= 5.0) {
[429465a]508        size_snap *= 5.0;
[c1cf79d]509    }
510    else if (t >= 2.0) {
[429465a]511        size_snap *= 2.0;
[c1cf79d]512    }
513
[d67450e]514    Double grid_size = size_snap * 0.1;
[c6d95d8]515    Double edge = grid_size * 2.0;
[d67450e]516    Double grid_z = -m_Parent->GetZExtent() * 0.5 - grid_size;
517    Double left = -m_Parent->GetXExtent() * 0.5 - edge;
518    Double right = m_Parent->GetXExtent() * 0.5 + edge;
519    Double bottom = -m_Parent->GetYExtent() * 0.5 - edge;
520    Double top = m_Parent->GetYExtent() * 0.5 + edge;
[c1cf79d]521    int count_x = (int) ceil((right - left) / grid_size);
522    int count_y = (int) ceil((top - bottom) / grid_size);
[c6d95d8]523    Double actual_right = left + count_x*grid_size;
524    Double actual_top = bottom + count_y*grid_size;
[c1cf79d]525
[b81eee2]526    BeginLines();
527
[c1cf79d]528    for (int xc = 0; xc <= count_x; xc++) {
[429465a]529        Double x = left + xc*grid_size;
[f433fda]530
[b81eee2]531        PlaceVertex(x, bottom, grid_z);
532        PlaceVertex(x, actual_top, grid_z);
[c1cf79d]533    }
534
535    for (int yc = 0; yc <= count_y; yc++) {
[429465a]536        Double y = bottom + yc*grid_size;
[b81eee2]537        PlaceVertex(left, y, grid_z);
538        PlaceVertex(actual_right, y, grid_z);
[c1cf79d]539    }
[b81eee2]540
541    EndLines();
[c1cf79d]542}
543
[1eeb55a]544int GfxCore::GetClinoOffset() const
[c300a04]545{
546    return m_Compass ? CLINO_OFFSET_X : INDICATOR_OFFSET_X;
547}
548
[fe665c4]549void GfxCore::DrawTick(int angle_cw)
[6606406]550{
[fe665c4]551    const Double theta = rad(angle_cw);
552    const wxCoord length1 = INDICATOR_RADIUS;
553    const wxCoord length0 = length1 + TICK_LENGTH;
554    wxCoord x0 = wxCoord(length0 * sin(theta));
555    wxCoord y0 = wxCoord(length0 * cos(theta));
556    wxCoord x1 = wxCoord(length1 * sin(theta));
557    wxCoord y1 = wxCoord(length1 * cos(theta));
[6606406]558
[fe665c4]559    PlaceIndicatorVertex(x0, y0);
560    PlaceIndicatorVertex(x1, y1);
[6606406]561}
562
[d67450e]563void GfxCore::DrawArrow(gla_colour col1, gla_colour col2) {
564    Vector3 p1(0, INDICATOR_RADIUS, 0);
565    Vector3 p2(INDICATOR_RADIUS/2, INDICATOR_RADIUS*-.866025404, 0); // 150deg
566    Vector3 p3(-INDICATOR_RADIUS/2, INDICATOR_RADIUS*-.866025404, 0); // 210deg
567    Vector3 pc(0, 0, 0);
568
569    DrawTriangle(col_LIGHT_GREY, col1, p2, p1, pc);
570    DrawTriangle(col_LIGHT_GREY, col2, p3, p1, pc);
571}
572
[fe665c4]573void GfxCore::DrawCompass() {
574    // Ticks.
575    BeginLines();
576    for (int angle = 315; angle > 0; angle -= 45) {
577        DrawTick(angle);
578    }
579    SetColour(col_GREEN);
580    DrawTick(0);
581    EndLines();
582
583    // Compass background.
584    DrawCircle(col_LIGHT_GREY_2, col_GREY, 0, 0, INDICATOR_RADIUS);
[6606406]585
[fe665c4]586    // Compass arrow.
[d67450e]587    DrawArrow(col_INDICATOR_1, col_INDICATOR_2);
[6606406]588}
589
[fe665c4]590// Draw the non-rotating background to the clino.
591void GfxCore::DrawClinoBack() {
592    BeginLines();
593    for (int angle = 0; angle <= 180; angle += 90) {
594        DrawTick(angle);
595    }
596
597    SetColour(col_GREY);
598    PlaceIndicatorVertex(0, INDICATOR_RADIUS);
599    PlaceIndicatorVertex(0, -INDICATOR_RADIUS);
600    PlaceIndicatorVertex(0, 0);
601    PlaceIndicatorVertex(INDICATOR_RADIUS, 0);
[b56df45]602
[fe665c4]603    EndLines();
604}
605
606void GfxCore::DrawClino() {
607    // Ticks.
608    SetColour(col_GREEN);
[33b2094]609    BeginLines();
[fe665c4]610    DrawTick(0);
[33b2094]611    EndLines();
[fe665c4]612
613    // Clino background.
614    DrawSemicircle(col_LIGHT_GREY_2, col_GREY, 0, 0, INDICATOR_RADIUS, 0);
615
616    // Elevation arrow.
[d67450e]617    DrawArrow(col_INDICATOR_2, col_INDICATOR_1);
[b56df45]618}
619
[6606406]620void GfxCore::Draw2dIndicators()
621{
[76dd228]622    // Draw the compass and elevation indicators.
623
624    const int centre_y = INDICATOR_BOX_SIZE / 2 + INDICATOR_OFFSET_Y;
625
[fe665c4]626    const int comp_centre_x = GetCompassXPosition();
[6606406]627
[eef68f9]628    if (m_Compass && !m_Parent->IsExtendedElevation()) {
[fe665c4]629        // If the user is dragging the compass with the pointer outside the
630        // compass, we snap to 45 degree multiples, and the ticks go white.
[76dd228]631        SetColour(m_MouseOutsideCompass ? col_WHITE : col_LIGHT_GREY_2);
[fe665c4]632        DrawList2D(LIST_COMPASS, comp_centre_x, centre_y, -m_PanAngle);
[c300a04]633    }
[76dd228]634
[fe665c4]635    const int elev_centre_x = GetClinoXPosition();
[76dd228]636
[eef68f9]637    if (m_Clino) {
[fe665c4]638        // If the user is dragging the clino with the pointer outside the
639        // clino, we snap to 90 degree multiples, and the ticks go white.
640        SetColour(m_MouseOutsideElev ? col_WHITE : col_LIGHT_GREY_2);
641        DrawList2D(LIST_CLINO_BACK, elev_centre_x, centre_y, 0);
642        DrawList2D(LIST_CLINO, elev_centre_x, centre_y, 90 + m_TiltAngle);
[c300a04]643    }
[6606406]644
[aa048c3]645    SetColour(TEXT_COLOUR);
[6606406]646
[21958ec]647    static int triple_zero_width = 0;
648    static int height = 0;
649    if (!triple_zero_width) {
[5627cbb]650        GetTextExtent(wxT("000"), &triple_zero_width, &height);
[21958ec]651    }
652    const int y_off = INDICATOR_OFFSET_Y + INDICATOR_BOX_SIZE + height / 2;
[421b7d2]653
[eef68f9]654    if (m_Compass && !m_Parent->IsExtendedElevation()) {
[21958ec]655        wxString str;
656        int value;
[429465a]657        if (m_Degrees) {
[21958ec]658            value = int(m_PanAngle);
[429465a]659        } else {
[21958ec]660            value = int(m_PanAngle * 200.0 / 180.0);
[f433fda]661        }
[5627cbb]662        str.Printf(wxT("%03d"), value);
[21958ec]663        DrawIndicatorText(comp_centre_x - triple_zero_width / 2, y_off, str);
664
[5627cbb]665        str = wmsg(/*Facing*/203);
[21958ec]666        int w;
[d92d282]667        GetTextExtent(str, &w, NULL);
[21958ec]668        DrawIndicatorText(comp_centre_x - w / 2, y_off + height, str);
[c300a04]669    }
670
[eef68f9]671    if (m_Clino) {
[429465a]672        int angle;
[21958ec]673        wxString str;
674        int width;
[429465a]675        if (m_Degrees) {
[21958ec]676            static int zero_zero_width = 0;
677            if (!zero_zero_width) {
[5627cbb]678                GetTextExtent(wxT("00"), &zero_zero_width, NULL);
[21958ec]679            }
680            width = zero_zero_width;
[e577f89]681            angle = int(-m_TiltAngle);
[5627cbb]682            str = angle ? wxString::Format(wxT("%+03d"), angle) : wxT("00");
[429465a]683        } else {
[21958ec]684            width = triple_zero_width;
[e577f89]685            angle = int(-m_TiltAngle * 200.0 / 180.0);
[5627cbb]686            str = angle ? wxString::Format(wxT("%+04d"), angle) : wxT("000");
[f433fda]687        }
[21958ec]688
689        // Adjust horizontal position so the left of the first digit is
690        // always in the same place.
691        int sign_offset = 0;
692        if (angle < 0) {
693            static int minus_width = 0;
694            if (!minus_width) {
[5627cbb]695                GetTextExtent(wxT("-"), &minus_width, NULL);
[21958ec]696            }
697            sign_offset = minus_width + 1;
698        } else if (angle > 0) {
699            static int plus_width = 0;
700            if (!plus_width) {
[5627cbb]701                GetTextExtent(wxT("+"), &plus_width, NULL);
[21958ec]702            }
703            sign_offset = plus_width + 1;
704        }
705        DrawIndicatorText(elev_centre_x - sign_offset - width / 2, y_off, str);
706
[5627cbb]707        str = wmsg(/*Elevation*/118);
[21958ec]708        int w;
[d92d282]709        GetTextExtent(str, &w, NULL);
[21958ec]710        DrawIndicatorText(elev_centre_x - w / 2, y_off + height, str);
[c300a04]711    }
[5809313]712}
713
714void GfxCore::NattyDrawNames()
715{
[84cab34]716    // Draw station names, without overlapping.
[f433fda]717
[d92d282]718    const unsigned int quantise(GetFontSize() / QUANTISE_FACTOR);
719    const unsigned int quantised_x = m_XSize / quantise;
720    const unsigned int quantised_y = m_YSize / quantise;
[84cab34]721    const size_t buffer_size = quantised_x * quantised_y;
[f433fda]722
[69463a0]723    if (!m_LabelGrid) m_LabelGrid = new char[buffer_size];
[5809313]724
[33b2094]725    memset((void*) m_LabelGrid, 0, buffer_size);
[156dc16]726
[33b2094]727    list<LabelInfo*>::const_iterator label = m_Parent->GetLabels();
[36c3285]728    for ( ; label != m_Parent->GetLabelsEnd(); ++label) {
[ece003f]729        if (!((m_Surface && (*label)->IsSurface()) ||
730              (m_Legs && (*label)->IsUnderground()) ||
731              (!(*label)->IsSurface() && !(*label)->IsUnderground()))) {
732            // if this station isn't to be displayed, skip to the next
733            // (last case is for stns with no legs attached)
734            continue;
735        }
736
[f6d8375]737        double x, y, z;
[f433fda]738
[d67450e]739        Transform(**label, &x, &y, &z);
[36c3285]740        // Check if the label is behind us (in perspective view).
[d92d282]741        if (z <= 0.0 || z >= 1.0) continue;
[421b7d2]742
[1eeb55a]743        // Apply a small shift so that translating the view doesn't make which
744        // labels are displayed change as the resulting twinkling effect is
745        // distracting.
[f6d8375]746        double tx, ty, tz;
[d67450e]747        Transform(Vector3(), &tx, &ty, &tz);
[429465a]748        tx -= floor(tx / quantise) * quantise;
749        ty -= floor(ty / quantise) * quantise;
[5809313]750
[f12e8cc]751        tx = x - tx;
752        if (tx < 0) continue;
753
754        ty = y - ty;
755        if (ty < 0) continue;
[421b7d2]756
[f12e8cc]757        unsigned int iy = unsigned(ty) / quantise;
[d92d282]758        if (iy >= quantised_y) continue;
[5a24583]759        unsigned int width = (*label)->get_width();
[f12e8cc]760        unsigned int ix = unsigned(tx) / quantise;
761        if (ix + width >= quantised_x) continue;
[33b2094]762
[f12e8cc]763        char * test = m_LabelGrid + ix + iy * quantised_x;
[d92d282]764        if (memchr(test, 1, width)) continue;
[33b2094]765
[8bd480e]766        x += 3;
767        y -= GetFontSize() / 2;
[d92d282]768        DrawIndicatorText((int)x, (int)y, (*label)->GetText());
769
770        if (iy > QUANTISE_FACTOR) iy = QUANTISE_FACTOR;
771        test -= quantised_x * iy;
[f12e8cc]772        iy += 4;
773        while (--iy && test < m_LabelGrid + buffer_size) {
[d92d282]774            memset(test, 1, width);
775            test += quantised_x;
[429465a]776        }
[84cab34]777    }
[5809313]778}
779
780void GfxCore::SimpleDrawNames()
781{
[a8aedf4]782    // Draw all station names, without worrying about overlaps
[d5de678]783    list<LabelInfo*>::const_iterator label = m_Parent->GetLabels();
[01d91fd0]784    for ( ; label != m_Parent->GetLabelsEnd(); ++label) {
[ece003f]785        if (!((m_Surface && (*label)->IsSurface()) ||
786              (m_Legs && (*label)->IsUnderground()) ||
787              (!(*label)->IsSurface() && !(*label)->IsUnderground()))) {
788            // if this station isn't to be displayed, skip to the next
789            // (last case is for stns with no legs attached)
790            continue;
791        }
792
[f6d8375]793        double x, y, z;
[d67450e]794        Transform(**label, &x, &y, &z);
[d92d282]795
796        // Check if the label is behind us (in perspective view).
797        if (z <= 0) continue;
798
[8bd480e]799        x += 3;
800        y -= GetFontSize() / 2;
[d92d282]801        DrawIndicatorText((int)x, (int)y, (*label)->GetText());
[84cab34]802    }
[5809313]803}
804
805void GfxCore::DrawDepthbar()
806{
[76dd228]807    const int total_block_height =
[97ea48d]808        COLOUR_KEY_BLOCK_HEIGHT * (GetNumColourBands() - 1);
809    const int top = -(total_block_height + COLOUR_KEY_OFFSET_Y);
[1eeb55a]810    int size = 0;
[c300a04]811
[97ea48d]812    wxString* strs = new wxString[GetNumColourBands()];
[63dc4eb]813    int band;
[97ea48d]814    for (band = 0; band < GetNumColourBands(); ++band) {
[78c67a6]815        Double z = m_Parent->GetDepthMin() + m_Parent->GetOffset().GetZ() +
[97ea48d]816                   m_Parent->GetDepthExtent() * band / (GetNumColourBands() - 1);
[429465a]817        strs[band] = FormatLength(z, false);
[203d2a7]818
[a74b014]819        int x;
820        GetTextExtent(strs[band], &x, NULL);
[429465a]821        if (x > size) size = x;
[c300a04]822    }
823
[97ea48d]824    int left = -COLOUR_KEY_OFFSET_X - COLOUR_KEY_BLOCK_WIDTH
825                - COLOUR_KEY_MARGIN - size;
[c300a04]826
[aa048c3]827    DrawRectangle(col_BLACK, col_DARK_GREY,
[97ea48d]828                  left - COLOUR_KEY_MARGIN - COLOUR_KEY_EXTRA_LEFT_MARGIN,
829                  top - COLOUR_KEY_MARGIN * 2,
830                  COLOUR_KEY_BLOCK_WIDTH + size + COLOUR_KEY_MARGIN * 3 +
831                      COLOUR_KEY_EXTRA_LEFT_MARGIN,
832                  total_block_height + COLOUR_KEY_MARGIN*4);
[5809313]833
[76dd228]834    int y = top;
[97ea48d]835    for (band = 0; band < GetNumColourBands() - 1; ++band) {
[76dd228]836        DrawShadedRectangle(GetPen(band), GetPen(band + 1), left, y,
[97ea48d]837                            COLOUR_KEY_BLOCK_WIDTH, COLOUR_KEY_BLOCK_HEIGHT);
838        y += COLOUR_KEY_BLOCK_HEIGHT;
[76dd228]839    }
[dde4fe7]840
[a74b014]841    y = top - GetFontSize() / 2;
[97ea48d]842    left += COLOUR_KEY_BLOCK_WIDTH + 5;
[5809313]843
[76dd228]844    SetColour(TEXT_COLOUR);
[97ea48d]845    for (band = 0; band < GetNumColourBands(); ++band) {
[76dd228]846        DrawIndicatorText(left, y, strs[band]);
[97ea48d]847        y += COLOUR_KEY_BLOCK_HEIGHT;
[84cab34]848    }
[c300a04]849
850    delete[] strs;
[5809313]851}
852
[d4650b3]853void GfxCore::DrawDatebar()
854{
[2043961]855    int total_block_height;
856    int num_bands;
857    if (m_Parent->GetDateExtent() == 0) {
858        num_bands = 1;
[97ea48d]859        total_block_height = COLOUR_KEY_BLOCK_HEIGHT;
[2043961]860    } else {
[97ea48d]861        num_bands = GetNumColourBands();
862        total_block_height = COLOUR_KEY_BLOCK_HEIGHT * (num_bands - 1);
[2043961]863    }
[a74b014]864    if (!m_Parent->HasCompleteDateInfo())
[97ea48d]865        total_block_height += COLOUR_KEY_BLOCK_HEIGHT * 2;
866    const int top = -(total_block_height + COLOUR_KEY_OFFSET_Y);
[a74b014]867
[d4650b3]868    int size = 0;
[a74b014]869    if (!m_Parent->HasCompleteDateInfo()) {
[fbc1d32]870        GetTextExtent(wmsg(/*Undated*/221), &size, NULL);
[a74b014]871    }
[d4650b3]872
[2043961]873    wxString* strs = new wxString[num_bands];
[d4650b3]874    int band;
[2043961]875    for (band = 0; band < num_bands; ++band) {
[1ee204e]876        int y, m, d;
877        int days = m_Parent->GetDateMin();
[2043961]878        if (band)
879            days += m_Parent->GetDateExtent() * band / (num_bands - 1);
[1ee204e]880        ymd_from_days_since_1900(days, &y, &m, &d);
[2043961]881        strs[band].Printf(wxT("%04d-%02d-%02d"), y, m, d);
[d4650b3]882
[a74b014]883        int x;
884        GetTextExtent(strs[band], &x, NULL);
[d4650b3]885        if (x > size) size = x;
886    }
887
[97ea48d]888    int left = -COLOUR_KEY_OFFSET_X - COLOUR_KEY_BLOCK_WIDTH
889                - COLOUR_KEY_MARGIN - size;
[d4650b3]890
891    DrawRectangle(col_BLACK, col_DARK_GREY,
[97ea48d]892                  left - COLOUR_KEY_MARGIN - COLOUR_KEY_EXTRA_LEFT_MARGIN,
893                  top - COLOUR_KEY_MARGIN * 2,
894                  COLOUR_KEY_BLOCK_WIDTH + size + COLOUR_KEY_MARGIN * 3 +
895                      COLOUR_KEY_EXTRA_LEFT_MARGIN,
896                  total_block_height + COLOUR_KEY_MARGIN*4);
[d4650b3]897
898    int y = top;
[a74b014]899
900    if (!m_Parent->HasCompleteDateInfo()) {
901        DrawShadedRectangle(GetSurfacePen(), GetSurfacePen(), left, y,
[97ea48d]902                            COLOUR_KEY_BLOCK_WIDTH, COLOUR_KEY_BLOCK_HEIGHT);
903        y += COLOUR_KEY_BLOCK_HEIGHT * 2;
[a74b014]904    }
905
[2043961]906    if (num_bands == 1) {
907        DrawShadedRectangle(GetPen(0), GetPen(0), left, y,
[97ea48d]908                            COLOUR_KEY_BLOCK_WIDTH, COLOUR_KEY_BLOCK_HEIGHT);
[2043961]909    } else {
910        for (band = 0; band < num_bands - 1; ++band) {
911            DrawShadedRectangle(GetPen(band), GetPen(band + 1), left, y,
[97ea48d]912                                COLOUR_KEY_BLOCK_WIDTH, COLOUR_KEY_BLOCK_HEIGHT);
913            y += COLOUR_KEY_BLOCK_HEIGHT;
[2043961]914        }
[d4650b3]915    }
916
[a74b014]917    y = top - GetFontSize() / 2;
[97ea48d]918    left += COLOUR_KEY_BLOCK_WIDTH + 5;
[d4650b3]919
920    SetColour(TEXT_COLOUR);
[a74b014]921
922    if (!m_Parent->HasCompleteDateInfo()) {
[97ea48d]923        y += COLOUR_KEY_BLOCK_HEIGHT / 2;
[fbc1d32]924        DrawIndicatorText(left, y, wmsg(/*Undated*/221));
[97ea48d]925        y += COLOUR_KEY_BLOCK_HEIGHT * 2 - COLOUR_KEY_BLOCK_HEIGHT / 2;
[a74b014]926    }
927
[2043961]928    if (num_bands == 1) {
[97ea48d]929        y += COLOUR_KEY_BLOCK_HEIGHT / 2;
[2043961]930        DrawIndicatorText(left, y, strs[0]);
931    } else {
932        for (band = 0; band < num_bands; ++band) {
933            DrawIndicatorText(left, y, strs[band]);
[97ea48d]934            y += COLOUR_KEY_BLOCK_HEIGHT;
[2043961]935        }
[d4650b3]936    }
937
938    delete[] strs;
939}
940
[c61aa79]941void GfxCore::DrawErrorbar()
942{
[97ea48d]943    int total_block_height = COLOUR_KEY_BLOCK_HEIGHT * (GetNumColourBands() - 1);
[fbc1d32]944    // Always show the "Not in loop" legend for now (FIXME).
[97ea48d]945    total_block_height += COLOUR_KEY_BLOCK_HEIGHT * 2;
946    const int top = -(total_block_height + COLOUR_KEY_OFFSET_Y);
[a74b014]947
[c61aa79]948    int size = 0;
[fbc1d32]949    GetTextExtent(wmsg(/*Not in loop*/290), &size, NULL);
[c61aa79]950
[97ea48d]951    wxString* strs = new wxString[GetNumColourBands()];
[c61aa79]952    int band;
[97ea48d]953    for (band = 0; band < GetNumColourBands(); ++band) {
954        double E = MAX_ERROR * band / (GetNumColourBands() - 1);
[5627cbb]955        strs[band].Printf(wxT("%.2f"), E);
[c61aa79]956
[a74b014]957        int x;
958        GetTextExtent(strs[band], &x, NULL);
[c61aa79]959        if (x > size) size = x;
960    }
961
[97ea48d]962    int left = -COLOUR_KEY_OFFSET_X - COLOUR_KEY_BLOCK_WIDTH
963                - COLOUR_KEY_MARGIN - size;
[c61aa79]964
965    DrawRectangle(col_BLACK, col_DARK_GREY,
[97ea48d]966                  left - COLOUR_KEY_MARGIN - COLOUR_KEY_EXTRA_LEFT_MARGIN,
967                  top - COLOUR_KEY_MARGIN * 2,
968                  COLOUR_KEY_BLOCK_WIDTH + size + COLOUR_KEY_MARGIN * 3 +
969                      COLOUR_KEY_EXTRA_LEFT_MARGIN,
970                  total_block_height + COLOUR_KEY_MARGIN*4);
[c61aa79]971
972    int y = top;
[a74b014]973
974    DrawShadedRectangle(GetSurfacePen(), GetSurfacePen(), left, y,
[97ea48d]975            COLOUR_KEY_BLOCK_WIDTH, COLOUR_KEY_BLOCK_HEIGHT);
976    y += COLOUR_KEY_BLOCK_HEIGHT * 2;
[a74b014]977
[97ea48d]978    for (band = 0; band < GetNumColourBands() - 1; ++band) {
[c61aa79]979        DrawShadedRectangle(GetPen(band), GetPen(band + 1), left, y,
[97ea48d]980                            COLOUR_KEY_BLOCK_WIDTH, COLOUR_KEY_BLOCK_HEIGHT);
981        y += COLOUR_KEY_BLOCK_HEIGHT;
[c61aa79]982    }
983
[a74b014]984    y = top - GetFontSize() / 2;
[97ea48d]985    left += COLOUR_KEY_BLOCK_WIDTH + 5;
[c61aa79]986
987    SetColour(TEXT_COLOUR);
[a74b014]988
[97ea48d]989    y += COLOUR_KEY_BLOCK_HEIGHT / 2;
[fbc1d32]990    DrawIndicatorText(left, y, wmsg(/*Not in loop*/290));
[97ea48d]991    y += COLOUR_KEY_BLOCK_HEIGHT * 2 - COLOUR_KEY_BLOCK_HEIGHT / 2;
[a74b014]992
[97ea48d]993    for (band = 0; band < GetNumColourBands(); ++band) {
[c61aa79]994        DrawIndicatorText(left, y, strs[band]);
[97ea48d]995        y += COLOUR_KEY_BLOCK_HEIGHT;
[c61aa79]996    }
997
998    delete[] strs;
999}
1000
[c6d95d8]1001wxString GfxCore::FormatLength(Double size_snap, bool scalebar)
[ac537e9]1002{
1003    wxString str;
[1eb2590]1004    bool negative = (size_snap < 0.0);
1005
1006    if (negative) {
[429465a]1007        size_snap = -size_snap;
[1eb2590]1008    }
[ac537e9]1009
1010    if (size_snap == 0.0) {
[5627cbb]1011        str = wxT("0");
[7a89dc2]1012    } else if (m_Metric) {
[ac537e9]1013#ifdef SILLY_UNITS
[429465a]1014        if (size_snap < 1e-12) {
[5627cbb]1015            str.Printf(wxT("%.3gpm"), size_snap * 1e12);
[429465a]1016        } else if (size_snap < 1e-9) {
[5627cbb]1017            str.Printf(wxT("%.fpm"), size_snap * 1e12);
[429465a]1018        } else if (size_snap < 1e-6) {
[5627cbb]1019            str.Printf(wxT("%.fnm"), size_snap * 1e9);
[429465a]1020        } else if (size_snap < 1e-3) {
[5627cbb]1021            str.Printf(wxT("%.fum"), size_snap * 1e6);
[ac537e9]1022#else
[429465a]1023        if (size_snap < 1e-3) {
[5627cbb]1024            str.Printf(wxT("%.3gmm"), size_snap * 1e3);
[421b7d2]1025#endif
[429465a]1026        } else if (size_snap < 1e-2) {
[5627cbb]1027            str.Printf(wxT("%.fmm"), size_snap * 1e3);
[429465a]1028        } else if (size_snap < 1.0) {
[5627cbb]1029            str.Printf(wxT("%.fcm"), size_snap * 100.0);
[429465a]1030        } else if (size_snap < 1e3) {
[5627cbb]1031            str.Printf(wxT("%.fm"), size_snap);
[ac537e9]1032#ifdef SILLY_UNITS
[429465a]1033        } else if (size_snap < 1e6) {
[5627cbb]1034            str.Printf(wxT("%.fkm"), size_snap * 1e-3);
[429465a]1035        } else if (size_snap < 1e9) {
[5627cbb]1036            str.Printf(wxT("%.fMm"), size_snap * 1e-6);
[429465a]1037        } else {
[5627cbb]1038            str.Printf(wxT("%.fGm"), size_snap * 1e-9);
[ac537e9]1039#else
[429465a]1040        } else {
[5627cbb]1041            str.Printf(scalebar ? wxT("%.fkm") : wxT("%.2fkm"), size_snap * 1e-3);
[ac537e9]1042#endif
[429465a]1043        }
[7a89dc2]1044    } else {
[429465a]1045        size_snap /= METRES_PER_FOOT;
1046        if (scalebar) {
1047            Double inches = size_snap * 12;
1048            if (inches < 1.0) {
[5627cbb]1049                str.Printf(wxT("%.3gin"), inches);
[429465a]1050            } else if (size_snap < 1.0) {
[5627cbb]1051                str.Printf(wxT("%.fin"), inches);
[429465a]1052            } else if (size_snap < 5279.5) {
[5627cbb]1053                str.Printf(wxT("%.fft"), size_snap);
[429465a]1054            } else {
[5627cbb]1055                str.Printf(wxT("%.f miles"), size_snap / 5280.0);
[429465a]1056            }
1057        } else {
[5627cbb]1058            str.Printf(wxT("%.fft"), size_snap);
[429465a]1059        }
[ac537e9]1060    }
1061
[5627cbb]1062    return negative ? wxString(wxT("-")) + str : str;
[ac537e9]1063}
1064
[5809313]1065void GfxCore::DrawScalebar()
1066{
[84cab34]1067    // Draw the scalebar.
[eef68f9]1068    if (GetPerspective()) return;
[5809313]1069
[37bc1f5]1070    // Calculate how many metres of survey are currently displayed across the
1071    // screen.
[087bc72]1072    Double across_screen = SurveyUnitsAcrossViewport();
[156dc16]1073
[087bc72]1074    // Convert to imperial measurements if required.
[7a89dc2]1075    Double multiplier = 1.0;
1076    if (!m_Metric) {
[429465a]1077        across_screen /= METRES_PER_FOOT;
1078        multiplier = METRES_PER_FOOT;
1079        if (across_screen >= 5280.0 / 0.75) {
1080            across_screen /= 5280.0;
1081            multiplier *= 5280.0;
1082        }
[7a89dc2]1083    }
[5757725]1084
[7a89dc2]1085    // Calculate the length of the scale bar.
[087bc72]1086    Double size_snap = pow(10.0, floor(log10(0.65 * across_screen)));
1087    Double t = across_screen * 0.65 / size_snap;
[98860c5]1088    if (t >= 5.0) {
[429465a]1089        size_snap *= 5.0;
[7a89dc2]1090    } else if (t >= 2.0) {
[429465a]1091        size_snap *= 2.0;
[98860c5]1092    }
1093
[7a89dc2]1094    if (!m_Metric) size_snap *= multiplier;
1095
[84cab34]1096    // Actual size of the thing in pixels:
[087bc72]1097    int size = int((size_snap / SurveyUnitsAcrossViewport()) * m_XSize);
[e2c1671]1098    m_ScaleBarWidth = size;
[421b7d2]1099
[5809313]1100    // Draw it...
[e2c1671]1101    const int end_y = SCALE_BAR_OFFSET_Y + SCALE_BAR_HEIGHT;
[5809313]1102    int interval = size / 10;
1103
[aa048c3]1104    gla_colour col = col_WHITE;
[5809313]1105    for (int ix = 0; ix < 10; ix++) {
[e2c1671]1106        int x = SCALE_BAR_OFFSET_X + int(ix * ((Double) size / 10.0));
[421b7d2]1107
[e2c1671]1108        DrawRectangle(col, col, x, end_y, interval + 2, SCALE_BAR_HEIGHT);
[421b7d2]1109
[aa048c3]1110        col = (col == col_WHITE) ? col_GREY : col_WHITE;
[5809313]1111    }
1112
[84cab34]1113    // Add labels.
[ac537e9]1114    wxString str = FormatLength(size_snap);
[84cab34]1115
[1eeb55a]1116    int text_width, text_height;
[56da40e]1117    GetTextExtent(str, &text_width, &text_height);
[8bd480e]1118    const int text_y = end_y - text_height + 1;
1119    SetColour(TEXT_COLOUR);
[5627cbb]1120    DrawIndicatorText(SCALE_BAR_OFFSET_X, text_y, wxT("0"));
[8bd480e]1121    DrawIndicatorText(SCALE_BAR_OFFSET_X + size - text_width, text_y, str);
[5809313]1122}
[56da40e]1123
[2072157]1124bool GfxCore::CheckHitTestGrid(const wxPoint& point, bool centre)
[2effbf1]1125{
[0874c07e]1126    if (Animating()) return false;
1127
[203d2a7]1128    if (point.x < 0 || point.x >= m_XSize ||
[429465a]1129        point.y < 0 || point.y >= m_YSize) {
1130        return false;
[137e31b]1131    }
[421b7d2]1132
[156f645]1133    SetDataTransform();
[00a68e0]1134
[69463a0]1135    if (!m_HitTestGridValid) CreateHitTestGrid();
[fa42426]1136
[2effbf1]1137    int grid_x = (point.x * (HITTEST_SIZE - 1)) / m_XSize;
1138    int grid_y = (point.y * (HITTEST_SIZE - 1)) / m_YSize;
[137e31b]1139
[fa42426]1140    LabelInfo *best = NULL;
1141    int dist_sqrd = 25;
[2effbf1]1142    int square = grid_x + grid_y * HITTEST_SIZE;
[fa42426]1143    list<LabelInfo*>::iterator iter = m_PointGrid[square].begin();
[00a68e0]1144
[fa42426]1145    while (iter != m_PointGrid[square].end()) {
[429465a]1146        LabelInfo *pt = *iter++;
[fa42426]1147
[f6d8375]1148        double cx, cy, cz;
[00a68e0]1149
[d67450e]1150        Transform(*pt, &cx, &cy, &cz);
[00a68e0]1151
[429465a]1152        cy = m_YSize - cy;
[00a68e0]1153
[429465a]1154        int dx = point.x - int(cx);
1155        int ds = dx * dx;
1156        if (ds >= dist_sqrd) continue;
1157        int dy = point.y - int(cy);
[fa42426]1158
[429465a]1159        ds += dy * dy;
1160        if (ds >= dist_sqrd) continue;
[f433fda]1161
[429465a]1162        dist_sqrd = ds;
1163        best = pt;
[f433fda]1164
[429465a]1165        if (ds == 0) break;
[2effbf1]1166    }
[f433fda]1167
[fa42426]1168    if (best) {
[6b061db]1169        m_Parent->ShowInfo(best);
[429465a]1170        if (centre) {
[e67ed1b]1171            // FIXME: allow Ctrl-Click to not set there or something?
[82c3731]1172            CentreOn(*best);
[fc768a8]1173            WarpPointer(m_XSize / 2, m_YSize / 2);
[82c3731]1174            SetThere(*best);
[429465a]1175            m_Parent->SelectTreeItem(best);
1176        }
[e67ed1b]1177    } else {
1178        // Left-clicking not on a survey cancels the measuring line.
[0633bcc]1179        if (centre) {
1180            ClearTreeSelection();
1181        } else {
[6b061db]1182            m_Parent->ShowInfo(best);
[f6d8375]1183            double x, y, z;
[0633bcc]1184            ReverseTransform(point.x, m_YSize - point.y, &x, &y, &z);
1185            SetHere(Point(Vector3(x, y, z)));
[c00c6713]1186            m_here_is_temporary = true;
[0633bcc]1187        }
[2effbf1]1188    }
[203d2a7]1189
1190    return best;
[2effbf1]1191}
1192
[5876fcb]1193void GfxCore::OnSize(wxSizeEvent& event)
[5809313]1194{
[5876fcb]1195    // Handle a change in window size.
[bbc22ca]1196
[5876fcb]1197    wxSize size = event.GetSize();
[5809313]1198
[78beaf1]1199    if (size.GetWidth() <= 0 || size.GetHeight() <= 0) {
[0580c6a]1200        // Before things are fully initialised, we sometimes get a bogus
1201        // resize message...
[6ef8bd73]1202        // FIXME have changes in MainFrm cured this?  It still happens with
[880b954]1203        // 1.0.32 and wxGTK 2.5.2 (load a file from the command line).
1204        // With 1.1.6 and wxGTK 2.4.2 we only get negative sizes if MainFrm
[78beaf1]1205        // is resized such that the GfxCore window isn't visible.
1206        //printf("OnSize(%d,%d)\n", size.GetWidth(), size.GetHeight());
[0580c6a]1207        return;
1208    }
[b72f4b5]1209
1210    wxGLCanvas::OnSize(event);
1211
[5876fcb]1212    m_XSize = size.GetWidth();
1213    m_YSize = size.GetHeight();
[39e460c9]1214
[5876fcb]1215    if (m_DoneFirstShow) {
[69463a0]1216        if (m_LabelGrid) {
1217            delete[] m_LabelGrid;
1218            m_LabelGrid = NULL;
1219        }
[6ebc0ce]1220
[69463a0]1221        m_HitTestGridValid = false;
[33b2094]1222
[d67450e]1223        ForceRefresh();
[5809313]1224    }
1225}
1226
[de7a879]1227void GfxCore::DefaultParameters()
[5809313]1228{
[33b2094]1229    // Set default viewing parameters.
[b462168]1230
[f433fda]1231    m_Surface = false;
1232    if (!m_Parent->HasUndergroundLegs()) {
1233        if (m_Parent->HasSurfaceLegs()) {
1234            // If there are surface legs, but no underground legs, turn
1235            // surface surveys on.
1236            m_Surface = true;
1237        } else {
1238            // If there are no legs (e.g. after loading a .pos file), turn
1239            // crosses on.
1240            m_Crosses = true;
1241        }
[b462168]1242    }
1243
[714daae]1244    m_PanAngle = 0.0;
[eef68f9]1245    if (m_Parent->IsExtendedElevation()) {
1246        m_TiltAngle = 0.0;
1247    } else {
1248        m_TiltAngle = 90.0;
[b462168]1249    }
[714daae]1250
[08253d9]1251    SetRotation(m_PanAngle, m_TiltAngle);
[d67450e]1252    SetTranslation(Vector3());
[33b2094]1253
[e577f89]1254    m_RotationStep = 30.0;
[5809313]1255    m_Rotating = false;
[3d00693]1256    m_SwitchingTo = 0;
[fe444b8]1257    m_Entrances = false;
1258    m_FixedPts = false;
1259    m_ExportedPts = false;
[c1cf79d]1260    m_Grid = false;
[f4c5932]1261    m_BoundingBox = false;
[33b2094]1262    m_Tubes = false;
[1eeb55a]1263    if (GetPerspective()) TogglePerspective();
[de7a879]1264}
[5809313]1265
[de7a879]1266void GfxCore::Defaults()
1267{
1268    // Restore default scale, rotation and translation parameters.
1269    DefaultParameters();
[8b0d57f]1270    SetScale(1.0);
[ba358fc]1271
1272    // Invalidate all the cached lists.
1273    GLACanvas::FirstShow();
1274
[fa42426]1275    ForceRefresh();
[5809313]1276}
[84cab34]1277
[5876fcb]1278// return: true if animation occured (and ForceRefresh() needs to be called)
[1690fa9]1279bool GfxCore::Animate()
[5809313]1280{
[2a3d328]1281    if (!Animating()) return false;
1282
1283    // Don't show pointer coordinates while animating.
[4b031c0]1284    // FIXME : only do this when we *START* animating!  Use a static copy
1285    // of the value of "Animating()" last time we were here to track this?
1286    // MainFrm now checks if we're trying to clear already cleared labels
1287    // and just returns, but it might be simpler to check here!
[2a3d328]1288    ClearCoords();
[7c29c976]1289    m_Parent->ShowInfo(NULL);
[5809313]1290
[5876fcb]1291    static double last_t = 0;
[6a4cdcb6]1292    double t;
[75d4a2b]1293    if (movie) {
[aea4f8b]1294        ReadPixels(movie->GetWidth(), movie->GetHeight(), movie->GetBuffer());
[75d4a2b]1295        movie->AddFrame();
[6a4cdcb6]1296        t = 1.0 / 25.0; // 25 frames per second
1297    } else {
1298        t = timer.Time() * 1.0e-3;
1299        if (t == 0) t = 0.001;
1300        else if (t > 1.0) t = 1.0;
1301        if (last_t > 0) t = (t + last_t) / 2;
1302        last_t = t;
1303    }
[5809313]1304
[128fac4]1305    if (presentation_mode == PLAYING && pres_speed != 0.0) {
1306        t *= fabs(pres_speed);
[1690fa9]1307        while (t >= next_mark_time) {
1308            t -= next_mark_time;
[128fac4]1309            this_mark_total = 0;
[58dfdd21]1310            PresentationMark prev_mark = next_mark;
[e577f89]1311            if (prev_mark.angle < 0) prev_mark.angle += 360.0;
1312            else if (prev_mark.angle >= 360.0) prev_mark.angle -= 360.0;
[128fac4]1313            if (pres_reverse)
1314                next_mark = m_Parent->GetPresMark(MARK_PREV);
1315            else
1316                next_mark = m_Parent->GetPresMark(MARK_NEXT);
[1690fa9]1317            if (!next_mark.is_valid()) {
[128fac4]1318                SetView(prev_mark);
[1690fa9]1319                presentation_mode = 0;
[75d4a2b]1320                if (movie) {
1321                    delete movie;
1322                    movie = 0;
[6a4cdcb6]1323                }
[1690fa9]1324                break;
1325            }
[58dfdd21]1326
[128fac4]1327            double tmp = (pres_reverse ? prev_mark.time : next_mark.time);
1328            if (tmp > 0) {
1329                next_mark_time = tmp;
[58dfdd21]1330            } else {
[d67450e]1331                double d = (next_mark - prev_mark).magnitude();
[49ce5b0]1332                // FIXME: should ignore component of d which is unseen in
1333                // non-perspective mode?
[8674eea]1334                next_mark_time = sqrd(d / 30.0);
[49ce5b0]1335                double a = next_mark.angle - prev_mark.angle;
1336                if (a > 180.0) {
1337                    next_mark.angle -= 360.0;
1338                    a = 360.0 - a;
1339                } else if (a < -180.0) {
1340                    next_mark.angle += 360.0;
1341                    a += 360.0;
1342                } else {
1343                    a = fabs(a);
1344                }
1345                next_mark_time += sqrd(a / 60.0);
1346                double ta = fabs(next_mark.tilt_angle - prev_mark.tilt_angle);
1347                next_mark_time += sqrd(ta / 60.0);
1348                double s = fabs(log(next_mark.scale) - log(prev_mark.scale));
[8674eea]1349                next_mark_time += sqrd(s / 2.0);
[49ce5b0]1350                next_mark_time = sqrt(next_mark_time);
[8674eea]1351                // was: next_mark_time = max(max(d / 30, s / 2), max(a, ta) / 60);
[49ce5b0]1352                //printf("*** %.6f from (\nd: %.6f\ns: %.6f\na: %.6f\nt: %.6f )\n",
[8674eea]1353                //       next_mark_time, d/30.0, s/2.0, a/60.0, ta/60.0);
1354                if (tmp < 0) next_mark_time /= -tmp;
[58dfdd21]1355            }
[1690fa9]1356        }
1357
1358        if (presentation_mode) {
1359            // Advance position towards next_mark
1360            double p = t / next_mark_time;
1361            double q = 1 - p;
1362            PresentationMark here = GetView();
[d877aa2]1363            if (next_mark.angle < 0) {
[e577f89]1364                if (here.angle >= next_mark.angle + 360.0)
1365                    here.angle -= 360.0;
1366            } else if (next_mark.angle >= 360.0) {
1367                if (here.angle <= next_mark.angle - 360.0)
1368                    here.angle += 360.0;
[d877aa2]1369            }
[8674eea]1370            here.assign(q * here + p * next_mark);
[1690fa9]1371            here.angle = q * here.angle + p * next_mark.angle;
[e577f89]1372            if (here.angle < 0) here.angle += 360.0;
1373            else if (here.angle >= 360.0) here.angle -= 360.0;
[1690fa9]1374            here.tilt_angle = q * here.tilt_angle + p * next_mark.tilt_angle;
[58dfdd21]1375            here.scale = exp(q * log(here.scale) + p * log(next_mark.scale));
[1690fa9]1376            SetView(here);
[128fac4]1377            this_mark_total += t;
[1690fa9]1378            next_mark_time -= t;
1379        }
1380    }
1381
[5876fcb]1382    // When rotating...
1383    if (m_Rotating) {
[1690fa9]1384        TurnCave(m_RotationStep * t);
[5876fcb]1385    }
[5809313]1386
[5876fcb]1387    if (m_SwitchingTo == PLAN) {
[429465a]1388        // When switching to plan view...
[e577f89]1389        TiltCave(90.0 * t);
1390        if (m_TiltAngle == 90.0) {
[429465a]1391            m_SwitchingTo = 0;
1392        }
[1690fa9]1393    } else if (m_SwitchingTo == ELEVATION) {
[429465a]1394        // When switching to elevation view...
[e577f89]1395        if (fabs(m_TiltAngle) < 90.0 * t) {
[429465a]1396            m_SwitchingTo = 0;
1397            TiltCave(-m_TiltAngle);
[1690fa9]1398        } else if (m_TiltAngle < 0.0) {
[e577f89]1399            TiltCave(90.0 * t);
[1690fa9]1400        } else {
[e577f89]1401            TiltCave(-90.0 * t);
[429465a]1402        }
[3ddd351]1403    } else if (m_SwitchingTo) {
1404        int angle = (m_SwitchingTo - NORTH) * 90;
1405        double diff = angle - m_PanAngle;
1406        if (diff < -180) diff += 360;
1407        if (diff > 180) diff -= 360;
1408        double move = 90.0 * t;
1409        if (move >= fabs(diff)) {
1410            TurnCave(diff);
1411            m_SwitchingTo = 0;
1412        } else if (diff < 0) {
1413            TurnCave(-move);
1414        } else {
1415            TurnCave(move);
1416        }
[5876fcb]1417    }
[5809313]1418
[7757a4ed]1419    return true;
[5809313]1420}
[84cab34]1421
[0580c6a]1422// How much to allow around the box - this is because of the ring shape
1423// at one end of the line.
1424static const int HIGHLIGHTED_PT_SIZE = 2; // FIXME: tie in to blob and ring size
1425#define MARGIN (HIGHLIGHTED_PT_SIZE * 2 + 1)
1426void GfxCore::RefreshLine(const Point &a, const Point &b, const Point &c)
[7a89dc2]1427{
[06d367d]1428    // Best of all might be to copy the window contents before we draw the
1429    // line, then replace each time we redraw.
[796d7bf]1430
[5876fcb]1431    // Calculate the minimum rectangle which includes the old and new
1432    // measuring lines to minimise the redraw time
1433    int l = INT_MAX, r = INT_MIN, u = INT_MIN, d = INT_MAX;
[f6d8375]1434    double X, Y, Z;
[d67450e]1435    if (a.IsValid()) {
1436        if (!Transform(a, &X, &Y, &Z)) {
[796d7bf]1437            printf("oops\n");
1438        } else {
1439            int x = int(X);
1440            int y = m_YSize - 1 - int(Y);
1441            l = x - MARGIN;
1442            r = x + MARGIN;
1443            u = y + MARGIN;
1444            d = y - MARGIN;
1445        }
[5876fcb]1446    }
[d67450e]1447    if (b.IsValid()) {
1448        if (!Transform(b, &X, &Y, &Z)) {
[796d7bf]1449            printf("oops\n");
1450        } else {
1451            int x = int(X);
1452            int y = m_YSize - 1 - int(Y);
1453            l = min(l, x - MARGIN);
1454            r = max(r, x + MARGIN);
1455            u = max(u, y + MARGIN);
1456            d = min(d, y - MARGIN);
1457        }
[5876fcb]1458    }
[d67450e]1459    if (c.IsValid()) {
1460        if (!Transform(c, &X, &Y, &Z)) {
[796d7bf]1461            printf("oops\n");
1462        } else {
1463            int x = int(X);
1464            int y = m_YSize - 1 - int(Y);
1465            l = min(l, x - MARGIN);
1466            r = max(r, x + MARGIN);
1467            u = max(u, y + MARGIN);
1468            d = min(d, y - MARGIN);
1469        }
[5876fcb]1470    }
1471    const wxRect R(l, d, r - l, u - d);
1472    Refresh(false, &R);
[7a89dc2]1473}
1474
[5876fcb]1475void GfxCore::SetHere()
[7a89dc2]1476{
[d67450e]1477    if (!m_here.IsValid()) return;
[6b061db]1478    bool line_active = MeasuringLineActive();
[5876fcb]1479    Point old = m_here;
[d67450e]1480    m_here.Invalidate();
[6b061db]1481    if (line_active || MeasuringLineActive())
1482        RefreshLine(old, m_there, m_here);
[7a89dc2]1483}
1484
[82c3731]1485void GfxCore::SetHere(const Point &p)
[c6d95d8]1486{
[6b061db]1487    bool line_active = MeasuringLineActive();
[5876fcb]1488    Point old = m_here;
[82c3731]1489    m_here = p;
[6b061db]1490    if (line_active || MeasuringLineActive())
1491        RefreshLine(old, m_there, m_here);
[c00c6713]1492    m_here_is_temporary = false;
[156dc16]1493}
1494
[5876fcb]1495void GfxCore::SetThere()
[156dc16]1496{
[d67450e]1497    if (!m_there.IsValid()) return;
[5876fcb]1498    Point old = m_there;
[d67450e]1499    m_there.Invalidate();
[0580c6a]1500    RefreshLine(m_here, old, m_there);
[156dc16]1501}
1502
[82c3731]1503void GfxCore::SetThere(const Point &p)
[156dc16]1504{
[5876fcb]1505    Point old = m_there;
[82c3731]1506    m_there = p;
[0580c6a]1507    RefreshLine(m_here, old, m_there);
[156dc16]1508}
1509
[5876fcb]1510void GfxCore::CreateHitTestGrid()
[156dc16]1511{
[00a68e0]1512    // Clear hit-test grid.
[5876fcb]1513    for (int i = 0; i < HITTEST_SIZE * HITTEST_SIZE; i++) {
[429465a]1514        m_PointGrid[i].clear();
[5876fcb]1515    }
[156dc16]1516
[5876fcb]1517    // Fill the grid.
1518    list<LabelInfo*>::const_iterator pos = m_Parent->GetLabels();
1519    list<LabelInfo*>::const_iterator end = m_Parent->GetLabelsEnd();
1520    while (pos != end) {
[429465a]1521        LabelInfo* label = *pos++;
[33b2094]1522
[429465a]1523        if (!((m_Surface && label->IsSurface()) ||
[dc42b8e]1524              (m_Legs && label->IsUnderground()) ||
1525              (!label->IsSurface() && !label->IsUnderground()))) {
1526            // if this station isn't to be displayed, skip to the next
1527            // (last case is for stns with no legs attached)
[429465a]1528            continue;
1529        }
[33b2094]1530
[429465a]1531        // Calculate screen coordinates.
[f6d8375]1532        double cx, cy, cz;
[d67450e]1533        Transform(*label, &cx, &cy, &cz);
[429465a]1534        if (cx < 0 || cx >= m_XSize) continue;
1535        if (cy < 0 || cy >= m_YSize) continue;
[33b2094]1536
[429465a]1537        cy = m_YSize - cy;
[00a68e0]1538
[429465a]1539        // On-screen, so add to hit-test grid...
1540        int grid_x = int((cx * (HITTEST_SIZE - 1)) / m_XSize);
1541        int grid_y = int((cy * (HITTEST_SIZE - 1)) / m_YSize);
[33b2094]1542
[429465a]1543        m_PointGrid[grid_x + grid_y * HITTEST_SIZE].push_back(label);
[33b2094]1544    }
1545
[00a68e0]1546    m_HitTestGridValid = true;
[33b2094]1547}
[c6d95d8]1548
[2a02de2]1549//
[5876fcb]1550//  Methods for controlling the orientation of the survey
[2a02de2]1551//
1552
[5876fcb]1553void GfxCore::TurnCave(Double angle)
[156dc16]1554{
[5876fcb]1555    // Turn the cave around its z-axis by a given angle.
[156dc16]1556
[5876fcb]1557    m_PanAngle += angle;
[e577f89]1558    if (m_PanAngle >= 360.0) {
1559        m_PanAngle -= 360.0;
[5876fcb]1560    } else if (m_PanAngle < 0.0) {
[e577f89]1561        m_PanAngle += 360.0;
[156dc16]1562    }
[33b2094]1563
[00a68e0]1564    m_HitTestGridValid = false;
[c00c6713]1565    if (m_here_is_temporary) SetHere();
[00a68e0]1566
[08253d9]1567    SetRotation(m_PanAngle, m_TiltAngle);
[156dc16]1568}
1569
[5876fcb]1570void GfxCore::TurnCaveTo(Double angle)
[d80805e]1571{
[3ddd351]1572    timer.Start(drawtime);
1573    int new_switching_to = ((int)angle) / 90 + NORTH;
1574    if (new_switching_to == m_SwitchingTo) {
1575        // A second order to switch takes us there right away
1576        TurnCave(angle - m_PanAngle);
1577        m_SwitchingTo = 0;
1578        ForceRefresh();
1579    } else {
1580        m_SwitchingTo = new_switching_to;
1581    }
[d80805e]1582}
1583
[5876fcb]1584void GfxCore::TiltCave(Double tilt_angle)
[156dc16]1585{
[5876fcb]1586    // Tilt the cave by a given angle.
[e577f89]1587    if (m_TiltAngle + tilt_angle > 90.0) {
[08253d9]1588        m_TiltAngle = 90.0;
[e577f89]1589    } else if (m_TiltAngle + tilt_angle < -90.0) {
[08253d9]1590        m_TiltAngle = -90.0;
1591    } else {
1592        m_TiltAngle += tilt_angle;
[d80805e]1593    }
1594
[00a68e0]1595    m_HitTestGridValid = false;
[c00c6713]1596    if (m_here_is_temporary) SetHere();
[00a68e0]1597
[08253d9]1598    SetRotation(m_PanAngle, m_TiltAngle);
[5ffa439]1599}
1600
[5876fcb]1601void GfxCore::TranslateCave(int dx, int dy)
[5ffa439]1602{
[33b2094]1603    AddTranslationScreenCoordinates(dx, dy);
[00a68e0]1604    m_HitTestGridValid = false;
1605
[c00c6713]1606    if (m_here_is_temporary) SetHere();
1607
[33b2094]1608    ForceRefresh();
1609}
[5876fcb]1610
[33b2094]1611void GfxCore::DragFinished()
1612{
[76dd228]1613    m_MouseOutsideCompass = m_MouseOutsideElev = false;
[5876fcb]1614    ForceRefresh();
[2173dbd]1615}
1616
[d877aa2]1617void GfxCore::ClearCoords()
1618{
1619    m_Parent->ClearCoords();
1620}
1621
[5876fcb]1622void GfxCore::SetCoords(wxPoint point)
[fd6e0d5]1623{
[0874c07e]1624    // We can't work out 2D coordinates from a perspective view, and it
1625    // doesn't really make sense to show coordinates while we're animating.
1626    if (GetPerspective() || Animating()) return;
[0a811ab]1627
[5876fcb]1628    // Update the coordinate or altitude display, given the (x, y) position in
1629    // window coordinates.  The relevant display is updated depending on
1630    // whether we're in plan or elevation view.
1631
[f6d8375]1632    double cx, cy, cz;
[5876fcb]1633
[a2b3d62]1634    SetDataTransform();
[887c26e]1635    ReverseTransform(point.x, m_YSize - 1 - point.y, &cx, &cy, &cz);
[5876fcb]1636
[0633bcc]1637    if (ShowingPlan()) {
[d67450e]1638        m_Parent->SetCoords(cx + m_Parent->GetOffset().GetX(),
1639                            cy + m_Parent->GetOffset().GetY());
[0633bcc]1640    } else if (ShowingElevation()) {
[d67450e]1641        m_Parent->SetAltitude(cz + m_Parent->GetOffset().GetZ());
[d479c15]1642    } else {
[429465a]1643        m_Parent->ClearCoords();
[a2b3d62]1644    }
[fd6e0d5]1645}
1646
[1eeb55a]1647int GfxCore::GetCompassXPosition() const
[1fd2edb]1648{
[f433fda]1649    // Return the x-coordinate of the centre of the compass in window
1650    // coordinates.
[1eeb55a]1651    return m_XSize - INDICATOR_OFFSET_X - INDICATOR_BOX_SIZE / 2;
[1fd2edb]1652}
1653
[1eeb55a]1654int GfxCore::GetClinoXPosition() const
[1fd2edb]1655{
[f433fda]1656    // Return the x-coordinate of the centre of the compass in window
1657    // coordinates.
[1eeb55a]1658    return m_XSize - GetClinoOffset() - INDICATOR_BOX_SIZE / 2;
[1fd2edb]1659}
1660
[1eeb55a]1661int GfxCore::GetIndicatorYPosition() const
[dfe4454c]1662{
[f433fda]1663    // Return the y-coordinate of the centre of the indicators in window
1664    // coordinates.
[1eeb55a]1665    return m_YSize - INDICATOR_OFFSET_Y - INDICATOR_BOX_SIZE / 2;
[5876fcb]1666}
[fa42426]1667
[1eeb55a]1668int GfxCore::GetIndicatorRadius() const
[5876fcb]1669{
1670    // Return the radius of each indicator.
[1eeb55a]1671    return (INDICATOR_BOX_SIZE - INDICATOR_MARGIN * 2) / 2;
[5876fcb]1672}
[dfe4454c]1673
[14acdae]1674bool GfxCore::PointWithinCompass(wxPoint point) const
[5876fcb]1675{
[f433fda]1676    // Determine whether a point (in window coordinates) lies within the
1677    // compass.
[e2c1671]1678    if (!ShowingCompass()) return false;
1679
[33b2094]1680    glaCoord dx = point.x - GetCompassXPosition();
1681    glaCoord dy = point.y - GetIndicatorYPosition();
1682    glaCoord radius = GetIndicatorRadius();
[f433fda]1683
[5876fcb]1684    return (dx * dx + dy * dy <= radius * radius);
1685}
[fa42426]1686
[14acdae]1687bool GfxCore::PointWithinClino(wxPoint point) const
[5876fcb]1688{
1689    // Determine whether a point (in window coordinates) lies within the clino.
[e2c1671]1690    if (!ShowingClino()) return false;
1691
[33b2094]1692    glaCoord dx = point.x - GetClinoXPosition();
1693    glaCoord dy = point.y - GetIndicatorYPosition();
1694    glaCoord radius = GetIndicatorRadius();
[f433fda]1695
[5876fcb]1696    return (dx * dx + dy * dy <= radius * radius);
[dfe4454c]1697}
[8000d8f]1698
[14acdae]1699bool GfxCore::PointWithinScaleBar(wxPoint point) const
[5876fcb]1700{
[e2c1671]1701    // Determine whether a point (in window coordinates) lies within the scale
1702    // bar.
1703    if (!ShowingScaleBar()) return false;
[8000d8f]1704
[e2c1671]1705    return (point.x >= SCALE_BAR_OFFSET_X &&
1706            point.x <= SCALE_BAR_OFFSET_X + m_ScaleBarWidth &&
1707            point.y <= m_YSize - SCALE_BAR_OFFSET_Y - SCALE_BAR_HEIGHT &&
1708            point.y >= m_YSize - SCALE_BAR_OFFSET_Y - SCALE_BAR_HEIGHT*2);
[5876fcb]1709}
[8000d8f]1710
[5876fcb]1711void GfxCore::SetCompassFromPoint(wxPoint point)
[8000d8f]1712{
[d877aa2]1713    // Given a point in window coordinates, set the heading of the survey.  If
1714    // the point is outside the compass, it snaps to 45 degree intervals;
1715    // otherwise it operates as normal.
[8000d8f]1716
[5876fcb]1717    wxCoord dx = point.x - GetCompassXPosition();
1718    wxCoord dy = point.y - GetIndicatorYPosition();
1719    wxCoord radius = GetIndicatorRadius();
[7aa15c0]1720
[0580c6a]1721    double angle = deg(atan2(double(dx), double(dy))) - 180.0;
[5876fcb]1722    if (dx * dx + dy * dy <= radius * radius) {
[3ddd351]1723        TurnCave(angle - m_PanAngle);
[429465a]1724        m_MouseOutsideCompass = false;
[e577f89]1725    } else {
[3ddd351]1726        TurnCave(int(angle / 45.0) * 45.0 - m_PanAngle);
[429465a]1727        m_MouseOutsideCompass = true;
[7aa15c0]1728    }
[8000d8f]1729
[5876fcb]1730    ForceRefresh();
1731}
1732
1733void GfxCore::SetClinoFromPoint(wxPoint point)
1734{
[d877aa2]1735    // Given a point in window coordinates, set the elevation of the survey.
1736    // If the point is outside the clino, it snaps to 90 degree intervals;
1737    // otherwise it operates as normal.
[8000d8f]1738
[33b2094]1739    glaCoord dx = point.x - GetClinoXPosition();
1740    glaCoord dy = point.y - GetIndicatorYPosition();
1741    glaCoord radius = GetIndicatorRadius();
[f433fda]1742
[5876fcb]1743    if (dx >= 0 && dx * dx + dy * dy <= radius * radius) {
[0580c6a]1744        TiltCave(deg(atan2(double(dy), double(dx))) - m_TiltAngle);
[429465a]1745        m_MouseOutsideElev = false;
[e577f89]1746    } else if (dy >= INDICATOR_MARGIN) {
1747        TiltCave(90.0 - m_TiltAngle);
[429465a]1748        m_MouseOutsideElev = true;
[e577f89]1749    } else if (dy <= -INDICATOR_MARGIN) {
1750        TiltCave(-90.0 - m_TiltAngle);
[429465a]1751        m_MouseOutsideElev = true;
[e577f89]1752    } else {
[429465a]1753        TiltCave(-m_TiltAngle);
1754        m_MouseOutsideElev = true;
[5876fcb]1755    }
[8000d8f]1756
[5876fcb]1757    ForceRefresh();
[8000d8f]1758}
1759
[5876fcb]1760void GfxCore::SetScaleBarFromOffset(wxCoord dx)
[8000d8f]1761{
[5876fcb]1762    // Set the scale of the survey, given an offset as to how much the mouse has
1763    // been dragged over the scalebar since the last scale change.
[8000d8f]1764
[5b7164d]1765    SetScale((m_ScaleBarWidth + dx) * m_Scale / m_ScaleBarWidth);
[5876fcb]1766    ForceRefresh();
1767}
[8000d8f]1768
[5876fcb]1769void GfxCore::RedrawIndicators()
1770{
1771    // Redraw the compass and clino indicators.
[8000d8f]1772
[203d2a7]1773    const wxRect r(m_XSize - INDICATOR_OFFSET_X - INDICATOR_BOX_SIZE*2 -
[429465a]1774                     INDICATOR_GAP,
1775                   m_YSize - INDICATOR_OFFSET_Y - INDICATOR_BOX_SIZE,
1776                   INDICATOR_BOX_SIZE*2 + INDICATOR_GAP,
1777                   INDICATOR_BOX_SIZE);
[f433fda]1778
[5876fcb]1779    Refresh(false, &r);
[8000d8f]1780}
1781
[5876fcb]1782void GfxCore::StartRotation()
[8000d8f]1783{
[5876fcb]1784    // Start the survey rotating.
[f433fda]1785
[5876fcb]1786    m_Rotating = true;
1787    timer.Start(drawtime);
1788}
[8000d8f]1789
[5876fcb]1790void GfxCore::ToggleRotation()
1791{
1792    // Toggle the survey rotation on/off.
[f433fda]1793
[5876fcb]1794    if (m_Rotating) {
[2a3d328]1795        StopRotation();
1796    } else {
1797        StartRotation();
[5876fcb]1798    }
1799}
[8000d8f]1800
[5876fcb]1801void GfxCore::StopRotation()
1802{
1803    // Stop the survey rotating.
[8000d8f]1804
[5876fcb]1805    m_Rotating = false;
[33b2094]1806    ForceRefresh();
[5876fcb]1807}
[8000d8f]1808
[eef68f9]1809bool GfxCore::IsExtendedElevation() const
[5876fcb]1810{
[eef68f9]1811    return m_Parent->IsExtendedElevation();
[5876fcb]1812}
[8000d8f]1813
[5876fcb]1814void GfxCore::ReverseRotation()
1815{
1816    // Reverse the direction of rotation.
[8000d8f]1817
[5876fcb]1818    m_RotationStep = -m_RotationStep;
1819}
[8000d8f]1820
[5876fcb]1821void GfxCore::RotateSlower(bool accel)
1822{
1823    // Decrease the speed of rotation, optionally by an increased amount.
[8000d8f]1824
[5876fcb]1825    m_RotationStep /= accel ? 1.44 : 1.2;
[e577f89]1826    if (m_RotationStep < 1.0) {
1827        m_RotationStep = 1.0;
[1690fa9]1828    }
[8000d8f]1829}
1830
[5876fcb]1831void GfxCore::RotateFaster(bool accel)
1832{
1833    // Increase the speed of rotation, optionally by an increased amount.
1834
1835    m_RotationStep *= accel ? 1.44 : 1.2;
[e577f89]1836    if (m_RotationStep > 450.0) {
1837        m_RotationStep = 450.0;
[1690fa9]1838    }
[5876fcb]1839}
[8000d8f]1840
[5876fcb]1841void GfxCore::SwitchToElevation()
[8000d8f]1842{
[5876fcb]1843    // Perform an animated switch to elevation view.
[8000d8f]1844
[5876fcb]1845    switch (m_SwitchingTo) {
[429465a]1846        case 0:
1847            timer.Start(drawtime);
1848            m_SwitchingTo = ELEVATION;
1849            break;
[f433fda]1850
[429465a]1851        case PLAN:
1852            m_SwitchingTo = ELEVATION;
1853            break;
[f433fda]1854
[429465a]1855        case ELEVATION:
1856            // a second order to switch takes us there right away
1857            TiltCave(-m_TiltAngle);
1858            m_SwitchingTo = 0;
1859            ForceRefresh();
[5876fcb]1860    }
[8000d8f]1861}
1862
[5876fcb]1863void GfxCore::SwitchToPlan()
[8000d8f]1864{
[5876fcb]1865    // Perform an animated switch to plan view.
[8000d8f]1866
[5876fcb]1867    switch (m_SwitchingTo) {
[429465a]1868        case 0:
1869            timer.Start(drawtime);
1870            m_SwitchingTo = PLAN;
1871            break;
[f433fda]1872
[429465a]1873        case ELEVATION:
1874            m_SwitchingTo = PLAN;
1875            break;
[8000d8f]1876
[429465a]1877        case PLAN:
[0580c6a]1878            // A second order to switch takes us there right away
[e577f89]1879            TiltCave(90.0 - m_TiltAngle);
[429465a]1880            m_SwitchingTo = 0;
1881            ForceRefresh();
[8000d8f]1882    }
[5876fcb]1883}
[8000d8f]1884
[d1628e8e]1885void GfxCore::SetViewTo(Double xmin, Double xmax, Double ymin, Double ymax, Double zmin, Double zmax)
1886{
1887
1888    SetTranslation(-Vector3((xmin + xmax) / 2, (ymin + ymax) / 2, (zmin + zmax) / 2));
[d0f5918]1889    Double scale = HUGE_VAL;
[d1628e8e]1890    const Vector3 ext = m_Parent->GetExtent();
1891    if (xmax > xmin) {
1892        Double s = ext.GetX() / (xmax - xmin);
1893        if (s < scale) scale = s;
1894    }
1895    if (ymax > ymin) {
1896        Double s = ext.GetY() / (ymax - ymin);
1897        if (s < scale) scale = s;
1898    }
1899    if (!ShowingPlan() && zmax > zmin) {
1900        Double s = ext.GetZ() / (zmax - zmin);
1901        if (s < scale) scale = s;
1902    }
[d0f5918]1903    if (scale != HUGE_VAL) SetScale(scale);
[d1628e8e]1904    ForceRefresh();
1905}
1906
[14acdae]1907bool GfxCore::CanRaiseViewpoint() const
[5876fcb]1908{
1909    // Determine if the survey can be viewed from a higher angle of elevation.
[f433fda]1910
[0662b0b]1911    return GetPerspective() ? (m_TiltAngle > -90.0) : (m_TiltAngle < 90.0);
[8000d8f]1912}
1913
[14acdae]1914bool GfxCore::CanLowerViewpoint() const
[2effbf1]1915{
[5876fcb]1916    // Determine if the survey can be viewed from a lower angle of elevation.
[2effbf1]1917
[0662b0b]1918    return GetPerspective() ? (m_TiltAngle < 90.0) : (m_TiltAngle > -90.0);
[5876fcb]1919}
[2effbf1]1920
[78c67a6]1921bool GfxCore::HasDepth() const
[bd21214]1922{
[78c67a6]1923    return m_Parent->GetDepthExtent() == 0.0;
[bd21214]1924}
1925
[843ee7b]1926bool GfxCore::HasErrorInformation() const
[d4650b3]1927{
[843ee7b]1928    return m_Parent->HasErrorInformation();
[d4650b3]1929}
1930
[843ee7b]1931bool GfxCore::HasDateInformation() const
[c61aa79]1932{
[843ee7b]1933    return m_Parent->GetDateMin() >= 0;
[c61aa79]1934}
1935
[14acdae]1936bool GfxCore::ShowingPlan() const
[5876fcb]1937{
1938    // Determine if the survey is in plan view.
[f433fda]1939
[e577f89]1940    return (m_TiltAngle == 90.0);
[2effbf1]1941}
1942
[14acdae]1943bool GfxCore::ShowingElevation() const
[8000d8f]1944{
[5876fcb]1945    // Determine if the survey is in elevation view.
[f433fda]1946
[5876fcb]1947    return (m_TiltAngle == 0.0);
[8000d8f]1948}
1949
[14acdae]1950bool GfxCore::ShowingMeasuringLine() const
[8000d8f]1951{
[5876fcb]1952    // Determine if the measuring line is being shown.
[f433fda]1953
[c00c6713]1954    return (m_there.IsValid() && m_here.IsValid());
[8000d8f]1955}
1956
[eff44b9]1957void GfxCore::ToggleFlag(bool* flag, int update)
[5876fcb]1958{
1959    *flag = !*flag;
[6747314]1960    if (update == UPDATE_BLOBS) {
[eff44b9]1961        UpdateBlobs();
[ef1870d]1962    } else if (update == UPDATE_BLOBS_AND_CROSSES) {
1963        UpdateBlobs();
1964        InvalidateList(LIST_CROSSES);
[6cd6bbe]1965    }
[eff44b9]1966    ForceRefresh();
[5876fcb]1967}
[93744a5]1968
[14acdae]1969int GfxCore::GetNumEntrances() const
[4b1fc48]1970{
[5876fcb]1971    return m_Parent->GetNumEntrances();
1972}
[bd7a61b]1973
[14acdae]1974int GfxCore::GetNumFixedPts() const
[5876fcb]1975{
1976    return m_Parent->GetNumFixedPts();
1977}
[7757a4ed]1978
[14acdae]1979int GfxCore::GetNumExportedPts() const
[5876fcb]1980{
1981    return m_Parent->GetNumExportedPts();
1982}
1983
1984void GfxCore::ClearTreeSelection()
1985{
1986    m_Parent->ClearTreeSelection();
1987}
1988
[82c3731]1989void GfxCore::CentreOn(const Point &p)
[5876fcb]1990{
[d67450e]1991    SetTranslation(-p);
[00a68e0]1992    m_HitTestGridValid = false;
[f433fda]1993
[5876fcb]1994    ForceRefresh();
[4b1fc48]1995}
[5876fcb]1996
[33b2094]1997void GfxCore::ForceRefresh()
1998{
1999    Refresh(false);
2000}
2001
[d2fcc9b]2002void GfxCore::GenerateList(unsigned int l)
[33b2094]2003{
[9eb58d0]2004    assert(m_HaveData);
[3ddcad8]2005
[d2fcc9b]2006    switch (l) {
[fe665c4]2007        case LIST_COMPASS:
2008            DrawCompass();
2009            break;
2010        case LIST_CLINO:
2011            DrawClino();
2012            break;
2013        case LIST_CLINO_BACK:
2014            DrawClinoBack();
2015            break;
[252d759]2016        case LIST_DEPTHBAR:
2017            DrawDepthbar();
2018            break;
[d4650b3]2019        case LIST_DATEBAR:
2020            DrawDatebar();
2021            break;
[c61aa79]2022        case LIST_ERRORBAR:
2023            DrawErrorbar();
2024            break;
[d2fcc9b]2025        case LIST_UNDERGROUND_LEGS:
2026            GenerateDisplayList();
2027            break;
2028        case LIST_TUBES:
2029            GenerateDisplayListTubes();
2030            break;
2031        case LIST_SURFACE_LEGS:
2032            GenerateDisplayListSurface();
2033            break;
[86fe6e4]2034        case LIST_BLOBS:
2035            GenerateBlobsDisplayList();
[37d7084]2036            break;
[86fe6e4]2037        case LIST_CROSSES: {
2038            BeginCrosses();
2039            SetColour(col_LIGHT_GREY);
2040            list<LabelInfo*>::const_iterator pos = m_Parent->GetLabels();
2041            while (pos != m_Parent->GetLabelsEnd()) {
2042                const LabelInfo* label = *pos++;
2043
2044                if ((m_Surface && label->IsSurface()) ||
2045                    (m_Legs && label->IsUnderground()) ||
2046                    (!label->IsSurface() && !label->IsUnderground())) {
2047                    // Check if this station should be displayed
2048                    // (last case is for stns with no legs attached)
2049                    DrawCross(label->GetX(), label->GetY(), label->GetZ());
2050                }
2051            }
2052            EndCrosses();
2053            break;
2054        }
[37d7084]2055        case LIST_GRID:
2056            DrawGrid();
[d2fcc9b]2057            break;
[86fe6e4]2058        case LIST_SHADOW:
2059            GenerateDisplayListShadow();
[d2fcc9b]2060            break;
2061        default:
2062            assert(false);
2063            break;
2064    }
2065}
2066
[d67450e]2067void GfxCore::ToggleSmoothShading()
2068{
2069    GLACanvas::ToggleSmoothShading();
2070    InvalidateList(LIST_TUBES);
2071    ForceRefresh();
2072}
2073
[d2fcc9b]2074void GfxCore::GenerateDisplayList()
2075{
2076    // Generate the display list for the underground legs.
[c61aa79]2077    list<traverse>::const_iterator trav = m_Parent->traverses_begin();
2078    list<traverse>::const_iterator tend = m_Parent->traverses_end();
[3ddcad8]2079    while (trav != tend) {
2080        (this->*AddPoly)(*trav);
2081        ++trav;
2082    }
[33b2094]2083}
2084
[9eb58d0]2085void GfxCore::GenerateDisplayListTubes()
[33b2094]2086{
[9eb58d0]2087    // Generate the display list for the tubes.
[ee05463]2088    list<vector<XSect> >::const_iterator trav = m_Parent->tubes_begin();
2089    list<vector<XSect> >::const_iterator tend = m_Parent->tubes_end();
[3ddcad8]2090    while (trav != tend) {
2091        SkinPassage(*trav);
2092        ++trav;
2093    }
[9eb58d0]2094}
[33b2094]2095
[9eb58d0]2096void GfxCore::GenerateDisplayListSurface()
2097{
2098    // Generate the display list for the surface legs.
[3ddcad8]2099    EnableDashedLines();
[c61aa79]2100    list<traverse>::const_iterator trav = m_Parent->surface_traverses_begin();
2101    list<traverse>::const_iterator tend = m_Parent->surface_traverses_end();
[3ddcad8]2102    while (trav != tend) {
[c61aa79]2103        if (m_ColourBy == COLOUR_BY_ERROR) {
2104            AddPolylineError(*trav);
2105        } else {
2106            AddPolyline(*trav);
2107        }
[3ddcad8]2108        ++trav;
2109    }
2110    DisableDashedLines();
[d9b3270]2111}
[33b2094]2112
[37d7084]2113void GfxCore::GenerateDisplayListShadow()
[f4c5932]2114{
2115    SetColour(col_BLACK);
[c61aa79]2116    list<traverse>::const_iterator trav = m_Parent->traverses_begin();
2117    list<traverse>::const_iterator tend = m_Parent->traverses_end();
[f4c5932]2118    while (trav != tend) {
2119        AddPolylineShadow(*trav);
2120        ++trav;
2121    }
2122}
2123
[d2fcc9b]2124// Plot blobs.
[d9b3270]2125void GfxCore::GenerateBlobsDisplayList()
2126{
[e633bb1]2127    if (!(m_Entrances || m_FixedPts || m_ExportedPts ||
2128          m_Parent->GetNumHighlightedPts()))
2129        return;
[429465a]2130
[e633bb1]2131    // Plot blobs.
2132    gla_colour prev_col = col_BLACK; // not a colour used for blobs
2133    list<LabelInfo*>::const_iterator pos = m_Parent->GetLabels();
2134    BeginBlobs();
2135    while (pos != m_Parent->GetLabelsEnd()) {
[429465a]2136        const LabelInfo* label = *pos++;
2137
2138        // When more than one flag is set on a point:
2139        // search results take priority over entrance highlighting
2140        // which takes priority over fixed point
2141        // highlighting, which in turn takes priority over exported
2142        // point highlighting.
2143
2144        if (!((m_Surface && label->IsSurface()) ||
2145              (m_Legs && label->IsUnderground()) ||
2146              (!label->IsSurface() && !label->IsUnderground()))) {
2147            // if this station isn't to be displayed, skip to the next
2148            // (last case is for stns with no legs attached)
2149            continue;
2150        }
2151
[e633bb1]2152        gla_colour col;
2153
[429465a]2154        if (label->IsHighLighted()) {
2155            col = col_YELLOW;
2156        } else if (m_Entrances && label->IsEntrance()) {
2157            col = col_GREEN;
2158        } else if (m_FixedPts && label->IsFixedPt()) {
2159            col = col_RED;
2160        } else if (m_ExportedPts && label->IsExportedPt()) {
2161            col = col_TURQUOISE;
2162        } else {
2163            continue;
2164        }
2165
[e633bb1]2166        // Stations are sorted by blob type, so colour changes are infrequent.
2167        if (col != prev_col) {
[aa048c3]2168            SetColour(col);
[e633bb1]2169            prev_col = col;
[429465a]2170        }
[e633bb1]2171        DrawBlob(label->GetX(), label->GetY(), label->GetZ());
[d9b3270]2172    }
[e633bb1]2173    EndBlobs();
[33b2094]2174}
2175
[6747314]2176void GfxCore::DrawIndicators()
[33b2094]2177{
[97ea48d]2178    // Draw colour key.
2179    if (m_ColourKey) {
[78c67a6]2180       if (m_ColourBy == COLOUR_BY_DEPTH && m_Parent->GetDepthExtent() != 0.0) {
[d4650b3]2181           DrawList2D(LIST_DEPTHBAR, m_XSize, m_YSize, 0);
[843ee7b]2182       } else if (m_ColourBy == COLOUR_BY_DATE && HasDateInformation()) {
[d4650b3]2183           DrawList2D(LIST_DATEBAR, m_XSize, m_YSize, 0);
[c61aa79]2184       } else if (m_ColourBy == COLOUR_BY_ERROR && m_Parent->HasErrorInformation()) {
2185           DrawList2D(LIST_ERRORBAR, m_XSize, m_YSize, 0);
[d4650b3]2186       }
[33b2094]2187    }
[56da40e]2188
[203d2a7]2189    // Draw compass or elevation/heading indicators.
[eef68f9]2190    if (m_Compass || m_Clino) {
2191        if (!m_Parent->IsExtendedElevation()) Draw2dIndicators();
[203d2a7]2192    }
[f433fda]2193
[56da40e]2194    // Draw scalebar.
2195    if (m_Scalebar) {
[429465a]2196        DrawScalebar();
[56da40e]2197    }
[33b2094]2198}
2199
[f336ab9]2200void GfxCore::PlaceVertexWithColour(const Vector3 & v,
2201                                    glaTexCoord tex_x, glaTexCoord tex_y,
[b839829]2202                                    Double factor)
[f383708]2203{
[da6c802]2204    SetColour(GetSurfacePen(), factor); // FIXME : assumes surface pen is white!
[b839829]2205    PlaceVertex(v, tex_x, tex_y);
[da6c802]2206}
[f433fda]2207
[b839829]2208void GfxCore::SetDepthColour(Double z, Double factor) {
[da6c802]2209    // Set the drawing colour based on the altitude.
[78c67a6]2210    Double z_ext = m_Parent->GetDepthExtent();
[125c9ea]2211    assert(z_ext > 0);
[f383708]2212
[b839829]2213    z -= m_Parent->GetDepthMin();
[f383708]2214    // points arising from tubes may be slightly outside the limits...
[78c67a6]2215    if (z < 0) z = 0;
2216    if (z > z_ext) z = z_ext;
[a6f081c]2217
[78c67a6]2218    Double how_far = z / z_ext;
[f383708]2219    assert(how_far >= 0.0);
2220    assert(how_far <= 1.0);
2221
[97ea48d]2222    int band = int(floor(how_far * (GetNumColourBands() - 1)));
[0e69efe]2223    GLAPen pen1 = GetPen(band);
[97ea48d]2224    if (band < GetNumColourBands() - 1) {
[d4650b3]2225        const GLAPen& pen2 = GetPen(band + 1);
[f433fda]2226
[97ea48d]2227        Double interval = z_ext / (GetNumColourBands() - 1);
[78c67a6]2228        Double into_band = z / interval - band;
[f433fda]2229
[d4650b3]2230//      printf("%g z_offset=%g interval=%g band=%d\n", into_band,
2231//             z_offset, interval, band);
2232        // FIXME: why do we need to clamp here?  Is it because the walls can
2233        // extend further up/down than the centre-line?
2234        if (into_band < 0.0) into_band = 0.0;
2235        if (into_band > 1.0) into_band = 1.0;
2236        assert(into_band >= 0.0);
2237        assert(into_band <= 1.0);
[f433fda]2238
[d4650b3]2239        pen1.Interpolate(pen2, into_band);
2240    }
[aa048c3]2241    SetColour(pen1, factor);
[b839829]2242}
[f383708]2243
[b839829]2244void GfxCore::PlaceVertexWithDepthColour(const Vector3 &v, Double factor)
2245{
2246    SetDepthColour(v.GetZ(), factor);
[d67450e]2247    PlaceVertex(v);
[f383708]2248}
2249
[b839829]2250void GfxCore::PlaceVertexWithDepthColour(const Vector3 &v,
[f336ab9]2251                                         glaTexCoord tex_x, glaTexCoord tex_y,
[b839829]2252                                         Double factor)
2253{
2254    SetDepthColour(v.GetZ(), factor);
2255    PlaceVertex(v, tex_x, tex_y);
2256}
2257
[82f584f]2258void GfxCore::SplitLineAcrossBands(int band, int band2,
[4a0e6b35]2259                                   const Vector3 &p, const Vector3 &q,
[82f584f]2260                                   Double factor)
[b5d64e6]2261{
[4a0e6b35]2262    const int step = (band < band2) ? 1 : -1;
[b5d64e6]2263    for (int i = band; i != band2; i += step) {
[4a0e6b35]2264        const Double z = GetDepthBoundaryBetweenBands(i, i + step);
2265
2266        // Find the intersection point of the line p -> q
2267        // with the plane parallel to the xy-plane with z-axis intersection z.
[d67450e]2268        assert(q.GetZ() - p.GetZ() != 0.0);
[4a0e6b35]2269
[d67450e]2270        const Double t = (z - p.GetZ()) / (q.GetZ() - p.GetZ());
[4a0e6b35]2271//      assert(0.0 <= t && t <= 1.0);           FIXME: rounding problems!
2272
[d67450e]2273        const Double x = p.GetX() + t * (q.GetX() - p.GetX());
2274        const Double y = p.GetY() + t * (q.GetY() - p.GetY());
[4a0e6b35]2275
[d67450e]2276        PlaceVertexWithDepthColour(Vector3(x, y, z), factor);
[b5d64e6]2277    }
2278}
2279
[14acdae]2280int GfxCore::GetDepthColour(Double z) const
[b5d64e6]2281{
[82f584f]2282    // Return the (0-based) depth colour band index for a z-coordinate.
[78c67a6]2283    Double z_ext = m_Parent->GetDepthExtent();
[125c9ea]2284    assert(z_ext > 0);
[78c67a6]2285    z -= m_Parent->GetDepthMin();
[a6f081c]2286    z += z_ext / 2;
[97ea48d]2287    return int(z / z_ext * (GetNumColourBands() - 1));
[b5d64e6]2288}
2289
[14acdae]2290Double GfxCore::GetDepthBoundaryBetweenBands(int a, int b) const
[b5d64e6]2291{
[82f584f]2292    // Return the z-coordinate of the depth colour boundary between
2293    // two adjacent depth colour bands (specified by 0-based indices).
2294
2295    assert((a == b - 1) || (a == b + 1));
[97ea48d]2296    if (GetNumColourBands() == 1) return 0;
[82f584f]2297
2298    int band = (a > b) ? a : b; // boundary N lies on the bottom of band N.
[78c67a6]2299    Double z_ext = m_Parent->GetDepthExtent();
[97ea48d]2300    return (z_ext * band / (GetNumColourBands() - 1)) - z_ext / 2
[78c67a6]2301        + m_Parent->GetDepthMin();
[b5d64e6]2302}
2303
[c61aa79]2304void GfxCore::AddPolyline(const traverse & centreline)
[da6c802]2305{
2306    BeginPolyline();
2307    SetColour(GetSurfacePen());
[d4650b3]2308    vector<PointInfo>::const_iterator i = centreline.begin();
[d67450e]2309    PlaceVertex(*i);
[da6c802]2310    ++i;
2311    while (i != centreline.end()) {
[d67450e]2312        PlaceVertex(*i);
[da6c802]2313        ++i;
2314    }
2315    EndPolyline();
2316}
[2a3d328]2317
[c61aa79]2318void GfxCore::AddPolylineShadow(const traverse & centreline)
[f4c5932]2319{
2320    BeginPolyline();
[78c67a6]2321    const double z = -0.5 * m_Parent->GetZExtent();
[d4650b3]2322    vector<PointInfo>::const_iterator i = centreline.begin();
[78c67a6]2323    PlaceVertex(i->GetX(), i->GetY(), z);
[f4c5932]2324    ++i;
2325    while (i != centreline.end()) {
[78c67a6]2326        PlaceVertex(i->GetX(), i->GetY(), z);
[f4c5932]2327        ++i;
2328    }
2329    EndPolyline();
2330}
2331
[c61aa79]2332void GfxCore::AddPolylineDepth(const traverse & centreline)
[da6c802]2333{
2334    BeginPolyline();
[d4650b3]2335    vector<PointInfo>::const_iterator i, prev_i;
[da6c802]2336    i = centreline.begin();
[ee7af72]2337    int band0 = GetDepthColour(i->GetZ());
[d67450e]2338    PlaceVertexWithDepthColour(*i);
[da6c802]2339    prev_i = i;
2340    ++i;
2341    while (i != centreline.end()) {
[ee7af72]2342        int band = GetDepthColour(i->GetZ());
[da6c802]2343        if (band != band0) {
[d67450e]2344            SplitLineAcrossBands(band0, band, *prev_i, *i);
[da6c802]2345            band0 = band;
2346        }
[d67450e]2347        PlaceVertexWithDepthColour(*i);
[da6c802]2348        prev_i = i;
2349        ++i;
2350    }
2351    EndPolyline();
2352}
2353
[f433fda]2354void GfxCore::AddQuadrilateral(const Vector3 &a, const Vector3 &b,
[14acdae]2355                               const Vector3 &c, const Vector3 &d)
[da6c802]2356{
2357    Vector3 normal = (a - c) * (d - b);
2358    normal.normalise();
2359    Double factor = dot(normal, light) * .3 + .7;
[f336ab9]2360    glaTexCoord w(ceil(((b - a).magnitude() + (d - c).magnitude()) * .5));
2361    glaTexCoord h(ceil(((b - c).magnitude() + (d - a).magnitude()) * .5));
[9b57c71b]2362    // FIXME: should plot triangles instead to avoid rendering glitches.
[da6c802]2363    BeginQuadrilaterals();
[b839829]2364    PlaceVertexWithColour(a, 0, 0, factor);
2365    PlaceVertexWithColour(b, w, 0, factor);
2366    PlaceVertexWithColour(c, w, h, factor);
2367    PlaceVertexWithColour(d, 0, h, factor);
[da6c802]2368    EndQuadrilaterals();
2369}
2370
2371void GfxCore::AddQuadrilateralDepth(const Vector3 &a, const Vector3 &b,
2372                                    const Vector3 &c, const Vector3 &d)
[2b02270]2373{
2374    Vector3 normal = (a - c) * (d - b);
2375    normal.normalise();
2376    Double factor = dot(normal, light) * .3 + .7;
2377    int a_band, b_band, c_band, d_band;
[d67450e]2378    a_band = GetDepthColour(a.GetZ());
[97ea48d]2379    a_band = min(max(a_band, 0), GetNumColourBands());
[d67450e]2380    b_band = GetDepthColour(b.GetZ());
[97ea48d]2381    b_band = min(max(b_band, 0), GetNumColourBands());
[d67450e]2382    c_band = GetDepthColour(c.GetZ());
[97ea48d]2383    c_band = min(max(c_band, 0), GetNumColourBands());
[d67450e]2384    d_band = GetDepthColour(d.GetZ());
[97ea48d]2385    d_band = min(max(d_band, 0), GetNumColourBands());
[97fb83a]2386    // All this splitting is incorrect - we need to make a separate polygon
2387    // for each depth band...
[f336ab9]2388    glaTexCoord w(ceil(((b - a).magnitude() + (d - c).magnitude()) * .5));
2389    glaTexCoord h(ceil(((b - c).magnitude() + (d - a).magnitude()) * .5));
[b5d64e6]2390    BeginPolygon();
[d67450e]2391////    PlaceNormal(normal);
[b839829]2392    PlaceVertexWithDepthColour(a, 0, 0, factor);
[2b02270]2393    if (a_band != b_band) {
[b5d64e6]2394        SplitLineAcrossBands(a_band, b_band, a, b, factor);
[2b02270]2395    }
[b839829]2396    PlaceVertexWithDepthColour(b, w, 0, factor);
[2b02270]2397    if (b_band != c_band) {
[b5d64e6]2398        SplitLineAcrossBands(b_band, c_band, b, c, factor);
[2b02270]2399    }
[b839829]2400    PlaceVertexWithDepthColour(c, w, h, factor);
[2b02270]2401    if (c_band != d_band) {
[b5d64e6]2402        SplitLineAcrossBands(c_band, d_band, c, d, factor);
[2b02270]2403    }
[b839829]2404    PlaceVertexWithDepthColour(d, 0, h, factor);
[2b02270]2405    if (d_band != a_band) {
[b5d64e6]2406        SplitLineAcrossBands(d_band, a_band, d, a, factor);
[2b02270]2407    }
[b5d64e6]2408    EndPolygon();
[2b02270]2409}
2410
[1ee204e]2411void GfxCore::SetColourFromDate(int date, Double factor)
[d4650b3]2412{
2413    // Set the drawing colour based on a date.
2414
[1ee204e]2415    if (date == -1) {
[2043961]2416        // Undated.
[d4650b3]2417        SetColour(GetSurfacePen(), factor);
2418        return;
2419    }
2420
[1ee204e]2421    int date_offset = date - m_Parent->GetDateMin();
[2043961]2422    if (date_offset == 0) {
2423        // Earliest date - handle as a special case for the single date case.
2424        SetColour(GetPen(0), factor);
2425        return;
2426    }
[d4650b3]2427
[2043961]2428    int date_ext = m_Parent->GetDateExtent();
[d4650b3]2429    Double how_far = (Double)date_offset / date_ext;
2430    assert(how_far >= 0.0);
2431    assert(how_far <= 1.0);
2432
[97ea48d]2433    int band = int(floor(how_far * (GetNumColourBands() - 1)));
[d4650b3]2434    GLAPen pen1 = GetPen(band);
[97ea48d]2435    if (band < GetNumColourBands() - 1) {
[d4650b3]2436        const GLAPen& pen2 = GetPen(band + 1);
2437
[97ea48d]2438        Double interval = date_ext / (GetNumColourBands() - 1);
[d4650b3]2439        Double into_band = date_offset / interval - band;
2440
2441//      printf("%g z_offset=%g interval=%g band=%d\n", into_band,
2442//             z_offset, interval, band);
2443        // FIXME: why do we need to clamp here?  Is it because the walls can
2444        // extend further up/down than the centre-line?
2445        if (into_band < 0.0) into_band = 0.0;
2446        if (into_band > 1.0) into_band = 1.0;
2447        assert(into_band >= 0.0);
2448        assert(into_band <= 1.0);
2449
2450        pen1.Interpolate(pen2, into_band);
2451    }
2452    SetColour(pen1, factor);
2453}
2454
[c61aa79]2455void GfxCore::AddPolylineDate(const traverse & centreline)
[d4650b3]2456{
2457    BeginPolyline();
2458    vector<PointInfo>::const_iterator i, prev_i;
2459    i = centreline.begin();
[1ee204e]2460    int date = i->GetDate();
[d4650b3]2461    SetColourFromDate(date, 1.0);
[d67450e]2462    PlaceVertex(*i);
[d4650b3]2463    prev_i = i;
2464    while (++i != centreline.end()) {
[1ee204e]2465        int newdate = i->GetDate();
[d4650b3]2466        if (newdate != date) {
2467            EndPolyline();
2468            BeginPolyline();
2469            date = newdate;
2470            SetColourFromDate(date, 1.0);
[d67450e]2471            PlaceVertex(*prev_i);
[d4650b3]2472        }
[d67450e]2473        PlaceVertex(*i);
[d4650b3]2474        prev_i = i;
2475    }
2476    EndPolyline();
2477}
2478
[1ee204e]2479static int static_date_hack; // FIXME
[d4650b3]2480
2481void GfxCore::AddQuadrilateralDate(const Vector3 &a, const Vector3 &b,
2482                                   const Vector3 &c, const Vector3 &d)
2483{
2484    Vector3 normal = (a - c) * (d - b);
2485    normal.normalise();
2486    Double factor = dot(normal, light) * .3 + .7;
2487    int w = int(ceil(((b - a).magnitude() + (d - c).magnitude()) / 2));
2488    int h = int(ceil(((b - c).magnitude() + (d - a).magnitude()) / 2));
2489    // FIXME: should plot triangles instead to avoid rendering glitches.
[b839829]2490    BeginQuadrilaterals();
[d67450e]2491////    PlaceNormal(normal);
[d4650b3]2492    SetColourFromDate(static_date_hack, factor);
[b839829]2493    PlaceVertex(a, 0, 0);
2494    PlaceVertex(b, w, 0);
2495    PlaceVertex(c, w, h);
2496    PlaceVertex(d, 0, h);
2497    EndQuadrilaterals();
[d4650b3]2498}
2499
[c61aa79]2500static double static_E_hack; // FIXME
2501
2502void GfxCore::SetColourFromError(double E, Double factor)
2503{
2504    // Set the drawing colour based on an error value.
2505
2506    if (E < 0) {
2507        SetColour(GetSurfacePen(), factor);
2508        return;
2509    }
2510
2511    Double how_far = E / MAX_ERROR;
2512    assert(how_far >= 0.0);
2513    if (how_far > 1.0) how_far = 1.0;
2514
[97ea48d]2515    int band = int(floor(how_far * (GetNumColourBands() - 1)));
[c61aa79]2516    GLAPen pen1 = GetPen(band);
[97ea48d]2517    if (band < GetNumColourBands() - 1) {
[c61aa79]2518        const GLAPen& pen2 = GetPen(band + 1);
2519
[97ea48d]2520        Double interval = MAX_ERROR / (GetNumColourBands() - 1);
[c61aa79]2521        Double into_band = E / interval - band;
2522
2523//      printf("%g z_offset=%g interval=%g band=%d\n", into_band,
2524//             z_offset, interval, band);
2525        // FIXME: why do we need to clamp here?  Is it because the walls can
2526        // extend further up/down than the centre-line?
2527        if (into_band < 0.0) into_band = 0.0;
2528        if (into_band > 1.0) into_band = 1.0;
2529        assert(into_band >= 0.0);
2530        assert(into_band <= 1.0);
2531
2532        pen1.Interpolate(pen2, into_band);
2533    }
2534    SetColour(pen1, factor);
2535}
2536
2537void GfxCore::AddQuadrilateralError(const Vector3 &a, const Vector3 &b,
2538                                    const Vector3 &c, const Vector3 &d)
2539{
2540    Vector3 normal = (a - c) * (d - b);
2541    normal.normalise();
2542    Double factor = dot(normal, light) * .3 + .7;
2543    int w = int(ceil(((b - a).magnitude() + (d - c).magnitude()) / 2));
2544    int h = int(ceil(((b - c).magnitude() + (d - a).magnitude()) / 2));
2545    // FIXME: should plot triangles instead to avoid rendering glitches.
[b839829]2546    BeginQuadrilaterals();
[c61aa79]2547////    PlaceNormal(normal);
2548    SetColourFromError(static_E_hack, factor);
[b839829]2549    PlaceVertex(a, 0, 0);
2550    PlaceVertex(b, w, 0);
2551    PlaceVertex(c, w, h);
2552    PlaceVertex(d, 0, h);
2553    EndQuadrilaterals();
[c61aa79]2554}
2555
2556void GfxCore::AddPolylineError(const traverse & centreline)
2557{
2558    BeginPolyline();
2559    SetColourFromError(centreline.E, 1.0);
2560    vector<PointInfo>::const_iterator i;
2561    for(i = centreline.begin(); i != centreline.end(); ++i) {
2562        PlaceVertex(*i);
2563    }
2564    EndPolyline();
2565}
2566
[da6c802]2567void
[ee05463]2568GfxCore::SkinPassage(const vector<XSect> & centreline)
[3ddcad8]2569{
[b3852b5]2570    assert(centreline.size() > 1);
[3ddcad8]2571    Vector3 U[4];
[ee05463]2572    XSect prev_pt_v;
[3ddcad8]2573    Vector3 last_right(1.0, 0.0, 0.0);
2574
[c61aa79]2575//  FIXME: it's not simple to set the colour of a tube based on error...
2576//    static_E_hack = something...
[ee05463]2577    vector<XSect>::const_iterator i = centreline.begin();
2578    vector<XSect>::size_type segment = 0;
[3ddcad8]2579    while (i != centreline.end()) {
2580        // get the coordinates of this vertex
[ee05463]2581        const XSect & pt_v = *i++;
[3ddcad8]2582
2583        double z_pitch_adjust = 0.0;
2584        bool cover_end = false;
2585
2586        Vector3 right, up;
2587
2588        const Vector3 up_v(0.0, 0.0, 1.0);
2589
2590        if (segment == 0) {
2591            assert(i != centreline.end());
2592            // first segment
2593
2594            // get the coordinates of the next vertex
[ee05463]2595            const XSect & next_pt_v = *i;
[3ddcad8]2596
2597            // calculate vector from this pt to the next one
[d67450e]2598            Vector3 leg_v = next_pt_v - pt_v;
[3ddcad8]2599
2600            // obtain a vector in the LRUD plane
2601            right = leg_v * up_v;
2602            if (right.magnitude() == 0) {
2603                right = last_right;
2604                // Obtain a second vector in the LRUD plane,
2605                // perpendicular to the first.
[760ad29d]2606                //up = right * leg_v;
2607                up = up_v;
[3ddcad8]2608            } else {
2609                last_right = right;
2610                up = up_v;
[da6c802]2611            }
2612
[3ddcad8]2613            cover_end = true;
[d4650b3]2614            static_date_hack = next_pt_v.GetDate();
[3ddcad8]2615        } else if (segment + 1 == centreline.size()) {
2616            // last segment
2617
2618            // Calculate vector from the previous pt to this one.
[d67450e]2619            Vector3 leg_v = pt_v - prev_pt_v;
[3ddcad8]2620
2621            // Obtain a horizontal vector in the LRUD plane.
2622            right = leg_v * up_v;
2623            if (right.magnitude() == 0) {
[d67450e]2624                right = Vector3(last_right.GetX(), last_right.GetY(), 0.0);
[3ddcad8]2625                // Obtain a second vector in the LRUD plane,
2626                // perpendicular to the first.
[760ad29d]2627                //up = right * leg_v;
2628                up = up_v;
[3ddcad8]2629            } else {
2630                last_right = right;
2631                up = up_v;
2632            }
[da6c802]2633
[3ddcad8]2634            cover_end = true;
[d4650b3]2635            static_date_hack = pt_v.GetDate();
[3ddcad8]2636        } else {
2637            assert(i != centreline.end());
2638            // Intermediate segment.
2639
2640            // Get the coordinates of the next vertex.
[ee05463]2641            const XSect & next_pt_v = *i;
[3ddcad8]2642
2643            // Calculate vectors from this vertex to the
2644            // next vertex, and from the previous vertex to
2645            // this one.
[d67450e]2646            Vector3 leg1_v = pt_v - prev_pt_v;
2647            Vector3 leg2_v = next_pt_v - pt_v;
[3ddcad8]2648
2649            // Obtain horizontal vectors perpendicular to
2650            // both legs, then normalise and average to get
2651            // a horizontal bisector.
2652            Vector3 r1 = leg1_v * up_v;
2653            Vector3 r2 = leg2_v * up_v;
2654            r1.normalise();
2655            r2.normalise();
2656            right = r1 + r2;
2657            if (right.magnitude() == 0) {
2658                // This is the "mid-pitch" case...
2659                right = last_right;
2660            }
2661            if (r1.magnitude() == 0) {
2662                Vector3 n = leg1_v;
2663                n.normalise();
[d67450e]2664                z_pitch_adjust = n.GetZ();
2665                //up = Vector3(0, 0, leg1_v.GetZ());
[760ad29d]2666                //up = right * up;
2667                up = up_v;
[3ddcad8]2668
2669                // Rotate pitch section to minimise the
2670                // "tortional stress" - FIXME: use
2671                // triangles instead of rectangles?
2672                int shift = 0;
2673                Double maxdotp = 0;
2674
2675                // Scale to unit vectors in the LRUD plane.
2676                right.normalise();
2677                up.normalise();
2678                Vector3 vec = up - right;
2679                for (int orient = 0; orient <= 3; ++orient) {
[d67450e]2680                    Vector3 tmp = U[orient] - prev_pt_v;
[3ddcad8]2681                    tmp.normalise();
2682                    Double dotp = dot(vec, tmp);
2683                    if (dotp > maxdotp) {
2684                        maxdotp = dotp;
2685                        shift = orient;
2686                    }
2687                }
2688                if (shift) {
2689                    if (shift != 2) {
2690                        Vector3 temp(U[0]);
[b3852b5]2691                        U[0] = U[shift];
2692                        U[shift] = U[2];
2693                        U[2] = U[shift ^ 2];
2694                        U[shift ^ 2] = temp;
[ee7af72]2695                    } else {
[3ddcad8]2696                        swap(U[0], U[2]);
2697                        swap(U[1], U[3]);
[ee7af72]2698                    }
[3ddcad8]2699                }
2700#if 0
2701                // Check that the above code actually permuted
2702                // the vertices correctly.
2703                shift = 0;
2704                maxdotp = 0;
[b3852b5]2705                for (int j = 0; j <= 3; ++j) {
[d67450e]2706                    Vector3 tmp = U[j] - prev_pt_v;
[3ddcad8]2707                    tmp.normalise();
2708                    Double dotp = dot(vec, tmp);
2709                    if (dotp > maxdotp) {
2710                        maxdotp = dotp + 1e-6; // Add small tolerance to stop 45 degree offset cases being flagged...
[b3852b5]2711                        shift = j;
[da6c802]2712                    }
[3ddcad8]2713                }
2714                if (shift) {
2715                    printf("New shift = %d!\n", shift);
2716                    shift = 0;
2717                    maxdotp = 0;
[b3852b5]2718                    for (int j = 0; j <= 3; ++j) {
[d67450e]2719                        Vector3 tmp = U[j] - prev_pt_v;
[3ddcad8]2720                        tmp.normalise();
2721                        Double dotp = dot(vec, tmp);
[b3852b5]2722                        printf("    %d : %.8f\n", j, dotp);
[da6c802]2723                    }
2724                }
[3ddcad8]2725#endif
2726            } else if (r2.magnitude() == 0) {
2727                Vector3 n = leg2_v;
2728                n.normalise();
[d67450e]2729                z_pitch_adjust = n.GetZ();
2730                //up = Vector3(0, 0, leg2_v.GetZ());
[760ad29d]2731                //up = right * up;
2732                up = up_v;
[3ddcad8]2733            } else {
2734                up = up_v;
[da6c802]2735            }
[3ddcad8]2736            last_right = right;
[d4650b3]2737            static_date_hack = pt_v.GetDate();
[da6c802]2738        }
2739
[3ddcad8]2740        // Scale to unit vectors in the LRUD plane.
2741        right.normalise();
2742        up.normalise();
[33b2094]2743
[3ddcad8]2744        if (z_pitch_adjust != 0) up += Vector3(0, 0, fabs(z_pitch_adjust));
[ce2f3ce]2745
[57a3cd4]2746        Double l = fabs(pt_v.GetL());
2747        Double r = fabs(pt_v.GetR());
2748        Double u = fabs(pt_v.GetU());
2749        Double d = fabs(pt_v.GetD());
[3ddcad8]2750
2751        // Produce coordinates of the corners of the LRUD "plane".
2752        Vector3 v[4];
[d67450e]2753        v[0] = pt_v - right * l + up * u;
2754        v[1] = pt_v + right * r + up * u;
2755        v[2] = pt_v + right * r - up * d;
2756        v[3] = pt_v - right * l - up * d;
[3ddcad8]2757
2758        if (segment > 0) {
2759            (this->*AddQuad)(v[0], v[1], U[1], U[0]);
2760            (this->*AddQuad)(v[2], v[3], U[3], U[2]);
2761            (this->*AddQuad)(v[1], v[2], U[2], U[1]);
2762            (this->*AddQuad)(v[3], v[0], U[0], U[3]);
[9eb58d0]2763        }
2764
[3ddcad8]2765        if (cover_end) {
2766            (this->*AddQuad)(v[3], v[2], v[1], v[0]);
2767        }
[9eb58d0]2768
[3ddcad8]2769        prev_pt_v = pt_v;
2770        U[0] = v[0];
2771        U[1] = v[1];
2772        U[2] = v[2];
2773        U[3] = v[3];
[9eb58d0]2774
[3ddcad8]2775        ++segment;
2776    }
[33b2094]2777}
[b13aee4]2778
2779void GfxCore::FullScreenMode()
2780{
[ea940373]2781    m_Parent->ViewFullScreen();
[b13aee4]2782}
[fdfa926]2783
2784bool GfxCore::IsFullScreen() const
2785{
2786    return m_Parent->IsFullScreen();
2787}
[1690fa9]2788
[46361bc]2789void
2790GfxCore::MoveViewer(double forward, double up, double right)
2791{
[e577f89]2792    double cT = cos(rad(m_TiltAngle));
2793    double sT = sin(rad(m_TiltAngle));
2794    double cP = cos(rad(m_PanAngle));
2795    double sP = sin(rad(m_PanAngle));
[46361bc]2796    Vector3 v_forward(cT * sP, cT * cP, -sT);
[867a1141]2797    Vector3 v_up(-sT * sP, -sT * cP, -cT);
2798    Vector3 v_right(-cP, sP, 0);
[d4a5aaf]2799    assert(fabs(dot(v_forward, v_up)) < 1e-6);
2800    assert(fabs(dot(v_forward, v_right)) < 1e-6);
2801    assert(fabs(dot(v_right, v_up)) < 1e-6);
[46361bc]2802    Vector3 move = v_forward * forward + v_up * up + v_right * right;
[d67450e]2803    AddTranslation(-move);
[d877aa2]2804    // Show current position.
[d67450e]2805    m_Parent->SetCoords(m_Parent->GetOffset() - GetTranslation());
[46361bc]2806    ForceRefresh();
2807}
2808
[1690fa9]2809PresentationMark GfxCore::GetView() const
2810{
[d67450e]2811    return PresentationMark(GetTranslation() + m_Parent->GetOffset(),
2812                            m_PanAngle, m_TiltAngle, m_Scale);
[1690fa9]2813}
2814
2815void GfxCore::SetView(const PresentationMark & p)
2816{
2817    m_SwitchingTo = 0;
[d67450e]2818    SetTranslation(p - m_Parent->GetOffset());
[1690fa9]2819    m_PanAngle = p.angle;
2820    m_TiltAngle = p.tilt_angle;
[08253d9]2821    SetRotation(m_PanAngle, m_TiltAngle);
[1690fa9]2822    SetScale(p.scale);
2823    ForceRefresh();
2824}
2825
[128fac4]2826void GfxCore::PlayPres(double speed, bool change_speed) {
2827    if (!change_speed || presentation_mode == 0) {
2828        if (speed == 0.0) {
2829            presentation_mode = 0;
2830            return;
2831        }
2832        presentation_mode = PLAYING;
2833        next_mark = m_Parent->GetPresMark(MARK_FIRST);
2834        SetView(next_mark);
2835        next_mark_time = 0; // There already!
2836        this_mark_total = 0;
2837        pres_reverse = (speed < 0);
2838    }
2839
[d67450e]2840    if (change_speed) pres_speed = speed;
2841
[128fac4]2842    if (speed != 0.0) {
2843        bool new_pres_reverse = (speed < 0);
2844        if (new_pres_reverse != pres_reverse) {
2845            pres_reverse = new_pres_reverse;
2846            if (pres_reverse) {
2847                next_mark = m_Parent->GetPresMark(MARK_PREV);
2848            } else {
2849                next_mark = m_Parent->GetPresMark(MARK_NEXT);
2850            }
2851            swap(this_mark_total, next_mark_time);
2852        }
2853    }
[1690fa9]2854}
[6a4cdcb6]2855
[da6c802]2856void GfxCore::SetColourBy(int colour_by) {
2857    m_ColourBy = colour_by;
2858    switch (colour_by) {
2859        case COLOUR_BY_DEPTH:
2860            AddQuad = &GfxCore::AddQuadrilateralDepth;
2861            AddPoly = &GfxCore::AddPolylineDepth;
2862            break;
[d4650b3]2863        case COLOUR_BY_DATE:
2864            AddQuad = &GfxCore::AddQuadrilateralDate;
2865            AddPoly = &GfxCore::AddPolylineDate;
2866            break;
[c61aa79]2867        case COLOUR_BY_ERROR:
2868            AddQuad = &GfxCore::AddQuadrilateralError;
2869            AddPoly = &GfxCore::AddPolylineError;
2870            break;
[da6c802]2871        default: // case COLOUR_BY_NONE:
2872            AddQuad = &GfxCore::AddQuadrilateral;
2873            AddPoly = &GfxCore::AddPolyline;
2874            break;
2875    }
2876
[d2fcc9b]2877    InvalidateList(LIST_UNDERGROUND_LEGS);
[c61aa79]2878    InvalidateList(LIST_SURFACE_LEGS);
[d2fcc9b]2879    InvalidateList(LIST_TUBES);
[da6c802]2880
2881    ForceRefresh();
2882}
2883
[6a4cdcb6]2884bool GfxCore::ExportMovie(const wxString & fnm)
2885{
2886    int width;
2887    int height;
2888    GetSize(&width, &height);
[028829f]2889    // Round up to next multiple of 2 (required by ffmpeg).
2890    width += (width & 1);
[6a4cdcb6]2891    height += (height & 1);
2892
[75d4a2b]2893    movie = new MovieMaker();
[6a4cdcb6]2894
[98a3786]2895    // FIXME: This should really use fn_str() - currently we probably can't
2896    // save to a Unicode path on wxmsw.
[75d4a2b]2897    if (!movie->Open(fnm.mb_str(), width, height)) {
[091069f]2898        wxGetApp().ReportError(wxString(movie->get_error_string(), wxConvUTF8));
[75d4a2b]2899        delete movie;
2900        movie = 0;
[6a4cdcb6]2901        return false;
2902    }
[f433fda]2903
[d10d369]2904    PlayPres(1);
[6a4cdcb6]2905    return true;
2906}
[223f1ad]2907
[ce403f1]2908void
2909GfxCore::OnPrint(const wxString &filename, const wxString &title,
2910                 const wxString &datestamp)
2911{
2912    svxPrintDlg * p;
2913    p = new svxPrintDlg(m_Parent, filename, title, datestamp,
2914                        m_PanAngle, m_TiltAngle,
[5940815]2915                        m_Names, m_Crosses, m_Legs, m_Surface,
2916                        true);
[ce403f1]2917    p->Show(TRUE);
2918}
2919
[5940815]2920void
[223f1ad]2921GfxCore::OnExport(const wxString &filename, const wxString &title)
2922{
[5940815]2923    svxPrintDlg * p;
[5627cbb]2924    p = new svxPrintDlg(m_Parent, filename, title, wxString(),
[5940815]2925                        m_PanAngle, m_TiltAngle,
2926                        m_Names, m_Crosses, m_Legs, m_Surface,
2927                        false);
2928    p->Show(TRUE);
[223f1ad]2929}
[e2c1671]2930
2931static wxCursor
2932make_cursor(const unsigned char * bits, const unsigned char * mask,
2933            int hotx, int hoty)
2934{
[b72f4b5]2935#if defined __WXMSW__ || defined __WXMAC__
[7f3fe6d]2936    wxBitmap cursor_bitmap(reinterpret_cast<const char *>(bits), 32, 32);
2937    wxBitmap mask_bitmap(reinterpret_cast<const char *>(mask), 32, 32);
2938    cursor_bitmap.SetMask(new wxMask(mask_bitmap));
[e2c1671]2939    wxImage cursor_image = cursor_bitmap.ConvertToImage();
2940    cursor_image.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_X, hotx);
2941    cursor_image.SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y, hoty);
[7f3fe6d]2942    return wxCursor(cursor_image);
[e2c1671]2943#else
2944    return wxCursor((const char *)bits, 32, 32, hotx, hoty,
2945                    (const char *)mask, wxWHITE, wxBLACK);
2946#endif
2947}
2948
2949const
2950#include "hand.xbm"
2951const
2952#include "handmask.xbm"
2953
2954const
2955#include "brotate.xbm"
2956const
2957#include "brotatemask.xbm"
2958
2959const
2960#include "vrotate.xbm"
2961const
2962#include "vrotatemask.xbm"
2963
2964const
2965#include "rotate.xbm"
2966const
2967#include "rotatemask.xbm"
2968
[ecf2d23]2969const
2970#include "rotatezoom.xbm"
2971const
2972#include "rotatezoommask.xbm"
2973
[e2c1671]2974void
2975GfxCore::SetCursor(GfxCore::cursor new_cursor)
2976{
2977    // Check if we're already showing that cursor.
2978    if (current_cursor == new_cursor) return;
2979
2980    current_cursor = new_cursor;
2981    switch (current_cursor) {
2982        case GfxCore::CURSOR_DEFAULT:
2983            GLACanvas::SetCursor(wxNullCursor);
2984            break;
2985        case GfxCore::CURSOR_POINTING_HAND:
2986            GLACanvas::SetCursor(wxCursor(wxCURSOR_HAND));
2987            break;
2988        case GfxCore::CURSOR_DRAGGING_HAND:
2989            GLACanvas::SetCursor(make_cursor(hand_bits, handmask_bits, 12, 18));
2990            break;
2991        case GfxCore::CURSOR_HORIZONTAL_RESIZE:
2992            GLACanvas::SetCursor(wxCursor(wxCURSOR_SIZEWE));
2993            break;
2994        case GfxCore::CURSOR_ROTATE_HORIZONTALLY:
2995            GLACanvas::SetCursor(make_cursor(rotate_bits, rotatemask_bits, 15, 15));
2996            break;
2997        case GfxCore::CURSOR_ROTATE_VERTICALLY:
2998            GLACanvas::SetCursor(make_cursor(vrotate_bits, vrotatemask_bits, 15, 15));
2999            break;
3000        case GfxCore::CURSOR_ROTATE_EITHER_WAY:
3001            GLACanvas::SetCursor(make_cursor(brotate_bits, brotatemask_bits, 15, 15));
3002            break;
3003        case GfxCore::CURSOR_ZOOM:
3004            GLACanvas::SetCursor(wxCursor(wxCURSOR_MAGNIFIER));
3005            break;
[ecf2d23]3006        case GfxCore::CURSOR_ZOOM_ROTATE:
3007            GLACanvas::SetCursor(make_cursor(rotatezoom_bits, rotatezoommask_bits, 15, 15));
3008            break;
[e2c1671]3009    }
3010}
[6b061db]3011
3012bool GfxCore::MeasuringLineActive() const
3013{
3014    if (Animating()) return false;
3015    return (m_here.IsValid() && !m_here_is_temporary) || m_there.IsValid();
3016}
[acdb8aa]3017
3018bool GfxCore::HandleRClick(wxPoint point)
3019{
3020    if (PointWithinCompass(point)) {
3021        // Pop up menu.
3022        wxMenu menu;
[055bfc58]3023        menu.Append(menu_ORIENT_MOVE_NORTH, wmsg(/*View &North*/240));
3024        menu.Append(menu_ORIENT_MOVE_EAST, wmsg(/*View &East*/241));
3025        menu.Append(menu_ORIENT_MOVE_SOUTH, wmsg(/*View &South*/242));
3026        menu.Append(menu_ORIENT_MOVE_WEST, wmsg(/*View &West*/243));
3027        menu.AppendSeparator();
[acdb8aa]3028        menu.AppendCheckItem(menu_IND_COMPASS, wmsg(/*&Hide Compass*/344));
3029        menu.AppendCheckItem(menu_CTL_DEGREES, wmsg(/*&Degrees*/343));
3030        menu.Connect(wxEVT_COMMAND_MENU_SELECTED, (wxObjectEventFunction)&MainFrm::ProcessEvent, NULL, m_Parent);
3031        PopupMenu(&menu);
3032        return true;
3033    }
3034
3035    if (PointWithinClino(point)) {
3036        // Pop up menu.
3037        wxMenu menu;
[055bfc58]3038        menu.Append(menu_ORIENT_PLAN, wmsg(/*&Plan View*/248));
3039        menu.Append(menu_ORIENT_ELEVATION, wmsg(/*Ele&vation*/249));
3040        menu.AppendSeparator();
[acdb8aa]3041        menu.AppendCheckItem(menu_IND_CLINO, wmsg(/*&Hide Clino*/384));
3042        menu.AppendCheckItem(menu_CTL_DEGREES, wmsg(/*&Degrees*/343));
3043        menu.Connect(wxEVT_COMMAND_MENU_SELECTED, (wxObjectEventFunction)&MainFrm::ProcessEvent, NULL, m_Parent);
3044        PopupMenu(&menu);
3045        return true;
3046    }
3047
3048    if (PointWithinScaleBar(point)) {
3049        // Pop up menu.
3050        wxMenu menu;
3051        menu.AppendCheckItem(menu_IND_SCALE_BAR, wmsg(/*&Hide scale bar*/385));
3052        menu.AppendCheckItem(menu_CTL_METRIC, wmsg(/*&Metric*/342));
3053        menu.Connect(wxEVT_COMMAND_MENU_SELECTED, (wxObjectEventFunction)&MainFrm::ProcessEvent, NULL, m_Parent);
3054        PopupMenu(&menu);
3055        return true;
3056    }
3057
3058#if 0 // FIXME: Don't yet have PointWithinColourKey() method.
3059    if (PointWithinColourKey(point)) {
3060        // Pop up menu.
3061        wxMenu menu;
[97ea48d]3062        menu.AppendCheckItem(menu_IND_COLOUR_KEY, wmsg(/*&Hide colour key*/386));
[acdb8aa]3063        menu.Connect(wxEVT_COMMAND_MENU_SELECTED, (wxObjectEventFunction)&MainFrm::OnToggleMetric, NULL, m_Parent);
3064        PopupMenu(&menu);
3065        return true;
3066    }
3067#endif
3068
3069    return false;
3070}
Note: See TracBrowser for help on using the repository browser.