forked from NatLabRockies/SAM
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paths3view.cpp
More file actions
1507 lines (1246 loc) · 37.2 KB
/
s3view.cpp
File metadata and controls
1507 lines (1246 loc) · 37.2 KB
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
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*******************************************************************************************************
* Copyright 2017 Alliance for Sustainable Energy, LLC
*
* NOTICE: This software was developed at least in part by Alliance for Sustainable Energy, LLC
* (“Alliance”) under Contract No. DE-AC36-08GO28308 with the U.S. Department of Energy and the U.S.
* The Government retains for itself and others acting on its behalf a nonexclusive, paid-up,
* irrevocable worldwide license in the software to reproduce, prepare derivative works, distribute
* copies to the public, perform publicly and display publicly, and to permit others to do so.
*
* Redistribution and use in source and binary forms, with or without modification, are permitted
* provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, the above government
* rights notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, the above government
* rights notice, this list of conditions and the following disclaimer in the documentation and/or
* other materials provided with the distribution.
*
* 3. The entire corresponding source code of any redistribution, with or without modification, by a
* research entity, including but not limited to any contracting manager/operator of a United States
* National Laboratory, any institution of higher learning, and any non-profit organization, must be
* made publicly available under this license for as long as the redistribution is made available by
* the research entity.
*
* 4. Redistribution of this software, without modification, must refer to the software by the same
* designation. Redistribution of a modified version of this software (i) may not refer to the modified
* version by the same designation, or by any confusingly similar designation, and (ii) must refer to
* the underlying software originally provided by Alliance as “System Advisor Model” or “SAM”. Except
* to comply with the foregoing, the terms “System Advisor Model”, “SAM”, or any confusingly similar
* designation may not be used to refer to any modified version of this software or any modified
* version of the underlying software originally provided by Alliance without the prior written consent
* of Alliance.
*
* 5. The name of the copyright holder, contributors, the United States Government, the United States
* Department of Energy, or any of their employees may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER,
* CONTRIBUTORS, UNITED STATES GOVERNMENT OR UNITED STATES DEPARTMENT OF ENERGY, NOR ANY OF THEIR
* EMPLOYEES, BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
* IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
* THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*******************************************************************************************************/
#include <algorithm>
#include <numeric>
#include <wx/spinctrl.h>
#include <wx/datstrm.h>
#include <wx/fontenum.h>
#include <wx/dcbuffer.h>
#include <wx/dcgraph.h>
#include <wx/slider.h>
#include <wx/sizer.h>
#include <wx/stattext.h>
#include <wx/statline.h>
#include <wx/button.h>
#include <wx/checkbox.h>
#include <wx/msgdlg.h>
#include <wx/progdlg.h>
#include <wx/clrpicker.h>
#include <wx/checklst.h>
#include <wx/statbmp.h>
#include <wx/generic/statbmpg.h>
#include <wx/xml/xml.h>
#include <wx/ffile.h>
#include <wx/wfstream.h>
#include <wx/sstream.h>
#include <wx/txtstrm.h>
#include <wx/tglbtn.h>
#include <wx/busyinfo.h>
#include <wx/propgrid/propgrid.h>
#include <wx/propgrid/advprops.h>
#include <wex/numeric.h>
#include <wex/plot/plplotctrl.h>
#include <wex/plot/pllineplot.h>
#include <wex/dview/dvplotctrl.h>
#include <wex/utils.h>
#include <wex/jsonreader.h>
#include <wex/easycurl.h>
//#include <curl/curl.h>
#include "s3view.h"
DEFINE_EVENT_TYPE( wxEVT_VIEW3D_UPDATE_VIEW )
DEFINE_EVENT_TYPE( wxEVT_VIEW3D_UPDATE_OBJECTS )
DEFINE_EVENT_TYPE( wxEVT_VIEW3D_UPDATE_PROPERTIES )
DEFINE_EVENT_TYPE( wxEVT_VIEW3D_UPDATE_SELECTION )
class MovingObjectDC : public VRenderer2D
{
#ifdef VIEW_USE_OVERLAY
wxDCOverlay m_overlaydc;
#endif
wxClientDC &m_dc;
View3D *m_view;
public:
MovingObjectDC( wxClientDC &dc, View3D *view )
#ifdef VIEW_USE_OVERLAY
: m_dc(dc), m_overlaydc( view->m_overlay, &dc ), m_view(view)
#else
: m_dc(dc), m_view(view)
#endif
{
#ifdef VIEW_USE_OVERLAY
m_overlaydc.Clear();
m_dc.SetPen( wxColour( 100, 100, 100 ) );
m_dc.SetBrush( wxColour( 150, 150, 150, 150 ) );
#else
m_dc.SetBrush( *wxBLACK_BRUSH );
m_dc.SetPen( *wxBLACK_PEN );
m_dc.SetLogicalFunction( wxINVERT );
#endif
}
virtual void Poly( double *X, double *YZ, size_t n )
{
wxPoint *points = new wxPoint[n];
for( size_t i=0;i<n;i++ )
{
int xs, ys;
m_view->WorldToScreen( X[i], YZ[i], &xs, &ys );
points[i].x = xs;
points[i].y = ys;
}
m_dc.DrawPolygon( n, points );
delete [] points;
}
virtual void Rect( double x, double yz, double width, double height )
{
int x0, y0, x1, y1;
m_view->WorldToScreen( x, yz, &x0, &y0 );
m_view->WorldToScreen( x+width, yz+height, &x1, &y1 );
m_dc.DrawRectangle( x0, y0, x1-x0, y1-y0 );
}
virtual void Circ( double x, double yz, double radius )
{
int xc, yc;
m_view->WorldToScreen( x, yz, &xc, &yc );
m_dc.DrawCircle( xc, yc, radius*m_view->GetScale() );
}
};
#define HANDLE_RADIUS 5
BEGIN_EVENT_TABLE( View3D, wxWindow )
EVT_PAINT( View3D::OnPaint )
EVT_SIZE( View3D::OnSize )
EVT_LEFT_DOWN( View3D::OnLeftDown )
EVT_LEFT_DCLICK( View3D::OnLeftDown )
EVT_LEFT_UP( View3D::OnLeftUp )
EVT_MOUSEWHEEL( View3D::OnWheel )
EVT_MOTION( View3D::OnMotion )
EVT_KEY_DOWN( View3D::OnKey )
END_EVENT_TABLE()
View3D::View3D( wxWindow *parent, int id, const wxPoint &pos, const wxSize &size )
: wxWindow( parent, id, pos, size, wxWANTS_CHARS )
{
// '' default values for window size ''
m_winWidth = 400;
m_winHeight = 300;
m_sf = 0.0;
m_nextSelectionIndex = 0;
m_mpp = 1;
m_mode = SPIN_VIEW;
m_origX = m_origY = 0;
m_origScale = 1;
m_origXOff = m_origYOff = m_origZOff = 0;
m_zoomMode = false;
m_panMode = false;
m_pressed = false;
m_eraseNeeded = false;
m_movingHandle = 0;
m_gridSpacing = 5;
m_snapSpacing = 0.25;
SetBackgroundStyle( wxBG_STYLE_PAINT );
RegisterType( new VActiveSurfaceObject );
RegisterType( new VBoxObject );
RegisterType( new VCylinderObject );
RegisterType( new VRoofObject );
RegisterType( new VTreeObject );
//RegisterType( new VConicalTreeObject );
m_scene.basic_axes_with_ground();
m_lastView[0].azimuth = 143;
m_lastView[0].altitude = 28;
m_transform.rotate_azal( 143, 28 );
m_transform.set_scale( 4.0 );
Render();
}
View3D::~View3D()
{
DeleteAll();
for( size_t i=0;i<m_registeredTypes.size(); i++ )
delete m_registeredTypes[i];
}
void View3D::CreateStaticDemoScene()
{
// pv panels (active area)
m_scene.type( s3d::scene::ACTIVE );
m_scene.colors( s3d::rgba( 0, 107, 186, 200 ), s3d::rgba( 0, 88, 153 ) );
m_scene.point( 0, 0, 20 ) ;
m_scene.point( 0, 15, 20 ) ;
m_scene.point( 20, 15, 0 ) ;
m_scene.point( 20, 0, 0 ) ;
m_scene.poly( 1 );
m_scene.point( 0, 17, 20 );
m_scene.point( 0, 32, 20 );
m_scene.point( 20, 32, 0 );
m_scene.point( 20, 17, 0 );
m_scene.poly( 1 );
m_scene.point( 0, 34, 20 );
m_scene.point( 0, 49, 20 );
m_scene.point( 20, 49, 0 );
m_scene.point( 20, 34, 0 );
m_scene.poly( 1 );
// build obstructions
m_scene.type( s3d::scene::OBSTRUCTION );
// pole between panels to test sorting
wxUint8 alpha = 140;
m_scene.colors( s3d::rgba(254,0,0, alpha), s3d::rgba(254,0,0, alpha) );
m_scene.conical( 2, 10, 33, 0, 25, 1, 1 );
// magenta walls
m_scene.colors( s3d::rgba(202, 11, 183, alpha), s3d::rgba(202, 11, 183, alpha) );
m_scene.box( 2, 15, -20, 0, 10, 20, 0, 10 );
// tree 1, chopped off top.
m_scene.colors( s3d::rgba(128, 64, 0, alpha), s3d::rgba(128, 64, 0, alpha) );
m_scene.conical( 2, 50, 50, 0, 15, 3, 3 );
m_scene.colors( s3d::rgba(0, 186, 107, alpha ), s3d::rgba(0, 186, 107, alpha ) );
m_scene.conical( 2, 50, 50, 15, 45, 14, 4 );
// tree 2, pointy top
m_scene.colors( s3d::rgba(128, 64, 0, alpha), s3d::rgba(128, 64, 0, alpha) );
m_scene.conical( 2, 50, 0, 0, 25, 4, 4 );
m_scene.colors( s3d::rgba(0, 186, 107, alpha ), s3d::rgba(0, 186, 107, alpha ) );
m_scene.conical( 2, 50, 0, 25, 55, 16, 0 );
// upside down blue/red thing
m_scene.colors( s3d::rgba(0,0,255, alpha), s3d::rgba(255,0,0, alpha) );
m_scene.conical( 2, 50, 25, 0, 10, 0, 4 );
Render();
Refresh();
}
static void linspace( double *v, size_t N, double start, double end )
{
double step = (end-start)/N;
for ( size_t i=0;i<N;i++ )
v[i] = start + i*step;
v[N-1] = end; // fixup for any numerical rounding in stepping
}
void View3D::Animate( double az, double al, double scale, double xo, double yo, double zo )
{
#define ANIM_STEPS 10
double az_steps[ANIM_STEPS], al_steps[ANIM_STEPS];
double sc_steps[ANIM_STEPS], xo_steps[ANIM_STEPS], yo_steps[ANIM_STEPS], zo_steps[ANIM_STEPS];
double cur_az, cur_al, cur_scale, cur_xo, cur_yo, cur_zo;
m_transform.get_azal( &cur_az, &cur_al );
cur_scale = m_transform.get_scale();
m_transform.get_offset( &cur_xo, &cur_yo, &cur_zo );
linspace( az_steps, ANIM_STEPS, cur_az, az );
linspace( al_steps, ANIM_STEPS, cur_al, al );
linspace( sc_steps, ANIM_STEPS, cur_scale, scale );
linspace( xo_steps, ANIM_STEPS, cur_xo, xo );
linspace( yo_steps, ANIM_STEPS, cur_yo, yo );
linspace( zo_steps, ANIM_STEPS, cur_zo, zo );
for ( size_t i=0;i<ANIM_STEPS;i++ )
{
m_transform.rotate_azal( az_steps[i], al_steps[i] );
m_transform.set_scale( sc_steps[i] );
m_transform.offset( xo_steps[i], yo_steps[i], zo_steps[i] );
Render();
Refresh();
SendEvent( wxEVT_VIEW3D_UPDATE_VIEW );
wxYield();
wxMilliSleep( 6 );
}
}
void View3D::SetMode( int mode )
{
if ( mode == m_mode ) return;
// save the current view mode
m_transform.get_offset( &m_lastView[m_mode].xoff,
&m_lastView[m_mode].yoff,
&m_lastView[m_mode].zoff );
m_transform.get_azal( &m_lastView[m_mode].azimuth,
&m_lastView[m_mode].altitude );
m_lastView[m_mode].scale = m_transform.get_scale();
// switch to the new view
double azi_new, alt_new;
if ( mode == TOP_VIEW )
{
azi_new = 180;
alt_new = 90;
}
else if ( mode == Z_VIEW )
{
azi_new = 180;
alt_new = 0;
}
else
{
azi_new = m_lastView[mode].azimuth;
alt_new = m_lastView[mode].altitude;
SetCursor( wxCURSOR_DEFAULT );
}
Animate( azi_new, alt_new, m_lastView[mode].scale,
m_lastView[mode].xoff,
m_lastView[mode].yoff,
m_lastView[mode].zoff );
// switch view
m_mode = mode;
UpdateAllHandles();
Render();
Refresh();
SendEvent( wxEVT_VIEW3D_UPDATE_VIEW );
}
int View3D::GetMode()
{
return m_mode;
}
void View3D::RegisterType( VObject *obj )
{
wxString type = obj->GetTypeName().Lower();
for( size_t i=0;i<m_registeredTypes.size(); i++ )
if ( m_registeredTypes[i]->GetTypeName().Lower() == type )
return;
m_registeredTypes.push_back( obj );
}
wxArrayString View3D::GetRegisteredTypes()
{
wxArrayString list;
for( size_t i=0;i<m_registeredTypes.size(); i++ )
list.Add( m_registeredTypes[i]->GetTypeName() );
return list;
}
VObject *View3D::FindRegisteredType( const wxString &type )
{
VObject *klass = 0;
for( size_t i=0;i<m_registeredTypes.size(); i++ )
if ( type.Lower() == m_registeredTypes[i]->GetTypeName().Lower() )
klass = m_registeredTypes[i];
return klass;
}
VObject *View3D::CreateObject( const wxString &type )
{
VObject *klass = FindRegisteredType( type );
if ( !klass ) return 0;
VObject *instance = klass->Duplicate();
AddObject( instance );
return instance;
}
void View3D::AddObject( VObject *obj )
{
if ( std::find(m_objects.begin(), m_objects.end(), obj ) != m_objects.end() ) return;
m_objects.push_back( obj );
obj->BuildModel( m_scene );
UpdateHandles( obj );
Render();
Refresh();
SendEvent( wxEVT_VIEW3D_UPDATE_OBJECTS );
}
void View3D::RemoveHandlesForObject( VObject *obj )
{
// clean up handles associated with this object
size_t i=0;
while( i<m_handles.size() )
{
if ( m_handles[i]->GetObject() == obj )
m_handles.erase( m_handles.begin() + i );
else
i++;
}
}
void View3D::DeleteObject( VObject *obj )
{
m_objects.erase( std::find( m_objects.begin(), m_objects.end(), obj ) );
// clear from selections list
std::vector<VObject*>::iterator it = std::find( m_selections.begin(), m_selections.end(), obj );
if ( it != m_selections.end() ) m_selections.erase( it );
m_scene.clear( obj->GetId() );
RemoveHandlesForObject( obj );
delete obj;
Render();
Refresh();
SendEvent( wxEVT_VIEW3D_UPDATE_OBJECTS );
}
void View3D::DeleteAll()
{
m_selections.clear();
for( size_t i=0;i<m_objects.size();i++ )
delete m_objects[i];
m_objects.clear();
m_scene.clear();
m_handles.clear();
m_scene.basic_axes_with_ground();
Render();
Refresh();
SendEvent( wxEVT_VIEW3D_UPDATE_OBJECTS );
}
std::vector<VObject*> View3D::GetObjects()
{
return m_objects;
}
void View3D::Select( VObject *obj )
{
m_selections.clear();
if ( obj != 0 )
m_selections.push_back( obj );
SendEvent( wxEVT_VIEW3D_UPDATE_SELECTION );
Refresh();
}
void View3D::ClearSelections()
{
m_selections.clear();
SendEvent( wxEVT_VIEW3D_UPDATE_OBJECTS );
SendEvent( wxEVT_VIEW3D_UPDATE_SELECTION );
Refresh();
}
bool View3D::IsSelected( VObject *obj )
{
return std::find( m_selections.begin(), m_selections.end(), obj ) != m_selections.end();
}
std::vector<VObject*> View3D::GetSelectedObjects()
{
return m_selections;
}
VObject *View3D::GetFirstSelectedObject()
{
if ( m_selections.size() > 0 )
return m_selections[0];
else
return 0;
}
VObject *View3D::FindObjectByName( const wxString &name )
{
for( size_t i=0;i<m_objects.size(); i++ )
if ( m_objects[i]->Property("Name").GetString().Lower() == name.Lower() )
return m_objects[i];
return 0;
}
VObject *View3D::FindObjectById( int id )
{
for( size_t i=0;i<m_objects.size(); i++ )
if ( id == m_objects[i]->GetId() ) return m_objects[i];
return 0;
}
VObject *View3D::GetObject( size_t index )
{
if ( index < m_objects.size() ) return m_objects[index];
else return 0;
}
int View3D::GetObjectIndex( VObject *o )
{
for( size_t i=0;i<m_objects.size(); i++ )
if ( m_objects[i] == o ) return (int)i;
return -1;
}
void View3D::DeleteSelected()
{
std::vector<VObject*> list = m_selections;
for( size_t i=0;i<list.size();i++)
DeleteObject(list[i]);
}
void View3D::DuplicateSelected()
{
for( size_t i=0;i<m_selections.size();i++)
AddObject( m_selections[i]->Duplicate() );
}
void View3D::ShowAll()
{
for (size_t i=0;i<m_objects.size();i++ )
m_objects[i]->Show( true );
SendEvent( wxEVT_VIEW3D_UPDATE_OBJECTS );
Refresh();
}
void View3D::ChangeMap( const wxBitmap &map, double mpp )
{
m_mpp = mpp;
m_staticMapXY = map;
m_staticMapXYScaled = wxNullBitmap;
}
void View3D::Write( wxOutputStream &ostrm )
{
wxDataOutputStream out(ostrm);
out.Write8( 0xf2 ); // start code
out.Write8( 3 ); // version
// view settings
out.Write8( (wxUint8)m_mode );
out.Write32( __N_MODES );
for( size_t i=0;i<__N_MODES;i++ )
m_lastView[i].Write( ostrm );
// objects
out.Write32( m_objects.size() );
for( size_t i=0;i<m_objects.size();i++ )
{
out.WriteString( m_objects[i]->GetTypeName() );
m_objects[i]->Write( ostrm );
}
// background map if it exists
out.WriteDouble( m_mpp );
out.Write8( m_staticMapXY.IsNull() ? 0 : 1 ); // has map or not
if ( !m_staticMapXY.IsNull() )
{
wxImage img = m_staticMapXY.ConvertToImage();
wxPNGHandler().SaveFile( &img, ostrm, false );
}
// finish code
out.Write8( 0xf2 );
}
bool View3D::Read( wxInputStream &istrm )
{
DeleteAll(); // clear the scene
wxDataInputStream in(istrm);
wxUint8 code = in.Read8();
wxUint8 ver = in.Read8();
// read back view settings
wxUint32 mode = in.Read8();
wxUint32 nmodes = in.Read32();
for( size_t i=0;i<nmodes;i++ )
{
if ( i < __N_MODES ) m_lastView[i].Read( istrm );
else ViewParams().Read( istrm ); // just read it, but don't keep the data
}
// read in objects
wxUint32 nobj = in.Read32();
for( size_t i=0;i<nobj;i++ )
{
wxString tyname = in.ReadString();
// if object type names change, update them
// here manually for upgrading
if ( VObject *klass = FindRegisteredType( tyname ) )
{
VObject *obj = klass->Duplicate();
if ( obj ) obj->Read( istrm );
m_objects.push_back( obj );
}
else
VBoxObject().Read( istrm );
}
if ( ver == 1 )
{
in.ReadDouble(); // lat
in.ReadDouble(); // lon
in.ReadString(); // addr
in.Read32(); // zoom
}
else if ( ver >= 2 )
{
m_mpp = in.ReadDouble();
}
if ( ver < 3 )
in.Read8(); // formerly showmap flag
m_staticMapXY = wxNullBitmap;
m_staticMapXYScaled = wxNullBitmap;
bool has_map = in.Read8() != 0;
if ( has_map )
{
wxImage img;
wxPNGHandler().LoadFile( &img, istrm, false );
m_staticMapXY = wxBitmap(img);
}
// switch to view mode as it was saved
if ( mode < __N_MODES )
SetMode( mode );
UpdateAllModels();
UpdateAllHandles();
Refresh();
SendEvent( wxEVT_VIEW3D_UPDATE_OBJECTS );
return in.Read8() == code;
}
const s3d::scene &View3D::GetScene()
{
return m_scene;
}
double View3D::GetShadeFraction()
{
return m_sf;
}
void View3D::SetRotation( double azimuth, double altitude )
{
if ( m_mode == SPIN_VIEW )
{
m_transform.rotate_azal( azimuth, altitude );
Render();
Refresh();
}
}
void View3D::GetRotation( double *azi, double *alt )
{
m_transform.get_azal( azi, alt );
}
void View3D::SetOffset( double xoff, double yoff, double zoff )
{
m_transform.offset( xoff, yoff, zoff );
Render();
Refresh();
}
void View3D::GetOffset( double *xoff, double *yoff, double *zoff )
{
m_transform.get_offset( xoff, yoff, zoff );
}
void View3D::SetScale( double scale )
{
m_transform.set_scale( scale );
Render();
Refresh();
}
double View3D::GetScale()
{
return m_transform.get_scale();
}
void View3D::UpdateAllHandles()
{
m_handles.clear();
if ( m_mode == TOP_VIEW || m_mode == Z_VIEW )
for( size_t i=0;i<m_objects.size(); i++ )
UpdateHandles( m_objects[i] );
}
void View3D::UpdateHandles( VObject *obj )
{
if ( m_mode == SPIN_VIEW )
{
m_handles.clear();
return;
}
RemoveHandlesForObject( obj );
obj->DeleteHandles();
if ( !obj->IsVisible() ) return;
obj->SetupHandles( m_mode == TOP_VIEW ? PLANE_XY : PLANE_XZ );
std::vector<VHandle*> list = obj->GetHandles();
for( size_t j=0;j<list.size();j++ )
m_handles.push_back( list[j] );
}
void View3D::UpdateModel( VObject *obj )
{
// erase all polygons with this object's ID
m_scene.clear( obj->GetId() );
obj->BuildModel( m_scene );
UpdateHandles( obj );
Render();
}
void View3D::UpdateAllModels()
{
m_scene.clear();
m_scene.basic_axes_with_ground();
for (size_t i=0;i<m_objects.size();i++ )
m_objects[i]->BuildModel( m_scene );
Render();
}
void View3D::RebuildBSPTree()
{
m_scene.m_bspValid = false;
Render();
}
void View3D::Render()
{
m_scene.build( m_transform );
if (m_mode == SPIN_VIEW)
m_sf = m_scene.shade(m_shade);
else
m_sf = 0.0;
}
wxColour View3D::FromRGBA( s3d::rgba &c )
{
return wxColour( c.r, c.g, c.b, c.a );
}
void View3D::Draw( wxDC &dc, const s3d::polygon3d &poly, int xoff, int yoff )
{
if ( !poly.as_line && s3d::zeroarea( poly ) )
return;
size_t n = poly.points.size();
if ( n < 2 ) return;
wxPoint *pp = new wxPoint[ n ];
for (size_t i=0;i<n;i++)
{
pp[i].x = (int) poly.points[i]._x;
pp[i].y = (int) -poly.points[i]._y;
}
if ( poly.as_line ) dc.DrawLines( n, pp, xoff, yoff );
else dc.DrawPolygon( n, pp, xoff, yoff );
delete [] pp;
}
void View3D::ScreenToWorld( int xs, int yzs, double *xw, double *yzw )
{
double xoff, yoff, zoff, scale;
m_transform.get_offset( &xoff, &yoff, &zoff );
scale = m_transform.get_scale();
double yzoff = m_mode == TOP_VIEW ? yoff : zoff;
xs = xs-m_winWidth/2;
yzs = -1*(yzs-m_winHeight/2);
*xw = xs / scale + xoff;
*yzw = yzs / scale + yzoff;
}
void View3D::WorldToScreen( double xw, double yzw, int *xs, int *yzs )
{
double xoff, yoff, zoff, scale;
m_transform.get_offset( &xoff, &yoff, &zoff );
scale = m_transform.get_scale();
double yzoff = m_mode == TOP_VIEW ? yoff : zoff;
*xs = (int)((xw - xoff)*scale) + m_winWidth/2;
*yzs = (int)(-1.0*( yzw - yzoff)*scale) + m_winHeight/2;
}
void View3D::Snap(double *x, double *y, double spacing)
{
if ( spacing < 0 ) spacing = m_snapSpacing;
*x = Snap(*x, spacing);
*y = Snap(*y, spacing);
}
double View3D::Snap( double v, double spacing )
{
int incr = (v<0) ? -1 : 1;
int multiples = (int)(v / spacing);
double dist1 = fabs(spacing*multiples - v);
double dist2 = fabs(spacing*(multiples+incr) - v);
if ( dist1 < dist2 ) return spacing*multiples;
else return spacing*(multiples+incr);
}
void View3D::DrawGrid( wxDC &dc )
{
int xs, yzs;
double minX, minYZ, maxX, maxYZ;
ScreenToWorld( 0, 0, &minX, &maxYZ );
ScreenToWorld( m_winWidth, m_winHeight, &maxX, &minYZ );
Snap( &minX, &minYZ, m_gridSpacing );
Snap( &maxX, &maxYZ, m_gridSpacing );
dc.SetPen( *wxBLACK_PEN );
for ( double X = minX; X < maxX; X += m_gridSpacing )
{
for( double Y = minYZ; Y < maxYZ; Y+= m_gridSpacing )
{
WorldToScreen( X, Y, &xs, &yzs );
dc.DrawPoint( xs, yzs );
}
}
}
template <typename T> T CLAMP(T value, T low, T high)
{
return (value < low) ? low : ((value > high) ? high : value);
}
void View3D::OnPaint( wxPaintEvent & )
{
wxAutoBufferedPaintDC pdc(this);
wxGCDC dc( pdc );
GetClientSize( &m_winWidth, &m_winHeight );
int xoff = m_winWidth/2;
int yoff = m_winHeight/2;
dc.SetBackground( wxBrush( *wxWHITE ) );
dc.Clear();
bool has_statmap = false;
if ( m_mode == TOP_VIEW && !m_staticMapXY.IsNull() )
{
has_statmap = true;
double scale = m_transform.get_scale()*m_mpp;
int scaled_width = (int)(m_staticMapXY.GetWidth() * scale);
int scaled_height = (int)(m_staticMapXY.GetHeight() * scale);
if ( m_staticMapXYScaled.GetWidth() != scaled_width
|| m_staticMapXYScaled.GetHeight() != scaled_height )
{
m_staticMapXYScaled = wxBitmap(
m_staticMapXY.ConvertToImage().Scale(
scaled_width, scaled_height ) );
}
int xp, yp;
WorldToScreen( 0, 0, &xp, &yp );
dc.DrawBitmap( m_staticMapXYScaled,
xp-scaled_width/2, yp-scaled_height/2 );
}
double vx, vy, vz, nx, ny, nz;
const std::vector<s3d::polygon3d*> &polygons = m_scene.get_rendered();
for ( size_t i=0;i<polygons.size(); i++ )
{
s3d::polygon3d &p = *polygons[i];
// skip the green background if we have a static bitmap
if ( has_statmap && p.id == -2 ) continue;
if ( VObject *obj = FindObjectById( p.id ) )
if ( !obj->IsVisible() )
continue;
// compute "shaded" color of polygon
s3d::rgba cc = p.fill;
if ( !p.as_line && p.id >= 0 )
{
// get polygon normal (cross prod of p._x,p._y,p._z [0] --> [1]
double xo, yo, zo;
m_transform.get_offset( &xo, &yo, &zo );
m_transform.get_view_normal( &vx, &vy, &vz );
s3d::point3d vn( xo+vx, yo+vy, zo+vz );
m_transform( vn );
vx = vn._x;
vy = vn._y;
vz = vn._z;
s3d::polynormaltr( p, &nx, &ny, &nz );
nx = 0-nx;
ny = 0-ny;
nz = 0-nz;
double dot = nx*vx + ny*vy + nz*vz;
double costh = dot/( sqrt(nx*nx+ny*ny+nz*nz)*sqrt(vx*vx+vy*vy+vz*vz) );
double theta = acos( costh ) * 180/3.1415926;
double factor = theta/180.0+0.5;
double R = cc.r*factor;
double G = cc.g*factor;
double B = cc.b*factor;
cc.r = (unsigned char)CLAMP<double>( R, 1, 254 );
cc.g = (unsigned char)CLAMP<double>( G, 1, 254 );
cc.b = (unsigned char)CLAMP<double>( B, 1, 254 );
cc.a = (m_mode==SPIN_VIEW||p.as_line) ? 255 : 180;
}
wxColour wcol( FromRGBA( cc ) );
dc.SetBrush( wxBrush( wcol ) );
#ifdef _DEBUG
dc.SetPen( wxPen( FromRGBA( p.border ), p.thick ) );
#else
dc.SetPen( (m_mode==SPIN_VIEW||p.as_line) ? wxPen(wcol,2) : *wxTRANSPARENT_PEN );
#endif
Draw( dc, p, xoff, yoff );
}