source: git/src/gfxcore.cc @ 88707e0b

RELEASE/1.1RELEASE/1.2debug-cidebug-ci-sanitisersstereowalls-datawalls-data-hanging-as-warning
Last change on this file since 88707e0b was 88707e0b, checked in by Olly Betts <olly@…>, 14 years ago

Initialise all GfxCore?'s member variables so we don't use them uninitialised.

git-svn-id: file:///home/survex-svn/survex/branches/survex-1_1@3363 4b37db11-9a0c-4f06-9ece-9ab7cdaee568

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