My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes    
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
// cs
//
// Copyright (c) 2008 Scott Ellington and Authors
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
//

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Threading;

using Gtk;

using Tao.OpenGl;

using Cumberland;

namespace Appomattox
{
public class MapWidget : GLWidget, IMapControllable
{

#region variables

Coordinate eye = new Coordinate(0,0,0); /* Position of our eye or camera */

// the field of view y angle
double fovy = 90;

OpenGlMapDrawer renderer = new OpenGlMapDrawer();

bool mouseDown = false;

Cumberland.Point lastSpot = new Cumberland.Point();

Cumberland.Rectangle actualExtents = new Rectangle();

double scale = 1;
double scrollPace = 6;
int refresh_cnt = 0;

MapController mapController;

volatile bool stopAnimation = false;

bool animatedZooming = false;

Point mouseLocation = new Point();

bool syncing = false;

#endregion

#region properties

public double Scale {
get {
return scale;
}
}

// public bool AnimatedZooming {
// get {
// return animatedZooming;
// }
// set {
// animatedZooming = value;
// }
// }

public Point MouseLocation {
get {
return mouseLocation;
}
}

#endregion

#region ctor

public MapWidget()
{
//DoubleBuffered = true;
AddEvents((int)Gdk.EventMask.ExposureMask |
(int) Gdk.EventMask.LeaveNotifyMask |
(int) Gdk.EventMask.ButtonPressMask |
(int) Gdk.EventMask.ButtonReleaseMask |
(int) Gdk.EventMask.PointerMotionMask |
(int) Gdk.EventMask.PointerMotionHintMask |
(int) Gdk.EventMask.EnterNotifyMask |
(int) Gdk.EventMask.LeaveNotifyMask);

Realized += GlRealizedHandler;
Render += GLRenderHandler;
ButtonPressEvent += GLMouseHandler;
ScrollEvent += GlMouseWheelHandler;
SizeAllocated += GlSizeAllocatedHandler;
ButtonReleaseEvent += GlButtonReleaseHandler;
}

#endregion

#region event handlers

void GlSizeAllocatedHandler(object o, SizeAllocatedArgs e)
{
// don't set up perspective if not realized (Realized handler will call)
if (IsRealized)
{
GlSetupPerspective(e.Allocation.Width, e.Allocation.Height);
}
}

void GlMouseWheelHandler(object o, ScrollEventArgs e)
{
stopAnimation = true;

// we want to move the equivalent of one pixel times our pace

if (e.Event.Direction == Gdk.ScrollDirection.Down)
{
eye.Z += scale * scrollPace;
}
else if (e.Event.Direction == Gdk.ScrollDirection.Up)
{
eye.Z -= scale * scrollPace;
}

// extents have changed

double mapHeight = (eye.Z * Math.Tan((Math.PI/180) * fovy/2)) * 2;
double ratio = mapHeight / actualExtents.Height;
Cumberland.Rectangle r = actualExtents.Clone();
r.Height *= ratio;
r.Width *= ratio;
actualExtents = r;

CalculateScale();

SyncMapExtents();

RequestRenderMap();
}

void GlRealizedHandler(object o, EventArgs ev)
{
// we need to acquire the openGL context
MakeCurrent();

// don't enable depth. it doesn't look as good
//Gl.glEnable ( Gl.GL_DEPTH_TEST );

// enable fill
Gl.glPolygonMode ( Gl.GL_FRONT_AND_BACK, Gl.GL_FILL );

// set clear color
Gl.glClearColor( 1.0f, 1.0f, 1.0f, 1.0f );

// set to map extents
SetCameraToExtents();

// set up perspective initially
GlSetupPerspective(Allocation.Width, Allocation.Height);
}

void GLMouseHandler(object o, ButtonPressEventArgs e)
{
stopAnimation = true;

if (e.Event.Type == Gdk.EventType.TwoButtonPress)
{
int h = Allocation.Height;

// calculate the final point of the zoom in
Point finalPoint = new Point((actualExtents.Min.X + (e.Event.X * scale)),
(actualExtents.Min.Y + ((h - e.Event.Y) * scale)));

if (!animatedZooming)
{
eye = new Coordinate(finalPoint.X, finalPoint.Y, eye.Z/3);

SetExtentsToEye();
SyncMapExtents();
RequestRenderMap();
}
else
{
// Animated zoom in

List<Coordinate> coords = new List<Coordinate>();
double zscale = scale;
Coordinate animEye = eye.Clone();

Point center = actualExtents.Center.Clone();

// calculate a normalized vector from the current x/y loc
// to the final point
// we use this to get the direction that each animation
// will use in x/y coords
Point vectorPoint = new Point(finalPoint.X-center.X,
finalPoint.Y-center.Y);
double distance = Math.Sqrt(Math.Pow(vectorPoint.X, 2) +
Math.Pow(vectorPoint.Y, 2));
Point normalizedVector = new Point(vectorPoint.X / distance,
vectorPoint.Y / distance);

double floor = animEye.Z / 3;

// store the ratio of the z distance to the x/y offset
double tanb = distance / floor;

while (animEye.Z > floor)
{
// calculate the x/y offset
double offsetLength = zscale * tanb;

animEye.X += normalizedVector.X * offsetLength;
animEye.Y += normalizedVector.Y * offsetLength;
animEye.Z -= zscale;

coords.Add(animEye.Clone());

double mapHeight = (animEye.Z * Math.Tan((Math.PI/180) * fovy/2)) * 2;
zscale = mapHeight / h;
}

stopAnimation = false;

ThreadPool.QueueUserWorkItem(new WaitCallback(FlyZoomInCallback), coords);
}
}
else mouseDown = e.Event.Button == 1;
}

void FlyZoomInCallback(object obj)
{
List<Coordinate> coords = obj as List<Coordinate>;

foreach (Coordinate coord in coords)
{
if (stopAnimation) return;

eye = coord;

// // extents have changes
// double mapHeight = (eye.Z * Math.Tan((Math.PI/180) * fovy/2)) * 2;
// double ratio = mapHeight / actualExtents.Height;
//
// Cumberland.Rectangle r = actualExtents.Clone();
// r.Center.X = eye.X;
// r.Center.Y = eye.Y;
// r.Height *= ratio;
// r.Width *= ratio;
// actualExtents = r;
//
// CalculateScale();

SetExtentsToEye();

Application.Invoke(delegate (object o, EventArgs e) {
SyncMapExtents();
RequestRenderMap();
});

Thread.Sleep(5);
}
}

void GLRenderHandler(object o, EventArgs e)
{
Gl.glClear(Gl.GL_COLOR_BUFFER_BIT | Gl.GL_DEPTH_BUFFER_BIT);

if (mapController == null) return;

// create our view matrix and load into OpenGL
// (currently no rotating of the view)
double[] matrix = MatrixHelper.Transform ( 0, 0, 0, eye.X, eye.Y, eye.Z );
matrix = MatrixHelper.Inverse ( matrix );
Gl.glLoadMatrixd ( matrix );

// render
renderer.Render(mapController, true);

// flush out what in the OpenGL context
Gl.glFlush ();
}

void GlButtonReleaseHandler(object o, ButtonReleaseEventArgs e)
{
mouseDown = false;
}

void LayerAddedHandler(object o, LayerEventArgs e)
{
RequestRenderMap(true);
}

void LayerRemovedHandler(object o, LayerEventArgs e)
{
renderer.DeleteGlList(e.Layer.Id);

RequestRenderMap();
}


void LayerMovedHandler(object o, LayerEventArgs e)
{
RequestRenderMap(true);
}

#endregion

#region overrides

protected override bool OnMotionNotifyEvent(Gdk.EventMotion evnt)
{
if (mouseDown)
{
Cumberland.Point vector = new Cumberland.Point(lastSpot.X-evnt.X,
lastSpot.Y-evnt.Y);

// convert map units vector to pixels

double xoffset = vector.X * scale;
eye.X += xoffset;

double yoffset = vector.Y * scale; // map's origin is bottom-left
eye.Y -= yoffset;

// update extents
Cumberland.Point center = actualExtents.Center;
actualExtents.Center = new Cumberland.Point(center.X + xoffset,
center.Y - yoffset);

SyncMapExtents();

RequestRenderMap();
}

lastSpot.X = evnt.X;
lastSpot.Y = evnt.Y;

mouseLocation = new Point((actualExtents.Min.X + (evnt.X * scale)),
(actualExtents.Min.Y + ((Allocation.Height - evnt.Y) * scale)));

return base.OnMotionNotifyEvent (evnt);
}

void HandleLayerPropertyChanged(object sender, LayerEventArgs e)
{
if (e.PropertyName == "Visible")
{
RequestRenderMap();
}
else if (e.PropertyName == "Style")
{
Console.WriteLine(e.PropertyName);
renderer.DeleteGlList(e.Layer.Id);

RequestRenderMap(true);
}
}

void HandlePropertyChanged(object sender, PropertyChangedEventArgs e)
{
if (e.PropertyName == "Extents")
{
// only handle if we did not create this extents change
if (!syncing)
{
SetCameraToExtents(mapController.Extents);
RequestRenderMap(true);
}
}
else if (e.PropertyName == "Projection")
{
renderer.DeleteAllGlLists();
RequestRenderMap(true);
}
else if (e.PropertyName == "BackgroundColor")
{
SetBackgroundColor();
RequestRenderMap();
}
}

#endregion

#region private methods

void SetExtentsToEye()
{
// extents have changes
double mapHeight = (eye.Z * Math.Tan((Math.PI/180) * fovy/2)) * 2;
double ratio = mapHeight / actualExtents.Height;

Cumberland.Rectangle r = actualExtents.Clone();

r.Center = new Point(eye.X, eye.Y);
r.Height *= ratio;
r.Width *= ratio;

actualExtents = r;

CalculateScale();
}

void GlSetupPerspective(int w, int h)
{
// Note: setting up perspective must not change the map scale
// this means not changing eye.Z

actualExtents = new Rectangle(actualExtents.Min.X,
actualExtents.Max.Y-(h*scale),
actualExtents.Min.X+(w*scale),
actualExtents.Max.Y);

eye.X = actualExtents.Center.X;
eye.Y = actualExtents.Center.Y;

SyncMapExtents();

// calculate field of view y with our eye.Z, and new map height
// field of view x will be taken care of by setting aspect ratio
fovy = Math.Atan((actualExtents.Height/2) / eye.Z) *
(180/Math.PI) * 2; // convert to degrees and double

// set our viewport to the window dimensions
Gl.glViewport(0, 0, w, h);
Gl.glMatrixMode(Gl.GL_PROJECTION);
Gl.glLoadIdentity();

// we use a perspective projection in order to zoom in and out
Glu.gluPerspective (fovy, Convert.ToDouble(w) / Convert.ToDouble(h), 0.000000000001, double.MaxValue);

Gl.glMatrixMode(Gl.GL_MODELVIEW);
Gl.glLoadIdentity();
}

void SetCameraToExtents()
{
SetCameraToExtents(actualExtents);
}

void CalculateScale()
{
scale = actualExtents.Height / Allocation.Height; // scale = map units / pixels
}

bool RefreshGl()
{
RequestRenderMap();

return ++refresh_cnt < 6;
}

void SyncMapExtents()
{
// set a flag to keep this widget from processing the extents changed handler
syncing = true;

mapController.Extents = actualExtents;

syncing = false;
}

void SetCameraToExtents(Cumberland.Rectangle r)
{
// we need to expand our extents so that the aspect ratio is the same as the viewport
actualExtents = r.Clone();
actualExtents.AspectRatioOfWidth = Convert.ToDouble(Allocation.Width) /
Convert.ToDouble(Allocation.Height);

// scale has changed
CalculateScale();

// notify consumers
SyncMapExtents();

// set initial "eye" point in 3d space

// x/y is simply map center point
Cumberland.Point c = actualExtents.Center;
eye.X = c.X;
eye.Y = c.Y;

// trigonometry to acquire z coordinate
// by using the right triangle created by:
// 1) the center point of the map,
// 2) the top-center of the map
// 3) the eye
// there we can calculate the side (z-value) by
// b = a/Tan(A)
eye.Z = (actualExtents.Height / 2) / Math.Tan((Math.PI/180) * fovy/2);
}

void RequestRenderMap()
{
QueueDraw();
}

void RequestRenderMap(bool poll)
{
if (poll)
{
refresh_cnt = 0;
GLib.Timeout.Add(200, RefreshGl);
}
else RequestRenderMap();
}

void SetBackgroundColor()
{
if (mapController.BackgroundColor.A > 0)
{
Gl.glClearColor(Convert.ToSingle(mapController.BackgroundColor.R)/255,
Convert.ToSingle(mapController.BackgroundColor.G)/255,
Convert.ToSingle(mapController.BackgroundColor.B)/255,
Convert.ToSingle(mapController.BackgroundColor.A)/255);
}
}

#endregion

#region public methods

public void Bind (MapController controller)
{
this.mapController = controller;

mapController.LayerAdded += LayerAddedHandler;
mapController.LayerRemoved += LayerRemovedHandler;
// mapController.LayerSymbologyChanged += LayerSymbologyChangedHandler;
mapController.LayerMoved += LayerMovedHandler;
// mapController.LayerVisibilityChanged += HandleLayerVisibilityChanged;
mapController.PropertyChanged += HandlePropertyChanged;
mapController.LayerPropertyChanged += HandleLayerPropertyChanged;

SetBackgroundColor();

SetCameraToExtents(mapController.Extents);
}

public void Unbind ()
{
mapController.LayerAdded -= LayerAddedHandler;
mapController.LayerRemoved -= LayerRemovedHandler;
// mapController.LayerSymbologyChanged -= LayerSymbologyChangedHandler;
mapController.LayerMoved -= LayerMovedHandler;
// mapController.LayerVisibilityChanged -= HandleLayerVisibilityChanged;
mapController.PropertyChanged -= HandlePropertyChanged;
mapController.LayerPropertyChanged -= HandleLayerPropertyChanged;

mapController = null;

renderer.DeleteAllGlLists();

RequestRenderMap();
}

#endregion
}
}

Change log

r49 by scott.ellington on Jan 5, 2009   Diff
* appomattox/Appomattox/AxUtils.cs:
* appomattox/Appomattox/MapWidget.cs:
* appomattox/Appomattox/MainWindow.cs:
* appomattox/Appomattox/MapController.cs:
* appomattox/Appomattox/Appomattox.csproj:
* appomattox/Appomattox/LegendTreeView.cs:
*
appomattox/Appomattox/OpenGlMapDrawer.cs:
* appomattox/Appomattox/gtk-
gui/gui.stetic:
*
appomattox/Appomattox/LayerController.cs:
...
Go to: 
Project members, sign in to write a code review

Older revisions

r48 by scott.ellington on Jan 5, 2009   Diff
* appomattox/appomattox.userprefs:
* appomattox/Appomattox/MainWindow.cs:
* appomattox/Appomattox/Appomattox.csp
roj:
* appomattox/Appomattox/gtk-
...
r38 by scott.ellington on Dec 30, 2008   Diff
* appomattox/Appomattox/OpenGlMapDrawe
r.cs: improve

* appomattox/Appomattox/MapWidget.cs:
remove writeline
r36 by scott.ellington on Dec 30, 2008   Diff
* appomattox/Appomattox/MapWidget.cs:
add double click zoom in

* appomattox/Appomattox/Coordinate.cs:
* appomattox/Appomattox/Appomattox.csp
...
All revisions of this file

File info

Size: 14866 bytes, 581 lines
Powered by Google Project Hosting