source: git/src/gfxcore.cc @ f336ab9

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

src/gfxcore.cc,src/gla-gl.cc,src/gla.h: New type GLATexCoord to avoid
using GLint outside of the OpenGL-specific code.

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

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