source: git/src/gfxcore.cc @ 880b954

RELEASE/1.1RELEASE/1.2debug-cidebug-ci-sanitisersstereowalls-data
Last change on this file since 880b954 was 880b954, checked in by Olly Betts <olly@…>, 14 years ago

doc/manual.sgml,src/aventreectrl.cc,src/gfxcore.cc,src/mainfrm.h:
Correctly capitalise "GTK".

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