-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathscript.cpp
More file actions
1197 lines (991 loc) · 37.6 KB
/
script.cpp
File metadata and controls
1197 lines (991 loc) · 37.6 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 2018 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 "SolTrace". Except to comply with the
* foregoing, the term "SolTrace", 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 <lk/env.h>
#include "soltrace.h"
#include "project.h"
#include "trace.h"
#include "sunshape.h"
#include "geometry.h"
#include "optics.h"
//#include "fluxmapform.h"
#include "script.h"
static void get_3( double v[3], lk::vardata_t &arg )
{
for (int i=0;i<3;i++)
v[i] = arg.index(i)->as_number();
}
static void set_3( lk::vardata_t &arg, double v[3] )
{
arg.empty_vector();
arg.vec_append( v[0] );
arg.vec_append( v[1] );
arg.vec_append( v[2] );
}
static void get_3x3( double m[3][3], lk::vardata_t &arg )
{
for (int i=0;i<3;i++)
{
lk::vardata_t *v = arg.index(i);
for (int j=0;j<3;j++)
m[i][j] = v!=0 ? v->index(j)->as_number() : 0.0;
}
}
static void set_3x3( lk::vardata_t &arg, double m[3][3] )
{
arg.empty_vector();
arg.resize(3);
for (int i=0;i<3;i++)
{
arg.index(i)->empty_vector();
arg.index(i)->resize(3);
for (int j=0;j<3;j++)
arg.index(i)->index(j)->assign( m[i][j] );
}
}
static void _dot( lk::invoke_t &cxt )
{
LK_DOC("dot", "Calculates the dot product between two 3 dimensional vectors.", "(array[3]:a, array[3]:b):real");
double a[3],b[3], dot=0;
get_3(a, cxt.arg(0));
get_3(b, cxt.arg(1));
for (int i=0;i<3;i++)
dot += a[i]*b[i];
cxt.result().assign(dot);
}
static void _euler( lk::invoke_t &cxt )
{
LK_DOC("euler", "Calculates the euler vector given an origin, aim point, and z rotation.", "(array[3]:origin, array[3]:aimpoint, real:zrot):array[3]");
double origin[3], aimpoint[3], zrot, eu[3];
get_3( origin, cxt.arg(0) ); // args[0] //origin
get_3( aimpoint, cxt.arg(1) ); // args[1] //aimpoint
zrot = cxt.arg(2).as_number(); // args[2] //zrot
::st_calc_euler_angles( origin, aimpoint, zrot, eu );
set_3(cxt.result(), eu);
}
static void _reftoloc( lk::invoke_t &cxt )
{
LK_DOC("reftoloc", "Calculates the 3x3 transform matrix from euler angles for going from reference to local coordinates.", "(array[3]:euler):array[3][3]");
double euler[3], rreftoloc[3][3], rloctoref[3][3];
get_3( euler, cxt.arg(0) );
::st_calc_transform_matrices( euler, rreftoloc, rloctoref );
set_3x3( cxt.result(), rreftoloc );
}
static void _loctoref( lk::invoke_t &cxt )
{
LK_DOC("loctoref", "Calculates the 3x3 transform matrix from euler angles for going from local to reference coordinates.", "(array[3]:euler):array[3][3]");
double euler[3], rreftoloc[3][3], rloctoref[3][3];
get_3( euler, cxt.arg(0) );
::st_calc_transform_matrices( euler, rreftoloc, rloctoref );
set_3x3( cxt.result(), rloctoref );
}
static void _toloc( lk::invoke_t &cxt )
{
LK_DOC("toloc", "Transforms a reference position/cosine point description to local coordinates given the transform matrix.", "(array[3]:posref, array[3]:cosref, array[3]:origin, array[3][3]:reftoloc, array[3]:posloc, array[3]:cosloc):void");
double posref[3], cosref[3], origin[3], rreftoloc[3][3], posloc[3], cosloc[3];
get_3(posref, cxt.arg(0));
get_3(cosref, cxt.arg(1));
get_3(origin, cxt.arg(2));
get_3x3(rreftoloc, cxt.arg(3));
::st_transform_to_local( posref, cosref, origin, rreftoloc, posloc, cosloc);
set_3(cxt.arg(4), posloc);
set_3(cxt.arg(5), cosloc);
}
static void _toref( lk::invoke_t &cxt )
{
LK_DOC("toref", "Transforms a local position/cosine point description to reference coordinates given the transform matrix.", "(array[3]:posloc, array[3]:cosloc, array[3]:origin, array[3][3]:loctoref, array[3]:posref, array[3]:cosref):void");
double posloc[3], cosloc[3], origin[3], rloctoref[3][3], posref[3], cosref[3];
get_3(posloc, cxt.arg(0));
get_3(cosloc, cxt.arg(1));
get_3(origin, cxt.arg(2));
get_3x3(rloctoref, cxt.arg(3));
::st_transform_to_reference(posloc,cosloc,origin,rloctoref,posref,cosref);
set_3( cxt.arg(4), posref );
set_3( cxt.arg(5), cosref );
}
static void _transpose( lk::invoke_t &cxt )
{
LK_DOC("transpose", "Transposes a 3x3 matrix.", "(array[3][3]):array[3][3]");
double m1[3][3], m2[3][3];
get_3x3( m1, cxt.arg(0) );
::st_matrix_transpose( m1, m2 );
set_3x3( cxt.result(), m2 );
}
static void _matvecmult( lk::invoke_t &cxt )
{
LK_DOC("matvecmult", "Multiples a 3x3 matrix and a vector, returning the result.", "(array[3][3]:m, array[3]:v):array[3]");
double m[3][3], v[3], mxv[3];
get_3x3(m, cxt.arg(0));
get_3(v, cxt.arg(1));
::st_matrix_vector_mult(m,v,mxv);
set_3( cxt.result(), mxv );
}
static void _workdir( lk::invoke_t &cxt )
{
LK_DOC2("workdir", "Gets or sets the current SolTrace working directory.",
"Returns the current working directory for SolTrace.", "(void):string",
"Sets the current working directory for SolTrace.", "(string:path):void");
if (cxt.arg_count() == 1)
MainWindow::Instance().GetTrace()->SetWorkDir( cxt.arg(0).as_string() );
else
cxt.result().assign( MainWindow::Instance().GetTrace()->GetWorkDir() );
}
static void _file_name( lk::invoke_t &cxt )
{
LK_DOC("file_name", "Returns the current SolTrace project file name.", "(void):string");
cxt.result().assign( MainWindow::Instance().GetFileName() );
}
static void _save_project( lk::invoke_t &cxt )
{
LK_DOC("save_project", "Saves the current SolTrace project as an .stinput file.", "({string:optional file name}):boolean");
wxString file = MainWindow::Instance().GetFileName();
if (cxt.arg_count() > 0) file = cxt.arg(0).as_string();
if (!file.IsEmpty()) cxt.result().assign( MainWindow::Instance().SaveProject(file, true) ? 1.0 : 0.0 );
else cxt.result().assign( 0.0 );
}
static void _open_project( lk::invoke_t &cxt )
{
LK_DOC("open_project", "Opens an existing SolTrace .stinput project file, without asking to save or close any currently opened project.", "(string:filename):boolean");
wxString file = cxt.arg(0).as_string();
MainWindow::Instance().CloseProject( true );
cxt.result().assign( MainWindow::Instance().LoadProject( file, true ) ? 1.0 : 0.0 );
}
static void _clear_project( lk::invoke_t &cxt )
{
LK_DOC("clear_project", "Clears the current SolTrace project, without asking to save or close first.", "(void):void");
MainWindow::Instance().CloseProject( true );
}
static void _traceopt( lk::invoke_t &cxt )
{
LK_DOC2("traceopt", "Two modes of operation: Gets or sets ray trace parameters. For example: traceopt( {\"seed\"=152, \"cpus\"=3} ) sets the seed value to 152 and the number of CPUs to use to 3.",
"Sets various ray trace parameters. The argument is a table with keys {rays,maxrays,cpus,seed,include_sunshape,optical_errors,point_focus}, whose values are the corresponding integers.", "(table:parameters):void",
"Returns a table with the following five fields filled in with their integer values: {rays,maxrays,cpus,seed,include_sunshape,optical_errors,point_focus}", "(void):table" );
TraceForm *tf = MainWindow::Instance().GetTrace();
size_t nrays, nmax;
int ncpu, seed, runner_type;
bool ss, oe, pf, rf, native_runner, native_runner_file, optix_runner, optix_runner_file;
tf->GetOptions(&nrays, &nmax, &ncpu, &seed, &ss, &oe, &pf, &rf, &runner_type);
if (cxt.arg_count() == 0)
{
lk::vardata_t &r = cxt.result();
r.empty_hash();
r.hash_item("rays", nrays );
r.hash_item("maxrays", nmax );
r.hash_item("cpus", ncpu );
r.hash_item("seed", seed );
r.hash_item("include_sunshape", ss ? 1.0 : 0.0 );
r.hash_item("optical_errors", oe ? 1.0 : 0.0 );
r.hash_item("point_focus", pf ? 1.0 : 0.0 );
r.hash_item("use_refactor_trace", rf ? 1.0 : 0.0);
r.hash_item("use_native_runner", native_runner ? 1.0 : 0.0);
r.hash_item("use_optix_runner", optix_runner ? 1.0 : 0.0);
}
else if (cxt.arg_count() == 1)
{
lk::vardata_t *vval = 0;
if ( (vval = cxt.arg(0).lookup("rays")) )
nrays = vval->deref().as_unsigned();
if ( (vval = cxt.arg(0).lookup("maxrays")) )
nmax = vval->deref().as_unsigned();
if ( (vval = cxt.arg(0).lookup("cpus")) )
ncpu = vval->deref().as_integer();
if ( (vval = cxt.arg(0).lookup("seed")) )
seed = vval->deref().as_integer();
if ( (vval = cxt.arg(0).lookup("include_sunshape")) )
ss = vval->deref().as_integer() ? true : false;
if ( (vval = cxt.arg(0).lookup("optical_errors")) )
oe = vval->deref().as_integer() ? true : false;
if ( (vval = cxt.arg(0).lookup("point_focus")) )
pf = vval->deref().as_integer() ? true : false;
if ((vval = cxt.arg(0).lookup("use_refactor_trace")))
rf = vval->deref().as_integer() ? true : false;
if ((vval = cxt.arg(0).lookup("runner_type")))
runner_type = vval->deref().as_unsigned();
tf->SetOptions( nrays, nmax, ncpu, seed, ss, oe, pf, rf, runner_type );
}
else
{
cxt.error("invalid number of arguments to traceopt. must be 0 or 1");
}
}
static void _trace( lk::invoke_t &cxt )
{
LK_DOC("trace", "Starts a new ray trace operation. Returns time elapsed in milliseconds, or negative if an error occured or was canceled. Optionally, fills the 1st argument with error messages if passed in.", "( [string:errors] ):integer");
wxArrayString errors;
int ms = MainWindow::Instance().GetTrace()->StartTrace( true, true, &errors );
if ( ms < 0 && cxt.arg_count() == 1 )
cxt.arg(0).assign( wxJoin( errors, '\n' ) );
cxt.result().assign( ms );
}
static void _nintersect( lk::invoke_t &cxt )
{
LK_DOC2("nintersect", "Two modes of operation: returns the total number of intersections calculated, or the number of intersections with a particular element (stagenum, elementnum)",
"Returns the number of ray intersections in the results.", "(void):integer",
"Returns the number of ray intersections with a particular element.", "(integer:stagenum, integer:elementnum):integer");
if (cxt.arg_count() == 0)
{
cxt.result().assign( MainWindow::Instance().GetProject().Results.Length );
}
else if (cxt.arg_count() == 2)
{
Element *e = MainWindow::Instance().GetProject().GetElement(
cxt.arg(0).as_integer(), cxt.arg(1).as_integer() );
cxt.result().assign( e ? e->RayHits : 0.0 );
}
else
cxt.error("invalid number of arguments to nintersect. must be 0 or 2");
}
static void _raydata( lk::invoke_t &cxt )
{
LK_DOC( "raydata", "Returns the ray data as a 9 item array in stage coordinates [X,Y,Z,CosX,CosY,CosZ,Element,Stage,RayNum] for the specified intersection number. Use nintersect to get the number of intersections.", "(integer:index):array");
Project &prj = MainWindow::Instance().GetProject();
size_t idx = cxt.arg(0).as_unsigned();
if (idx <= prj.Results.Length)
{
lk::vardata_t &r = cxt.result();
r.empty_vector();
r.vec_append( prj.Results.Xi[idx] );
r.vec_append( prj.Results.Yi[idx] );
r.vec_append( prj.Results.Zi[idx] );
r.vec_append( prj.Results.Xc[idx] );
r.vec_append( prj.Results.Yc[idx] );
r.vec_append( prj.Results.Zc[idx] );
r.vec_append( prj.Results.ElementMap[idx] );
r.vec_append( prj.Results.StageMap[idx] );
r.vec_append( prj.Results.RayNumbers[idx] );
}
else
cxt.result().nullify();
}
static void _sundata( lk::invoke_t &cxt )
{
LK_DOC( "sundata", "Returns the number of generated sun rays and the extents as a table with fields {nrays,xmin,xmax,ymin,ymax}.", "(void):table");
Project &prj = MainWindow::Instance().GetProject();
lk::vardata_t &r = cxt.result();
r.empty_hash();
r.hash_item( "nrays", prj.Results.SunRayCount );
r.hash_item( "xmin", prj.Results.SunXMin );
r.hash_item( "xmax", prj.Results.SunXMax );
r.hash_item( "ymin", prj.Results.SunYMin );
r.hash_item( "ymax", prj.Results.SunYMax );
}
static void _sunopt( lk::invoke_t &cxt )
{
LK_DOC2("sunopt", "Two modes of operation. Gets or sets the sun shape parameters using a table with fields {ptsrc:boolean, shape:character, sigma:real, halfwidth:real, x:real, y:real, z:real, lat:real, day:real, hour:real, userdata:array[array[2]]}.",
"Sets various sun shape parameters. The argument is a table with keys {ptsrc, shape, sigma, halfwidth, x, y, z, useldh, lat, day, hour, userdata}.", "(table:parameters):void",
"Gets various sun shape parameters as a field indexed table.", "(void):table");
SunShape &sun = MainWindow::Instance().GetProject().Sun;
if (cxt.arg_count() == 1)
{
lk::vardata_t &h = cxt.arg(0);
lk::vardata_t *v = 0;
if ( (v=h.lookup("ptsrc")) )
sun.PointSource = v->deref().as_boolean();
if ( (v=h.lookup("shape")) )
{
sun.Shape = SunShape::PILLBOX;
wxString s = v->deref().as_string();
if (s.length() > 0)
{
if (tolower(s[0]) == 'g') sun.Shape = SunShape::GAUSSIAN;
if (tolower(s[0]) == 'u' || tolower(s[0]) == 'd') sun.Shape = SunShape::USER_DEFINED;
}
}
if ( (v=h.lookup("sigma")) )
sun.Sigma = v->deref().as_number();
if ( (v=h.lookup("halfwidth")) )
sun.HalfWidth = v->deref().as_number();
if ( (v=h.lookup("x")) )
sun.X = v->deref().as_number();
if ( (v=h.lookup("y")) )
sun.Y = v->deref().as_number();
if ( (v=h.lookup("z")) )
sun.Z = v->deref().as_number();
if ( (v=h.lookup("useldh")) )
sun.UseLDHSpec = v->deref().as_boolean();
if ( (v=h.lookup("lat")) )
sun.Latitude = v->deref().as_number();
if ( (v=h.lookup("day")) )
sun.Day = v->deref().as_number();
if ( (v=h.lookup("hour")) )
sun.Hour = v->deref().as_number();
if ( (v=h.lookup("userdata")) )
{
std::vector<lk::vardata_t> *arr = v->deref().vec();
sun.UserShapeData.resize( arr->size() );
for (size_t i=0;i<arr->size();i++)
{
std::vector<lk::vardata_t> *p = arr->at(i).deref().vec();
if (p->size() == 2)
{
sun.UserShapeData[i].x = p->at(0).as_number();
sun.UserShapeData[i].y = p->at(1).as_number();
}
}
}
MainWindow::Instance().GetSunShape()->UpdateFromData();
MainWindow::Instance().SetModified();
}
else if (cxt.arg_count() == 0)
{
lk::vardata_t &r = cxt.result();
r.empty_hash();
r.hash_item( "ptsrc", sun.PointSource ? 1.0 : 0.0 );
wxString shape = "g";
if (sun.Shape == SunShape::PILLBOX) shape = "p";
if (sun.Shape == SunShape::USER_DEFINED) shape = "u";
r.hash_item( "shape", shape );
r.hash_item( "sigma", sun.Sigma );
r.hash_item( "halfwidth", sun.HalfWidth );
r.hash_item( "x", sun.X );
r.hash_item( "y", sun.Y );
r.hash_item( "z", sun.Z );
r.hash_item( "useldh", sun.UseLDHSpec ? 1.0 : 0.0 );
r.hash_item( "lat", sun.Latitude );
r.hash_item( "day", sun.Day );
r.hash_item( "hour", sun.Hour );
lk::vardata_t ud;
ud.empty_vector();
ud.resize( sun.UserShapeData.size() );
for (int i=0;i<sun.UserShapeData.size();i++)
{
ud.index(i)->empty_vector();
ud.index(i)->resize(2);
ud.index(i)->index(0)->assign( sun.UserShapeData[i].x );
ud.index(i)->index(1)->assign( sun.UserShapeData[i].y );
}
r.hash_item( "userdata", ud );
}
else
cxt.error("invalid number of arguments for sunopt. must be 1 or 0.");
}
static void _addoptic( lk::invoke_t &cxt )
{
LK_DOC("addoptic", "Adds a new optical property set with the given name.", "(string:name):void");
wxString name = cxt.arg(0).as_string();
if (name.empty()) name = "untitled optic";
MainWindow::Instance().GetOptics()->AddOptic( name );
}
static void _clearoptics( lk::invoke_t &cxt )
{
LK_DOC("clearoptics", "Deletes all of the optical property sets.", "(void):void");
MainWindow::Instance().GetOptics()->ClearOptics();
}
static void _listoptics( lk::invoke_t &cxt )
{
LK_DOC("listoptics", "Returns a list of all the optical property sets.", "(void):array");
cxt.result().empty_vector();
Project &prj = MainWindow::Instance().GetProject();
for (size_t i=0;i<prj.OpticsList.size();i++)
cxt.result().vec_append( prj.OpticsList[i]->Name );
}
static void _opticopt( lk::invoke_t &cxt )
{
LK_DOC2("opticopt", "Two modes of operation: gets or sets optical property information, using a table with fields {dist=string, apstop=integer, surfnum=integer, difford=integer, refl=real, trans=real, errslope=real, errspec=real, refractr=real, refracti=real, grating=array[4], refltable=array[nx2]}",
"Set various optical properties for the given optic name and side (1=front,2=back) with a table whose fields are {dist, apstop, surfnum, difford, refl, trans, errslope, errspec, refractr, refracti, grating, refltable}", "(string:name, integer:front or back, table:properties):void",
"Get optical property information as table with named fields for the given optic name and side (1=front,2=back)", "(string:name, integer:front or back):table");
wxString name = cxt.arg(0).as_string();
int side = cxt.arg(1).as_integer();
Project &prj = MainWindow::Instance().GetProject();
Optical *opt = 0;
for (size_t idx=0;idx<prj.OpticsList.size();idx++)
if (prj.OpticsList[idx]->Name.CmpNoCase(name)==0)
opt = prj.OpticsList[idx];
if (!opt)
{
cxt.result().nullify();
return;
}
SurfaceOptic &o = (side==2) ? opt->Back : opt->Front;
//{ dist=string, apstop=integer, surfnum=integer, difford=integer,
// refl=real, trans=real, errslope=real, errspec=real, refractr=real, refracti=real,
// grating=array[4]}
if (cxt.arg_count() == 3)
{
lk::vardata_t &h = cxt.arg(2);
lk::vardata_t *v = 0;
if ( (v=h.lookup("dist")) )
{
char d = 'g';
wxString param = v->deref().as_string();
if ( param.length() > 0 && tolower( param[0] ) == 'p' ) d = 'p';
if ( param.length() > 0 && tolower( param[0] ) == 'f' ) d = 'f';
o.ErrorDistribution = d;
}
if ( (v=h.lookup("apstop")) )
o.ApertureStopOrGratingType = v->deref().as_integer();
if ( (v=h.lookup("surfnum")) )
o.OpticalSurfaceNumber = v->deref().as_integer();
if ( (v=h.lookup("difford")) )
o.DiffractionOrder = v->deref().as_integer();
if ( (v=h.lookup("refl")) )
o.Reflectivity = v->deref().as_number();
if ( (v=h.lookup("trans")) )
o.Transmissivity = v->deref().as_number();
if ( (v=h.lookup("errslope")) )
o.RMSSlope = v->deref().as_number();
if ( (v=h.lookup("errspec")) )
o.RMSSpecularity = v->deref().as_number();
if ( (v=h.lookup("refractr")) )
o.RefractionIndexReal = v->deref().as_number();
if ( (v=h.lookup("refracti")) )
o.RefractionIndexImag = v->deref().as_number();
if ( (v=h.lookup("grating")) )
{
for (int i=0;i<4;i++)
o.GratingCoeffs[i] = v->deref().index(i)->as_number();
}
if ( (v=h.lookup("refltable")) )
{
if (v->deref().type() == lk::vardata_t::NULLVAL)
o.UseReflectivityTable = false;
else
{
o.UseReflectivityTable = true;
size_t npoints = v->deref().length();
if (npoints > 0)
o.ReflectivityTable.resize( npoints );
for (size_t i=0;i<npoints;i++)
{
o.ReflectivityTable[i].x = 0.0;
o.ReflectivityTable[i].y = 0.0;
lk::vardata_t *item = v->deref().index(i);
if ( item != 0 )
{
if (lk::vardata_t *angle = item->deref().index(0))
o.ReflectivityTable[i].x = angle->as_number();
if (lk::vardata_t *refl = item->deref().index(1))
o.ReflectivityTable[i].y = refl->as_number();
}
}
}
}
MainWindow::Instance().GetOptics()->UpdateOptForms();
MainWindow::Instance().SetModified();
}
else if (cxt.arg_count() == 2)
{
lk::vardata_t &r = cxt.result();
r.empty_hash();
wxString buf;
buf += o.ErrorDistribution;
r.hash_item( "dist", buf);
r.hash_item( "apstop", o.ApertureStopOrGratingType );
r.hash_item( "surfnum", o.OpticalSurfaceNumber );
r.hash_item( "difford", o.DiffractionOrder );
r.hash_item( "refl", o.Reflectivity );
r.hash_item( "trans", o.Transmissivity );
r.hash_item( "errslope", o.RMSSlope );
r.hash_item( "errspec", o.RMSSpecularity );
r.hash_item( "refractr", o.RefractionIndexReal );
r.hash_item( "refracti", o.RefractionIndexImag );
lk::vardata_t grat;
grat.empty_vector();
grat.resize(4);
for (int j=0;j<4;j++)
grat.index(j)->assign( o.GratingCoeffs[j] );
r.hash_item( "grating", grat );
size_t npoints = o.ReflectivityTable.size();
if (o.UseReflectivityTable && npoints > 0)
{
lk::vardata_t tab;
tab.empty_vector();
tab.resize( npoints );
for (size_t j=0;j<npoints;j++)
{
tab.index(j)->empty_vector();
tab.index(j)->resize(2);
tab.index(j)->index(0)->assign( o.ReflectivityTable[j].x );
tab.index(j)->index(1)->assign( o.ReflectivityTable[j].y );
}
r.hash_item( "refltable", tab );
}
else
r.hash_item( "refltable", lk::vardata_t() ); // set to NULL value
}
else
cxt.error("invalid number of arguments to opticopt. must be 3 or 2.");
}
// tracks the currently active stage for the elementXX functions
static wxString active_stage_name;
static void _addstage( lk::invoke_t &cxt )
{
LK_DOC("addstage", "Adds a stage to the system with the given name. Sets the new stage as the currently active stage.", "(string:name):void");
wxString name = cxt.arg(0).as_string();
if (name.empty()) name = "untitled stage";
MainWindow::Instance().GetGeometry()->NewStage( name );
active_stage_name = name;
}
static void _clearstages(lk::invoke_t &cxt)
{
LK_DOC("clearstages", "Clears all stages from the project.", "(void):void");
MainWindow::Instance().GetGeometry()->ClearStages();
active_stage_name.Clear();
}
static void _liststages( lk::invoke_t &cxt )
{
LK_DOC("liststages", "Returns a list of all the stage names.", "(void):array");
cxt.result().empty_vector();
Project &prj = MainWindow::Instance().GetProject();
for (size_t i=0;i<prj.StageList.size();i++)
cxt.result().vec_append( prj.StageList[i]->Name );
}
static void _activestage( lk::invoke_t &cxt )
{
LK_DOC2("activestage", "Two modes of operation: gets or sets the currently active stage that the various 'elementXX functions operate on.",
"Set the currently active stage. Returns the active stage name also.", "(string:stage name):string",
"Get the currently active stage name.", "(void):string");
if (MainWindow::Instance().GetProject().StageList.size() == 0)
active_stage_name.Clear();
if ( cxt.arg_count() > 0)
{
wxString name = cxt.arg(0).as_string();
Project &prj = MainWindow::Instance().GetProject();
for (size_t i=0;i<prj.StageList.size();i++)
if (prj.StageList[i]->Name.CmpNoCase( name ) == 0)
active_stage_name = prj.StageList[i]->Name;
}
cxt.result().assign( active_stage_name );
}
static void _stageopt( lk::invoke_t &cxt )
{
LK_DOC2("stageopt", "Two modes of operation. Gets or sets stage properties using a table with fields { virtual:boolean, multihit:boolean, tracethrough:boolean, x:real, y:real, z:real, ax:real, ay:real, az:real, zrot:real }",
"Set various stage properties for the given stage name with a table whose fields can include { virtual, multihit, tracethrough, x, y, z, ax, ay, az, zrot }", "(string:stage name, table:properties):void",
"Get stage properties for the given stage name as a table.", "(string:stage name):table");
wxString name = cxt.arg(0).as_string();
Project &prj = MainWindow::Instance().GetProject();
Stage *stage = 0;
for (size_t idx=0;idx<prj.StageList.size();idx++)
if (prj.StageList[idx]->Name.CmpNoCase(name)==0)
stage = prj.StageList[idx];
if (!stage)
{
cxt.result().nullify();
return;
}
if (cxt.arg_count() == 2)
{
lk::vardata_t &h = cxt.arg(1);
lk::vardata_t *v = 0;
if ( (v=h.lookup("virtual")) )
stage->Virtual = v->deref().as_boolean();
if ( (v=h.lookup("multihit")) )
stage->MultiHit = v->deref().as_boolean();
if ( (v=h.lookup("tracethrough")) )
stage->TraceThrough = v->deref().as_boolean();
if ( (v=h.lookup("x")) )
stage->X = v->deref().as_number();
if ( (v=h.lookup("y")) )
stage->Y = v->deref().as_number();
if ( (v=h.lookup("z")) )
stage->Z = v->deref().as_number();
if ( (v=h.lookup("ax")) )
stage->AX = v->deref().as_number();
if ( (v=h.lookup("ay")) )
stage->AY = v->deref().as_number();
if ( (v=h.lookup("az")) )
stage->AZ = v->deref().as_number();
if ( (v=h.lookup("zrot")) )
stage->ZRot = v->deref().as_number();
if ( StageForm *sf = MainWindow::Instance().GetGeometry()->GetStageForm( stage ) )
sf->UpdateFromData();
MainWindow::Instance().SetModified();
}
else if (cxt.arg_count() == 1)
{
lk::vardata_t &r = cxt.result();
r.empty_hash();
r.hash_item( "virtual", stage->Virtual ? 1.0 : 0.0 );
r.hash_item( "multihit", stage->MultiHit ? 1.0 : 0.0 );
r.hash_item( "tracethrough", stage->TraceThrough ? 1.0 : 0.0 );
r.hash_item( "x", stage->X );
r.hash_item( "y", stage->Y );
r.hash_item( "z", stage->Z );
r.hash_item( "ax", stage->AX );
r.hash_item( "ay", stage->AY );
r.hash_item( "az", stage->AZ );
r.hash_item( "zrot", stage->ZRot );
}
else
cxt.error("invalid number of arguments to stageopt. must be 2 or 1.");
}
static bool deref_stage( Stage **pstage, StageForm **pform )
{
Project &prj = MainWindow::Instance().GetProject();
for (size_t i=0;i<prj.StageList.size();i++)
{
if (prj.StageList[i]->Name.CmpNoCase( active_stage_name ) == 0)
{
*pstage = prj.StageList[i];
*pform = MainWindow::Instance().GetGeometry()->GetStageForm( prj.StageList[i] );
return *pstage && *pform;
}
}
return false;
}
static void _addelement( lk::invoke_t &cxt )
{
LK_DOC("addelement", "Adds one (or optionally more) elements to the active stage.", "([integer:optional number of elements]):void");
Stage *s = 0;
StageForm *sf = 0;
if (!deref_stage( &s, &sf )) return;
int n = 1;
if (cxt.arg_count() > 0)
n = cxt.arg(0).as_integer();
if (n < 1) n = 1;
if (n > 1000) n = 1000;
sf->Append( n );
MainWindow::Instance().SetModified();
}
static void _clearelements( lk::invoke_t &cxt )
{
LK_DOC("clearelements", "Clears all the elements from the currently active stage.", "(void):void");
Stage *s = 0;
StageForm *sf = 0;
if (!deref_stage( &s, &sf )) return;
sf->Clear();
MainWindow::Instance().SetModified();
}
static void _nelements( lk::invoke_t &cxt )
{
LK_DOC("nelements", "Returns the number of elements in the current stage.", "(void):integer");
Stage *s = 0;
StageForm *sf = 0;
if (!deref_stage( &s, &sf )) return;
cxt.result().assign( (double) s->ElementList.size() );
}
static lk::vardata_t apersurfvar( char index, double params[8], const wxString &file = "" )
{
wxString buf;
buf += tolower(index);
lk::vardata_t r;
r.empty_vector();
if (!file.empty())
{
r.vec_append( buf );
r.vec_append(file);
}
else
{
r.vec_append( buf );
for (int i=0;i<8;i++)
r.vec_append( params[i] );
}
return r;
}
static void apersurfparse( std::vector<lk::vardata_t> *vec, char *index, double params[8], wxString *file)
{
if (!vec || vec->size() < 2) return;
wxString buf = vec->at(0).as_string();
if (buf.length() < 0) return;
*index = buf[0];
if (file != 0 && vec->size() == 2)
{
*file = vec->at(1).as_string();
for (int i=0;i<8;i++)
params[i] = 0.0;
}
else
{
for (size_t i=0;i<8;i++)
{
if (i+1 < vec->size())
params[i] = vec->at(i+1).as_number();
else
params[i] = 0.0;
}
}
}
static void _elementopt( lk::invoke_t &cxt )
{
LK_DOC2("elementopt", "Two modes of operation: gets or sets various element properties for the given element in the currently active stage. The parameters are transferred via a table with named fields { en:boolean, x:real, y:real, z:real, ax:real, ay:real, az:real, zrot:real, aper:array, surf:array, interact:string, optic:string, comment:string }.",
"Set various element properties for the specified element in the active stage. Table fields are { en, x, y, z, ax, ay, az, zrot, aper, surf, interact, optic, comment }", "(integer:element index, table:properties):void",
"Gets the properties of the specified element in the active stage.", "(integer:element index):table");
Stage *s = 0;
StageForm *sf = 0;
if (!deref_stage( &s, &sf ))
{
cxt.result().nullify();
return;
}
Element *e = 0;
size_t idx = cxt.arg(0).as_unsigned();
if (idx < s->ElementList.size())
e = s->ElementList[idx];
if (!e)
{
cxt.result().nullify();
return;
}
if (cxt.arg_count() == 2)
{
lk::vardata_t &h = cxt.arg(1);
lk::vardata_t *v = 0;
if ( (v=h.lookup("en")) )
e->Enabled = v->deref().as_boolean();
if ( (v=h.lookup("x")) )
e->X = v->deref().as_number();
if ( (v=h.lookup("y")) )
e->Y = v->deref().as_number();
if ( (v=h.lookup("z")) )
e->Z = v->deref().as_number();
if ( (v=h.lookup("ax")) )
e->AX = v->deref().as_number();
if ( (v=h.lookup("ay")) )
e->AY = v->deref().as_number();
if ( (v=h.lookup("az")) )
e->AZ = v->deref().as_number();
if ( (v=h.lookup("zrot")) )
e->ZRot = v->deref().as_number();
if ( (v=h.lookup("aper")) )
apersurfparse( v->deref().vec(), &e->ApertureIndex, e->ApertureParams, 0 );
if ( (v=h.lookup("surf")) )
apersurfparse( v->deref().vec(), &e->SurfaceIndex, e->SurfaceParams, &e->SurfaceFile );
if ( (v=h.lookup("interact")) )
{
wxString buf = v->deref().as_string();
if ( buf == "refraction" )
e->InteractionType = Element::REFRACTION;
else
e->InteractionType = Element::REFLECTION;
}
if ( (v=h.lookup("optic")) )
e->OpticName = v->deref().as_string();
if ( (v=h.lookup("comment")) )
e->Comment = v->deref().as_string();
sf->UpdateFromData();
MainWindow::Instance().SetModified();
}
else if (cxt.arg_count() == 1)
{
lk::vardata_t &r = cxt.result();
r.empty_hash();
r.hash_item( "en", e->Enabled ? 1.0 : 0.0 );
r.hash_item( "x", e->X );
r.hash_item( "y", e->Y );
r.hash_item( "z", e->Z );
r.hash_item( "ax", e->AX );
r.hash_item( "ay", e->AY );
r.hash_item( "az", e->AZ );
r.hash_item( "zrot", e->ZRot );
r.hash_item( "aper", apersurfvar(e->ApertureIndex, e->ApertureParams) );
r.hash_item( "surf", apersurfvar(e->SurfaceIndex, e->SurfaceParams, e->SurfaceFile) );
r.hash_item( "interact", e->InteractionType == Element::REFLECTION ? "reflection" : "refraction" );
r.hash_item( "optic", e->OpticName );
r.hash_item( "comment", e->Comment );
}
else
cxt.error("invalid number of arguments. must be 2 or 1.");
}