source: git/src/gfxcore.cc @ 5f50488

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

src/gfxcore.cc: Increase scale bar maximum width from 65% of the
window width to 75% as it was in 1.0.x, but if that would overlap
the clino then reduce it down until it reaches 50%. Allow zooming
in a little further to match what was possible in 1.0.x. (ticket#23)

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

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