source: git/src/gfxcore.cc @ aea4f8b

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

src/gfxcore.cc,src/gla-gl.cc,src/gla.h: Eliminate glReadPixels() call
from src/gfxcore.cc.

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

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