-
Notifications
You must be signed in to change notification settings - Fork 53
/
SpinWidget.cpp
2522 lines (2234 loc) · 93.5 KB
/
SpinWidget.cpp
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
#include "SpinWidget.hpp"
#include <QMouseEvent>
#include <QTimer>
#include <QtWidgets>
#include <VFRendering/ArrowRenderer.hxx>
#include <VFRendering/BoundingBoxRenderer.hxx>
#include <VFRendering/CombinedRenderer.hxx>
#include <VFRendering/CoordinateSystemRenderer.hxx>
#include <VFRendering/IsosurfaceRenderer.hxx>
#include <VFRendering/VectorSphereRenderer.hxx>
#include <glm/gtc/type_ptr.hpp>
#include <Spirit/Configurations.h>
#include <Spirit/Geometry.h>
#include <Spirit/Hamiltonian.h>
#include <Spirit/Simulation.h>
#include <Spirit/System.h>
#include <algorithm>
#include <sstream>
SpinWidget::SpinWidget( std::shared_ptr<State> state, QWidget * parent )
: QOpenGLWidget( parent ), m_vf( {}, {} ), m_vf_surf2D( {}, {} )
{
this->state = state;
this->m_gl_initialized = false;
this->m_suspended = false;
this->paste_atom_type = 0;
// QT Widget Settings
setFocusPolicy( Qt::StrongFocus );
QSizePolicy sizePolicy( QSizePolicy::Expanding, QSizePolicy::Expanding );
sizePolicy.setHorizontalStretch( 0 );
sizePolicy.setVerticalStretch( 0 );
this->setSizePolicy( sizePolicy );
this->setMinimumSize( 200, 200 );
this->setBaseSize( 600, 600 );
// Default VFRendering Settings
setColormapGeneral( Colormap::HSV );
setColormapArrows( Colormap::HSV );
setColormapRotationInverted( 0, false, false, glm::vec3{ 1, 0, 0 }, glm::vec3{ 0, 1, 0 }, glm::vec3{ 0, 0, 1 } );
this->m_view.setOption<VFRendering::ArrowRenderer::Option::CONE_RADIUS>( 0.125f );
this->m_view.setOption<VFRendering::ArrowRenderer::Option::CONE_HEIGHT>( 0.3f );
this->m_view.setOption<VFRendering::ArrowRenderer::Option::CYLINDER_RADIUS>( 0.0625f );
this->m_view.setOption<VFRendering::ArrowRenderer::Option::CYLINDER_HEIGHT>( 0.35f );
setOverallDirectionRange( { -1, 1 }, { -1, 1 }, { -1, 1 } );
float b_min[3], b_max[3];
Geometry_Get_Bounds( state.get(), b_min, b_max );
glm::vec3 bounds_min = glm::make_vec3( b_min );
glm::vec3 bounds_max = glm::make_vec3( b_max );
glm::vec2 x_range{ bounds_min[0], bounds_max[0] };
glm::vec2 y_range{ bounds_min[1], bounds_max[1] };
glm::vec2 z_range{ bounds_min[2], bounds_max[2] };
setOverallPositionRange( x_range, y_range, z_range );
this->m_surface_x_range = x_range;
this->m_surface_y_range = y_range;
this->m_surface_z_range = z_range;
int n_cell[3];
Geometry_Get_N_Cells( state.get(), n_cell );
this->m_cell_a_min = 0;
this->m_cell_a_max = n_cell[0] - 1;
this->m_cell_b_min = 0;
this->m_cell_b_max = n_cell[1] - 1;
this->m_cell_c_min = 0;
this->m_cell_c_max = n_cell[2] - 1;
this->m_source = 0;
this->visMode = VisualizationMode::SYSTEM;
this->m_location_coordinatesystem = WidgetLocation::BOTTOM_RIGHT;
this->m_location_miniview = WidgetLocation::BOTTOM_LEFT;
this->show_arrows = true;
this->show_boundingbox = true;
this->show_isosurface = false;
idx_cycle = 0;
slab_displacements = glm::vec3{ 0, 0, 0 };
this->n_cell_step = 1;
this->show_surface = false;
this->show_miniview = true;
this->show_coordinatesystem = true;
// Initial camera position
this->_reset_camera = false;
this->m_camera_rotate_free = false;
this->m_camera_projection_perspective = true;
// Initial drag mode settings
drag_radius = 80;
this->mouse_decoration = new MouseDecoratorWidget( drag_radius );
this->mouse_decoration->setMinimumSize( 2 * drag_radius, 2 * drag_radius );
this->mouse_decoration->setMaximumSize( 2 * drag_radius, 2 * drag_radius );
this->mouse_decoration->setParent( this );
this->m_interactionmode = InteractionMode::REGULAR;
this->m_timer_drag = new QTimer( this );
this->m_timer_drag_decoration = new QTimer( this );
this->m_dragging = false;
// Setup Arrays
this->updateData();
// Read persistent settings
this->readSettings();
this->show_arrows = this->user_show_arrows;
this->show_surface = this->user_show_surface;
this->show_isosurface = this->user_show_isosurface;
this->show_boundingbox = this->user_show_boundingbox;
}
void SpinWidget::setSuspended( bool suspended )
{
this->m_suspended = suspended;
if( !suspended )
QTimer::singleShot( 1, this, SLOT( update() ) );
}
const VFRendering::View * SpinWidget::view()
{
return &( this->m_view );
}
const VFRendering::VectorField * SpinWidget::vectorfield()
{
return &( this->m_vf );
}
void SpinWidget::addIsosurface( std::shared_ptr<VFRendering::IsosurfaceRenderer> renderer )
{
this->m_renderers_isosurface.insert( renderer );
if( m_gl_initialized )
this->enableSystem( this->show_arrows, this->show_boundingbox, this->show_surface, this->show_isosurface );
}
void SpinWidget::removeIsosurface( std::shared_ptr<VFRendering::IsosurfaceRenderer> renderer )
{
this->m_renderers_isosurface.erase( renderer );
if( m_gl_initialized )
this->enableSystem( this->show_arrows, this->show_boundingbox, this->show_surface, this->show_isosurface );
}
// Return the relative mouse position [-1,1]
glm::vec2 relative_coords_from_mouse( glm::vec2 mouse_pos, glm::vec2 winsize )
{
glm::vec2 relative = 2.0f * ( mouse_pos - 0.5f * winsize );
relative.x /= winsize.x;
relative.y /= winsize.y;
return relative;
}
glm::vec2 SpinWidget::system_coords_from_mouse( glm::vec2 mouse_pos, glm::vec2 winsize )
{
auto relative = relative_coords_from_mouse( mouse_pos, winsize );
glm::vec4 proj_back{ relative.x, relative.y, 0, 0 };
auto matrices = VFRendering::Utilities::getMatrices( options(), winsize.x / winsize.y );
auto model_view = glm::inverse( matrices.first );
auto projection = glm::inverse( matrices.second );
proj_back = proj_back * projection;
proj_back = proj_back * model_view;
auto camera_position = options().get<VFRendering::View::Option::CAMERA_POSITION>();
return glm::vec2{ proj_back.x + camera_position.x, -proj_back.y + camera_position.y };
}
float SpinWidget::system_radius_from_relative( float radius, glm::vec2 winsize )
{
auto r1 = system_coords_from_mouse( { 0.0f, 0.0f }, winsize );
auto r2 = system_coords_from_mouse( { radius - 5, 0.0f }, winsize );
return r2.x - r1.x;
}
void SpinWidget::dragpaste()
{
QPoint localCursorPos = this->mapFromGlobal( cursor().pos() );
QSize widgetSize = this->size();
glm::vec2 mouse_pos{ localCursorPos.x(), localCursorPos.y() };
glm::vec2 size{ widgetSize.width(), widgetSize.height() };
glm::vec2 coords = system_coords_from_mouse( mouse_pos, size );
float radius = system_radius_from_relative( this->drag_radius, size );
float rect[3]{ -1, -1, -1 };
float current_position[3]{ coords.x, coords.y, 0.0f };
float shift[3]{ last_drag_coords.x - coords.x, last_drag_coords.y - coords.y, 0.0f };
Configuration_From_Clipboard_Shift( state.get(), shift, current_position, rect, radius );
}
void SpinWidget::defectpaste()
{
QPoint localCursorPos = this->mapFromGlobal( cursor().pos() );
QSize widgetSize = this->size();
glm::vec2 mouse_pos{ localCursorPos.x(), localCursorPos.y() };
glm::vec2 size{ widgetSize.width(), widgetSize.height() };
glm::vec2 coords = system_coords_from_mouse( mouse_pos, size );
float radius = system_radius_from_relative( this->drag_radius, size );
float rect[3]{ -1, -1, -1 };
float current_position[3]{ coords.x, coords.y, 0.0f };
float center[3];
Geometry_Get_Center( this->state.get(), center );
current_position[0] -= center[0];
current_position[1] -= center[1];
Configuration_Set_Atom_Type( state.get(), this->paste_atom_type, current_position, rect, radius );
}
void SpinWidget::pinningpaste()
{
QPoint localCursorPos = this->mapFromGlobal( cursor().pos() );
QSize widgetSize = this->size();
glm::vec2 mouse_pos{ localCursorPos.x(), localCursorPos.y() };
glm::vec2 size{ widgetSize.width(), widgetSize.height() };
glm::vec2 coords = system_coords_from_mouse( mouse_pos, size );
float radius = system_radius_from_relative( this->drag_radius, size );
float rect[3]{ -1, -1, -1 };
float current_position[3]{ coords.x, coords.y, 0.0f };
float center[3];
Geometry_Get_Center( this->state.get(), center );
current_position[0] -= center[0];
current_position[1] -= center[1];
Configuration_Set_Pinned( state.get(), this->m_dragging, current_position, rect, radius );
}
void SpinWidget::setPasteAtomType( int type )
{
this->paste_atom_type = type;
}
void SpinWidget::initializeGL()
{
if( m_interactionmode == InteractionMode::DRAG )
{
this->setCursor( Qt::BlankCursor );
}
else
{
mouse_decoration->hide();
}
// Initialize VectorField data
this->updateVectorFieldGeometry();
this->updateVectorFieldDirections();
// Get GL context
makeCurrent();
// Initialize the visualisation options
float b_min[3], b_max[3];
Geometry_Get_Bounds( state.get(), b_min, b_max );
glm::vec3 bounds_min = glm::make_vec3( b_min );
glm::vec3 bounds_max = glm::make_vec3( b_max );
glm::vec2 x_range{ bounds_min[0], bounds_max[0] };
glm::vec2 y_range{ bounds_min[1], bounds_max[1] };
glm::vec2 z_range{ bounds_min[2], bounds_max[2] };
glm::vec3 bounding_box_center = { ( bounds_min[0] + bounds_max[0] ) / 2, ( bounds_min[1] + bounds_max[1] ) / 2,
( bounds_min[2] + bounds_max[2] ) / 2 };
glm::vec3 bounding_box_side_lengths
= { bounds_max[0] - bounds_min[0], bounds_max[1] - bounds_min[1], bounds_max[2] - bounds_min[2] };
// Create renderers
// System
this->m_renderer_arrows = std::make_shared<VFRendering::ArrowRenderer>( m_view, m_vf );
float indi_length = glm::length( bounds_max - bounds_min ) * 0.05;
int indi_dashes = 5;
float indi_dashes_per_length = (float)indi_dashes / indi_length;
bool periodical[3];
Hamiltonian_Get_Boundary_Conditions( this->state.get(), periodical );
glm::vec3 indis{ indi_length * periodical[0], indi_length * periodical[1], indi_length * periodical[2] };
this->m_renderer_boundingbox
= std::make_shared<VFRendering::BoundingBoxRenderer>( VFRendering::BoundingBoxRenderer::forCuboid(
m_view, bounding_box_center, bounding_box_side_lengths, indis, indi_dashes_per_length ) );
std::vector<std::shared_ptr<VFRendering::RendererBase>> renderers = { m_renderer_arrows, m_renderer_boundingbox };
if( Geometry_Get_Dimensionality( this->state.get() ) == 2 )
{
// 2D Surface options
// No options yet...
this->m_renderer_surface_2D = std::make_shared<VFRendering::SurfaceRenderer>( m_view, m_vf_surf2D );
this->m_renderer_surface = m_renderer_surface_2D;
}
else if( Geometry_Get_Dimensionality( this->state.get() ) == 3 )
{
// 3D Surface options
this->m_renderer_surface_3D = std::make_shared<VFRendering::IsosurfaceRenderer>( m_view, m_vf );
this->m_renderer_surface_3D->setOption<VFRendering::IsosurfaceRenderer::Option::ISOVALUE>( 0.0 );
auto mini_diff = glm::vec2{ 0.00001f, -0.00001f };
setSurface( x_range + mini_diff, y_range + mini_diff, z_range + mini_diff );
this->m_renderer_surface = m_renderer_surface_3D;
}
this->m_system = std::make_shared<VFRendering::CombinedRenderer>( m_view, renderers );
// Sphere
this->m_sphere = std::make_shared<VFRendering::VectorSphereRenderer>( m_view, m_vf );
// Coordinate cross
this->m_coordinatesystem = std::make_shared<VFRendering::CoordinateSystemRenderer>( m_view );
this->m_coordinatesystem->setOption<VFRendering::CoordinateSystemRenderer::Option::NORMALIZE>( true );
// Setup the View
this->setVisualizationMode( this->visMode );
// Configure System (Setup the renderers)
this->setSystemCycle( SystemMode( this->idx_cycle ) );
this->enableSystem( this->show_arrows, this->show_boundingbox, this->show_surface, this->show_isosurface );
// Set renderers' colormaps
this->setColormapArrows( this->colormap_arrows() );
this->m_gl_initialized = true;
}
void SpinWidget::teardownGL()
{
// GLSpins::terminate();
}
void SpinWidget::resizeGL( int width, int height )
{
this->m_view.setFramebufferSize( width * devicePixelRatio(), height * devicePixelRatio() );
QTimer::singleShot( 1, this, SLOT( update() ) );
}
void SpinWidget::screenShot( std::string filename )
{
auto pixmap = this->grab();
pixmap.save( ( filename + ".png" ).c_str() );
}
void SpinWidget::updateVectorFieldGeometry()
{
int nos = System_Get_NOS( state.get() );
int n_cells[3];
Geometry_Get_N_Cells( this->state.get(), n_cells );
int n_cell_atoms = Geometry_Get_N_Cell_Atoms( this->state.get() );
int n_cells_draw[3] = { std::max( 1, int( ceil( ( m_cell_a_max - m_cell_a_min + 1.0 ) / n_cell_step ) ) ),
std::max( 1, int( ceil( ( m_cell_b_max - m_cell_b_min + 1.0 ) / n_cell_step ) ) ),
std::max( 1, int( ceil( ( m_cell_c_max - m_cell_c_min + 1.0 ) / n_cell_step ) ) ) };
int nos_draw = n_cell_atoms * n_cells_draw[0] * n_cells_draw[1] * n_cells_draw[2];
// Positions of the vectorfield
std::vector<glm::vec3> positions = std::vector<glm::vec3>( nos_draw );
// ToDo: Update the pointer to our Data instead of copying Data?
// Positions
// get pointer
scalar * spin_pos;
int * atom_types;
spin_pos = Geometry_Get_Positions( state.get() );
atom_types = Geometry_Get_Atom_Types( state.get() );
int icell = 0;
for( int cell_c = m_cell_c_min; cell_c < m_cell_c_max + 1; cell_c += n_cell_step )
{
for( int cell_b = m_cell_b_min; cell_b < m_cell_b_max + 1; cell_b += n_cell_step )
{
for( int cell_a = m_cell_a_min; cell_a < m_cell_a_max + 1; cell_a += n_cell_step )
{
for( int ibasis = 0; ibasis < n_cell_atoms; ++ibasis )
{
int idx = ibasis + n_cell_atoms * ( cell_a + n_cells[0] * ( cell_b + n_cells[1] * cell_c ) );
positions[icell] = glm::vec3( spin_pos[3 * idx], spin_pos[1 + 3 * idx], spin_pos[2 + 3 * idx] );
++icell;
}
}
}
}
// Generate the right geometry (triangles and tetrahedra)
VFRendering::Geometry geometry;
VFRendering::Geometry geometry_surf2D;
// get tetrahedra
if( Geometry_Get_Dimensionality( state.get() ) == 3 )
{
if( ( n_cells_draw[0] <= 1 || n_cells_draw[1] <= 1 || n_cells_draw[2] <= 1 ) )
{
geometry = VFRendering::Geometry( positions, {}, {}, true );
}
else
{
int temp_ranges[6]
= { m_cell_a_min, m_cell_a_max + 1, m_cell_b_min, m_cell_b_max + 1, m_cell_c_min, m_cell_c_max + 1 };
const std::array<VFRendering::Geometry::index_type, 4> * tetrahedra_indices_ptr = nullptr;
int num_tetrahedra = Geometry_Get_Tetrahedra_Ranged(
state.get(), reinterpret_cast<const int **>( &tetrahedra_indices_ptr ), n_cell_step, temp_ranges );
std::vector<std::array<VFRendering::Geometry::index_type, 4>> tetrahedra_indices(
tetrahedra_indices_ptr, tetrahedra_indices_ptr + num_tetrahedra );
geometry = VFRendering::Geometry( positions, {}, tetrahedra_indices, false );
}
}
else if( Geometry_Get_Dimensionality( state.get() ) == 2 )
{
// Determine two basis vectors
std::array<glm::vec3, 2> basis;
float eps = 1e-6;
for( int i = 1, j = 0; i < nos_draw && j < 2; ++i )
{
if( glm::length( positions[i] - positions[0] ) > eps )
{
if( j < 1 )
{
basis[j] = glm::normalize( positions[i] - positions[0] );
++j;
}
else
{
if( 1 - std::abs( glm::dot( basis[0], glm::normalize( positions[i] - positions[0] ) ) ) > eps )
{
basis[j] = glm::normalize( positions[i] - positions[0] );
++j;
}
}
}
}
int n_cells[3];
Geometry_Get_N_Cells( this->state.get(), n_cells );
float bounds_min[3], bounds_max[3];
Geometry_Get_Bounds( state.get(), bounds_min, bounds_max );
float density = 0.01f;
if( n_cells[0] > 1 )
density = std::max( density, n_cells[0] / ( bounds_max[0] - bounds_min[0] ) );
if( n_cells[1] > 1 )
density = std::max( density, n_cells[1] / ( bounds_max[1] - bounds_min[1] ) );
if( n_cells[2] > 1 )
density = std::max( density, n_cells[2] / ( bounds_max[2] - bounds_min[2] ) );
density /= n_cell_step;
glm::vec3 normal = this->arrowSize() / density * glm::normalize( glm::cross( basis[0], basis[1] ) );
// By default, +z is up, which is where we want the normal oriented towards
if( glm::dot( normal, glm::vec3{ 0, 0, 1 } ) < 1e-6 )
normal = -normal;
// Rectilinear with one basis atom
if( n_cell_atoms == 1 && std::abs( glm::dot( basis[0], basis[1] ) ) < 1e-6 && glm::length( basis[0] ) > 1e-6
&& glm::length( basis[1] )
> 1e-6 ) // Check for length of the basis so that we do not get 1D geometries here
{
std::vector<float> xs( n_cells_draw[0] ), ys( n_cells_draw[1] ), zs( n_cells_draw[2] );
for( int i = 0; i < n_cells_draw[0]; ++i )
xs[i] = positions[i].x;
for( int i = 0; i < n_cells_draw[1]; ++i )
ys[i] = positions[i * n_cells_draw[0]].y;
for( int i = 0; i < n_cells_draw[2]; ++i )
zs[i] = positions[i * n_cells_draw[0] * n_cells_draw[1]].z;
geometry = VFRendering::Geometry::rectilinearGeometry( xs, ys, zs );
for( int i = 0; i < n_cells_draw[0]; ++i )
xs[i] = ( positions[i] - normal ).x;
for( int i = 0; i < n_cells_draw[1]; ++i )
ys[i] = ( positions[i * n_cells_draw[0]] - normal ).y;
for( int i = 0; i < n_cells_draw[2]; ++i )
zs[i] = ( positions[i * n_cells_draw[0] * n_cells_draw[1]] - normal ).z;
geometry_surf2D = VFRendering::Geometry::rectilinearGeometry( xs, ys, zs );
}
// All others
else
{
const std::array<VFRendering::Geometry::index_type, 3> * triangle_indices_ptr = nullptr;
// int num_triangles = Geometry_Get_Triangulation(state.get(), reinterpret_cast<const int
// **>(&triangle_indices_ptr), n_cell_step);
int temp_ranges[6]
= { m_cell_a_min, m_cell_a_max + 1, m_cell_b_min, m_cell_b_max + 1, m_cell_c_min, m_cell_c_max + 1 };
int num_triangles = Geometry_Get_Triangulation_Ranged(
state.get(), reinterpret_cast<const int **>( &triangle_indices_ptr ), n_cell_step, temp_ranges, -1,
-1 );
// If the geometry cannot be triangulated, it may e.g be one dimensional due to filters etc. we push a dummy
// triangle, otherwise we get a lot of VFRendering error messages
if( num_triangles < 1 && this->show_surface )
{
num_triangles = 1;
triangle_indices_ptr = new const std::array<VFRendering::Geometry::index_type, 3>();
}
std::vector<std::array<VFRendering::Geometry::index_type, 3>> triangle_indices(
triangle_indices_ptr, triangle_indices_ptr + num_triangles );
geometry = VFRendering::Geometry( positions, triangle_indices, {}, true );
for( int i = 0; i < nos_draw; ++i )
positions[i] = positions[i] - normal;
geometry_surf2D = VFRendering::Geometry( positions, triangle_indices, {}, true );
}
// Update the vectorfield geometry
this->m_vf_surf2D.updateGeometry( geometry_surf2D );
}
else
{
geometry = VFRendering::Geometry( positions, {}, {}, true );
}
// Update the vectorfield
this->m_vf.updateGeometry( geometry );
}
void SpinWidget::updateVectorFieldDirections()
{
int nos = System_Get_NOS( state.get() );
int n_cells[3];
Geometry_Get_N_Cells( this->state.get(), n_cells );
int n_cell_atoms = Geometry_Get_N_Cell_Atoms( this->state.get() );
int n_cells_draw[3] = { std::max( 1, int( ceil( ( m_cell_a_max - m_cell_a_min + 1.0 ) / n_cell_step ) ) ),
std::max( 1, int( ceil( ( m_cell_b_max - m_cell_b_min + 1.0 ) / n_cell_step ) ) ),
std::max( 1, int( ceil( ( m_cell_c_max - m_cell_c_min + 1.0 ) / n_cell_step ) ) ) };
int nos_draw = n_cell_atoms * n_cells_draw[0] * n_cells_draw[1] * n_cells_draw[2];
// Directions of the vectorfield
std::vector<glm::vec3> directions = std::vector<glm::vec3>( nos_draw );
// ToDo: Update the pointer to our Data instead of copying Data?
// Directions
// get pointer
scalar * spins;
int * atom_types;
atom_types = Geometry_Get_Atom_Types( state.get() );
if( this->m_source == 0 )
spins = System_Get_Spin_Directions( state.get() );
else if( this->m_source == 1 )
spins = System_Get_Effective_Field( state.get() );
else
spins = System_Get_Spin_Directions( state.get() );
// copy
/*positions.assign(spin_pos, spin_pos + 3*nos);
directions.assign(spins, spins + 3*nos);*/
int icell = 0;
for( int cell_c = m_cell_c_min; cell_c < m_cell_c_max + 1; cell_c += n_cell_step )
{
for( int cell_b = m_cell_b_min; cell_b < m_cell_b_max + 1; cell_b += n_cell_step )
{
for( int cell_a = m_cell_a_min; cell_a < m_cell_a_max + 1; cell_a += n_cell_step )
{
for( int ibasis = 0; ibasis < n_cell_atoms; ++ibasis )
{
int idx = ibasis + n_cell_atoms * ( cell_a + n_cells[0] * ( cell_b + n_cells[1] * cell_c ) );
directions[icell] = glm::vec3( spins[3 * idx], spins[1 + 3 * idx], spins[2 + 3 * idx] );
if( atom_types[idx] < 0 )
directions[icell] *= 0;
++icell;
}
}
}
}
// rescale if effective field
if( this->m_source == 1 )
{
float max_length = 0;
for( auto direction : directions )
{
max_length = std::max( max_length, glm::length( direction ) );
}
if( max_length > 0 )
{
for( auto & direction : directions )
{
direction /= max_length;
}
}
}
// Update the vectorfield
this->m_vf.updateVectors( directions );
if( Geometry_Get_Dimensionality( state.get() ) == 2 )
this->m_vf_surf2D.updateVectors( directions );
}
void SpinWidget::updateData( bool update_directions, bool update_geometry, bool update_camera )
{
// Update the VectorField
if( update_directions )
this->updateVectorFieldDirections();
if( update_geometry )
this->updateVectorFieldGeometry();
// Update the View
if( update_camera )
{
float b_min[3], b_max[3];
Geometry_Get_Bounds( state.get(), b_min, b_max );
glm::vec3 bounds_min = glm::make_vec3( b_min );
glm::vec3 bounds_max = glm::make_vec3( b_max );
glm::vec3 center = ( bounds_min + bounds_max ) * 0.5f;
this->m_view.setOption<VFRendering::View::Option::SYSTEM_CENTER>( center );
if( this->_reset_camera )
{
setCameraToDefault();
this->_reset_camera = false;
}
}
// Update Widget
QTimer::singleShot( 1, this, SLOT( update() ) );
}
void SpinWidget::paintGL()
{
if( this->m_suspended )
return;
if( Simulation_Running_On_Image( this->state.get() ) || Simulation_Running_On_Chain( this->state.get() )
|| this->m_dragging )
{
this->updateData( true, false, true );
}
this->m_view.draw();
}
void SpinWidget::setVisualisationSource( int source )
{
this->m_source = source;
}
void SpinWidget::mousePressEvent( QMouseEvent * event )
{
if( this->m_suspended )
return;
m_previous_mouse_position = event->pos();
if( m_interactionmode == InteractionMode::DRAG || m_interactionmode == InteractionMode::DEFECT
|| m_interactionmode == InteractionMode::PIN )
{
if( event->button() == Qt::LeftButton )
{
QPoint localCursorPos = this->mapFromGlobal( cursor().pos() );
QSize widgetSize = this->size();
glm::vec2 mouse_pos{ localCursorPos.x(), localCursorPos.y() };
glm::vec2 size{ widgetSize.width(), widgetSize.height() };
last_drag_coords = system_coords_from_mouse( mouse_pos, size );
m_timer_drag->stop();
// Copy spin configuration
Configuration_To_Clipboard( state.get() );
// Set up Update Timers
if( m_interactionmode == InteractionMode::DRAG )
connect( m_timer_drag, &QTimer::timeout, this, &SpinWidget::dragpaste );
else if( m_interactionmode == InteractionMode::DEFECT )
connect( m_timer_drag, &QTimer::timeout, this, &SpinWidget::defectpaste );
else if( m_interactionmode == InteractionMode::PIN )
connect( m_timer_drag, &QTimer::timeout, this, &SpinWidget::pinningpaste );
float ips = Simulation_Get_IterationsPerSecond( state.get() );
if( ips > 1000 )
{
m_timer_drag->start( 1 );
}
else if( ips > 0 )
{
m_timer_drag->start( (int)( 1000 / ips ) );
}
m_dragging = true;
}
}
}
void SpinWidget::mouseReleaseEvent( QMouseEvent * event )
{
if( this->m_suspended )
return;
if( m_interactionmode == InteractionMode::DRAG || m_interactionmode == InteractionMode::DEFECT
|| m_interactionmode == InteractionMode::PIN )
{
if( event->button() == Qt::LeftButton )
{
m_timer_drag->stop();
m_dragging = false;
}
else if( event->button() == Qt::RightButton )
{
if( m_interactionmode == InteractionMode::DRAG )
dragpaste();
else if( m_interactionmode == InteractionMode::DEFECT )
defectpaste();
else if( m_interactionmode == InteractionMode::PIN )
pinningpaste();
this->updateData();
}
}
}
void SpinWidget::mouseMoveEvent( QMouseEvent * event )
{
if( this->m_suspended )
return;
if( m_interactionmode == InteractionMode::DRAG )
{
dragpaste();
QTimer::singleShot( 1, this, SLOT( update() ) );
}
else if( m_interactionmode == InteractionMode::DEFECT )
{
defectpaste();
QTimer::singleShot( 1, this, SLOT( update() ) );
}
else if( m_interactionmode == InteractionMode::PIN )
{
pinningpaste();
QTimer::singleShot( 1, this, SLOT( update() ) );
}
else if( event->buttons() & Qt::LeftButton || event->buttons() & Qt::RightButton )
{
float scale = 1;
if( event->modifiers() & Qt::ShiftModifier )
scale = 0.1f;
glm::vec2 current_mouse_position
= glm::vec2( event->pos().x(), event->pos().y() ) * (float)devicePixelRatio() * scale;
glm::vec2 previous_mouse_position = glm::vec2( m_previous_mouse_position.x(), m_previous_mouse_position.y() )
* (float)devicePixelRatio() * scale;
m_previous_mouse_position = event->pos();
VFRendering::CameraMovementModes movement_mode = VFRendering::CameraMovementModes::ROTATE_BOUNDED;
if( this->m_camera_rotate_free )
movement_mode = VFRendering::CameraMovementModes::ROTATE_FREE;
if( ( event->modifiers() & Qt::AltModifier ) == Qt::AltModifier || event->buttons() & Qt::RightButton )
{
movement_mode = VFRendering::CameraMovementModes::TRANSLATE;
}
this->m_view.mouseMove( previous_mouse_position, current_mouse_position, movement_mode );
QTimer::singleShot( 1, this, SLOT( update() ) );
}
}
void SpinWidget::wheelEvent( QWheelEvent * event )
{
float scale = 1;
if( event->modifiers() & Qt::ShiftModifier )
{
scale = 0.1f;
}
if( event->modifiers() & Qt::ControlModifier )
{
float wheel_delta = scale * event->angleDelta().y() / 10.0f;
drag_radius = std::max( 1.0f, std::min( 500.0f, drag_radius + wheel_delta ) );
this->mouse_decoration->setRadius( drag_radius );
this->mouse_decoration->setMinimumSize( 2 * drag_radius, 2 * drag_radius );
this->mouse_decoration->setMaximumSize( 2 * drag_radius, 2 * drag_radius );
}
else
{
float wheel_delta = event->angleDelta().y();
this->m_view.mouseScroll( -wheel_delta * 0.1 * scale );
QTimer::singleShot( 1, this, SLOT( update() ) );
}
}
void SpinWidget::updateMouseDecoration()
{
auto pos = this->mapFromGlobal( QCursor::pos() - QPoint( drag_radius, drag_radius ) );
this->mouse_decoration->move( (int)pos.x(), (int)pos.y() );
}
float SpinWidget::getFramesPerSecond() const
{
return this->m_view.getFramerate();
}
const VFRendering::Options & SpinWidget::options() const
{
return this->m_view.options();
}
void SpinWidget::moveCamera( float backforth, float rightleft, float updown )
{
if( this->m_suspended )
return;
auto movement_mode = VFRendering::CameraMovementModes::TRANSLATE;
this->m_view.mouseMove( { 0, 0 }, { rightleft, updown }, movement_mode );
this->m_view.mouseScroll( backforth * 0.1 );
QTimer::singleShot( 1, this, SLOT( update() ) );
}
void SpinWidget::rotateCamera( float theta, float phi )
{
if( this->m_suspended )
return;
if( this->m_interactionmode == InteractionMode::DRAG )
{
theta = 0;
}
VFRendering::CameraMovementModes movement_mode = VFRendering::CameraMovementModes::ROTATE_BOUNDED;
if( this->m_camera_rotate_free )
movement_mode = VFRendering::CameraMovementModes::ROTATE_FREE;
this->m_view.mouseMove( { 0, 0 }, { phi, theta }, movement_mode );
QTimer::singleShot( 1, this, SLOT( update() ) );
}
//////////////////////////////////////////////////////////////////////////////////////
int SpinWidget::visualisationNCellSteps()
{
return this->n_cell_step;
}
void SpinWidget::setVisualisationNCellSteps( int n_cell_steps )
{
float size_before = this->arrowSize();
this->n_cell_step = n_cell_steps;
this->setArrows( size_before, this->arrowLOD() );
this->updateData();
}
///// --- Mode ---
void SpinWidget::setVisualizationMode( SpinWidget::VisualizationMode visualization_mode )
{
if( visualization_mode == SpinWidget::VisualizationMode::SYSTEM )
{
this->visMode = VisualizationMode::SYSTEM;
this->m_mainview = this->m_system;
this->m_miniview = this->m_sphere;
}
else if( visualization_mode == SpinWidget::VisualizationMode::SPHERE )
{
this->visMode = VisualizationMode::SPHERE;
this->m_mainview = this->m_sphere;
this->m_miniview = this->m_system;
}
this->setupRenderers();
}
SpinWidget::VisualizationMode SpinWidget::visualizationMode()
{
return this->visMode;
}
void SpinWidget::setInteractionMode( InteractionMode mode )
{
if( mode == InteractionMode::DRAG || mode == InteractionMode::DEFECT || mode == InteractionMode::PIN )
{
// Save latest regular mode settings
this->regular_mode_perspective = this->cameraProjection();
this->regular_mode_cam_pos = this->getCameraPositon();
this->regular_mode_cam_focus = this->getCameraFocus();
this->regular_mode_cam_up = this->getCameraUpVector();
// Set cursor
this->setCursor( Qt::BlankCursor );
if( mode == InteractionMode::DRAG )
this->mouse_decoration->setColors( Qt::white, Qt::black );
else if( mode == InteractionMode::DEFECT )
this->mouse_decoration->setColors( Qt::white, Qt::darkRed );
else if( mode == InteractionMode::PIN )
this->mouse_decoration->setColors( Qt::white, Qt::darkBlue );
this->mouse_decoration->show();
// Apply camera changes
this->setCameraToZ();
this->setCameraProjection( false );
// Set mode after changes so that changes are not blocked
this->m_interactionmode = mode;
// Set up update timers
m_timer_drag_decoration->stop();
connect( m_timer_drag_decoration, &QTimer::timeout, this, &SpinWidget::updateMouseDecoration );
m_timer_drag_decoration->start( 5 );
}
else
{
// Stop update timers
m_timer_drag_decoration->stop();
// Unset cursor
this->unsetCursor();
this->mouse_decoration->hide();
// Set mode before changes so that changes are not blocked
this->m_interactionmode = mode;
// Apply camera changes
this->setCameraProjection( this->regular_mode_perspective );
this->setCameraPosition( this->regular_mode_cam_pos );
this->setCameraFocus( this->regular_mode_cam_focus );
this->setCameraUpVector( this->regular_mode_cam_up );
}
QTimer::singleShot( 1, this, SLOT( update() ) );
}
SpinWidget::InteractionMode SpinWidget::interactionMode()
{
return this->m_interactionmode;
}
//////////////////////////////////////////////////////////////////////////////////////
///// --- MiniView ---
void SpinWidget::setVisualizationMiniview( bool show, SpinWidget::WidgetLocation location )
{
enableMiniview( show );
setMiniviewPosition( location );
}
bool SpinWidget::isMiniviewEnabled() const
{
return this->show_miniview;
}
void SpinWidget::enableMiniview( bool enabled )
{
this->show_miniview = enabled;
setupRenderers();
}
SpinWidget::WidgetLocation SpinWidget::miniviewPosition() const
{
return this->m_location_miniview;
}
void SpinWidget::setMiniviewPosition( SpinWidget::WidgetLocation location )
{
this->m_location_miniview = location;
this->setupRenderers();
}
//////////////////////////////////////////////////////////////////////////////////////
///// --- Coordinate System ---
void SpinWidget::setVisualizationCoordinatesystem( bool show, SpinWidget::WidgetLocation location )
{
enableCoordinateSystem( show );
setCoordinateSystemPosition( location );
}
bool SpinWidget::isCoordinateSystemEnabled() const
{
return this->show_coordinatesystem;
}
void SpinWidget::enableCoordinateSystem( bool enabled )
{
this->show_coordinatesystem = enabled;
setupRenderers();
}
SpinWidget::WidgetLocation SpinWidget::coordinateSystemPosition() const
{
return this->m_location_coordinatesystem;
}
void SpinWidget::setCoordinateSystemPosition( SpinWidget::WidgetLocation location )
{
this->m_location_coordinatesystem = location;
this->setupRenderers();
}
//////////////////////////////////////////////////////////////////////////////////////
///// --- System ---
void SpinWidget::setSlabRanges()
{
float f_center[3], bounds_min[3], bounds_max[3];
Geometry_Get_Bounds( state.get(), bounds_min, bounds_max );
Geometry_Get_Center( state.get(), f_center );
glm::vec2 x_range( bounds_min[0], bounds_max[0] );
glm::vec2 y_range( bounds_min[1], bounds_max[1] );
glm::vec2 z_range( bounds_min[2], bounds_max[2] );
glm::vec3 center( f_center[0], f_center[1], f_center[2] );
center += this->slab_displacements;
float delta = 0.51f;
switch( this->idx_cycle )
{
case 2:
{
if( (int)center.x == center.x )
{
center.x += 0.5;
}
x_range = { center[0] - delta, center[0] + delta };
break;