forked from mcneel/opennurbs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopennurbs_sun.cpp
1673 lines (1345 loc) · 47.1 KB
/
opennurbs_sun.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
//
// Copyright (c) 1993-2022 Robert McNeel & Associates. All rights reserved.
// OpenNURBS, Rhinoceros, and Rhino3D are registered trademarks of Robert
// McNeel & Associates.
//
// THIS SOFTWARE IS PROVIDED "AS IS" WITHOUT EXPRESS OR IMPLIED WARRANTY.
// ALL IMPLIED WARRANTIES OF FITNESS FOR ANY PARTICULAR PURPOSE AND OF
// MERCHANTABILITY ARE HEREBY DISCLAIMED.
//
// For complete openNURBS copyright information see <http://www.opennurbs.org>.
//
////////////////////////////////////////////////////////////////
#include "opennurbs.h"
#include "opennurbs_internal_defines.h"
#if !defined(ON_COMPILING_OPENNURBS)
// This check is included in all opennurbs source .c and .cpp files to insure
// ON_COMPILING_OPENNURBS is defined when opennurbs source is compiled.
// When opennurbs source is being compiled, ON_COMPILING_OPENNURBS is defined
// and the opennurbs .h files alter what is declared and how it is declared.
#error ON_COMPILING_OPENNURBS must be defined when compiling opennurbs
#endif
// ShadowIntensity in the Sun and Skylight are currently unused. The only ShadowIntensity that's actually
// used is the one in the lights. See [SHADOW_INTENSITY_UNUSED]
static const wchar_t* XMLPath_Sun(void)
{
return ON_RDK_DOCUMENT ON_XML_SLASH ON_RDK_SETTINGS ON_XML_SLASH ON_RDK_SUN;
}
static double Sin(double deg) { return sin(ON_RadiansFromDegrees(deg)); }
static double Cos(double deg) { return cos(ON_RadiansFromDegrees(deg)); }
static double Tan(double deg) { return tan(ON_RadiansFromDegrees(deg)); }
static double ArcSin(double sine) { return ON_DegreesFromRadians(asin(sine)); }
static double ArcTan2(double dy, double dx) { return ON_DegreesFromRadians(atan2(dy, dx)); }
static double WorldToCompass(double d) { return 90.0 - d; }
static double TwilightZone(void) { return 3.0; }
static constexpr double c2 = 14388.0;
static constexpr double monitor_white = 5000.0;
static bool IsLeapYear(int year)
{
// The year can only be a leap year if it is divisible by 4.
if (0 != (year % 4))
return false;
// If the year is a century it is only a leap year if it is divisible by 400.
if ((0 == (year % 100)) && (0 != (year % 400)))
return false;
return true;
}
static double Planck(double lambda, double temp)
{
static const double E = 2.7182818284590452354;
return (pow(double(lambda), -5.0) / (pow(E, c2 / (lambda * temp)) - 1.0));
}
static ON_4fColor ColorTemperature(double temperature)
{
// Use a variant of Planck's equation to get values for the three CIE wavelengths.
double temp = monitor_white;
double er = Planck(0.60, temp);
double eg = Planck(0.56, temp);
double eb = Planck(0.44, temp);
double es = 1.0 / std::max(er, std::max(eg, eb));
const double r_white = er * es;
const double g_white = eg * es;
const double b_white = eb * es;
temp = temperature;
er = Planck(0.60, temp);
eg = Planck(0.56, temp);
eb = Planck(0.44, temp);
es = 1.0 / std::max(er,std::max(eg, eb));
const double r = er * es / r_white;
const double g = eg * es / g_white;
const double b = eb * es / b_white;
es = 1.0 / std::max(r, std::max(g, b));
const float rr = float(pow(r * es, 0.15));
const float gr = float(pow(g * es, 0.15));
const float br = float(pow(b * es, 0.15));
return ON_4fColor(rr, gr, br, 1.0f);
}
template <typename T>
static ON__UINT32 UpdateCRC(ON__UINT32 crc, T x)
{
return ON_CRC32(crc, sizeof(x), &x);
}
inline static double Int(double x)
{
return (x < 0.0) ? ceil(x) : floor(x);
}
inline static double Frac(double x)
{
return x - Int(x);
}
inline static double Unwind(double dDegrees)
{
dDegrees = Frac(dDegrees / 360.0) * 360.0;
if (dDegrees < 0.0)
dDegrees += 360.0;
return dDegrees;
}
static bool IsVectorEqual(const ON_3dVector& v1, const ON_3dVector& v2)
{
return IsDoubleEqual(v1.x, v2.x) && IsDoubleEqual(v1.y, v2.y) && IsDoubleEqual(v1.z, v2.z);
}
static ON_3dVector PerpendicularVectorOnXYPlane(const ON_3dVector& vec)
{
return ON_2dVector(vec.x, vec.y).IsTiny() ? ON_3dVector(vec.z, 0.0, -vec.x) : ON_3dVector(-vec.y, vec.x, 0.0);
}
static double AngleFromVectors(const ON_3dVector& v1, const ON_3dVector& v2, ON_3dVector normal)
{
if (IsVectorEqual(v1, v2))
return 0.0;
normal.Unitize();
const double numerator = v1 * v2;
const double denominator = v1.Length() * v2.Length();
auto cross = ON_CrossProduct(v1, v2);
cross.Unitize();
if (IsVectorEqual(cross, ON_3dVector::ZeroVector))
{
if (IsDoubleEqual(numerator, +1.0))
return 0.0;
if (IsDoubleEqual(numerator, -1.0))
return ON_PI;
}
double division = numerator / denominator;
if (division > 1.0)
division = 1.0;
else
if (division < -1.0)
division = -1.0;
if (IsDoubleEqual(division, -1.0))
return ON_PI;
double angle = acos(division);
// Check if cross is parallel or anti-parallel to normal vector. If anti-parallel then angle = 360 - angle.
const double dot = cross * normal;
if (IsDoubleEqual(dot, -1.0))
angle = (ON_PI * 2.0) - angle;
return angle;
}
// Reference: Jean Meeus - 'Astronomical Algorithms', second edition.
class ON_SunEngine::CImpl final
{
public:
void UpdateIfModified(void);
double JulianDay(void) const
{
return _local_julian_day - (_local_tz_hours + (_local_daylight_mins / 60.0)) / 24.0;
}
public:
double _azimuth = 0.0;
double _altitude = 0.0;
double _latitude = 0.0;
double _longitude = 0.0;
double _local_julian_day = 0.0;
double _local_tz_hours = 0.0;
int _local_daylight_mins = 0;
double _cache_right_ascension = 0.0;
double _cache_sin_declination = 0.0;
double _cache_cos_declination = 0.0;
double _cache_tan_declination = 0.0;
double _cache_sin_latitude = Sin(0.0);
double _cache_cos_latitude = Cos(0.0);
double _cache_greenwich_sidereal_time = 0.0;
bool _modified = true;
bool _julian_date_changed = true;
Accuracy _accuracy = Accuracy::Minimum;
// The obliquity of the ecliptic (the tilt of the earth's axis) is usually considered to be about 23.5
// degrees, but it actually changes very slowly with the passing centuries. These rough values are used
// when the accuracy is set to minimum.
const double _rough_cos_obliquity = 0.91747714052291862;
const double _rough_sin_obliquity = 0.39778850739794974;
};
void ON_SunEngine::CImpl::UpdateIfModified(void)
{
if (!_modified)
return;
if (_julian_date_changed)
{
const auto dJulianDayUT = JulianDay();
const auto dJulianDayUT2000 = dJulianDayUT - 2451545.0;
const auto dJulianCenturies = dJulianDayUT2000 / 36525.0;
const auto dJulianCenturies2 = dJulianCenturies * dJulianCenturies;
const auto dJulianCenturies3 = dJulianCenturies * dJulianCenturies2;
// Calculate the sun's true and apparent ecliptic longitude.
const auto dL0 = 280.46646 + 36000.76983 * dJulianCenturies + 0.0003032 * dJulianCenturies2;
auto dC = 0.0;
if ((Accuracy::Maximum == _accuracy))
{
const auto dMeanAnomaly = 357.52911 + 35999.05029 * dJulianCenturies - 0.0001537 * dJulianCenturies2;
dC = Sin(dMeanAnomaly * 3.0) * 0.000289 +
Sin(dMeanAnomaly * 2.0) * (0.019993 - 0.000101 * dJulianCenturies) +
Sin(dMeanAnomaly ) * (1.914602 - 0.004817 * dJulianCenturies -
0.000014 * dJulianCenturies2);
}
const auto dTrueLongitude = Unwind(dL0 + dC);
auto dApparentLongitude = dTrueLongitude - 0.00569;
auto dSinObliquity = _rough_sin_obliquity;
auto dCosObliquity = _rough_cos_obliquity;
if ((Accuracy::Maximum == _accuracy))
{
// Include the effect of the moon.
const auto dOmega = 125.04 - 1934.136 * dJulianCenturies;
dApparentLongitude -= 0.00478 * Sin(dOmega);
// Calculate the obliquity of the ecliptic (the tilt of the earth's axis).
const auto dObliquity = 23.439291111 - (46.81500 * dJulianCenturies -
0.000590 * dJulianCenturies2 +
0.001813 * dJulianCenturies3) / 3600.0 +
0.002560 * Cos(dOmega);
dSinObliquity = Sin(dObliquity);
dCosObliquity = Cos(dObliquity);
}
// Calculate the sun's equatorial coordinates (right ascension and declination).
const auto dSinApparentLongitude = Sin(dApparentLongitude);
const auto dCosApparentLongitude = Cos(dApparentLongitude);
const auto dDeclination = ArcSin(dSinApparentLongitude * dSinObliquity);
_cache_right_ascension = Unwind(ArcTan2(dSinApparentLongitude * dCosObliquity, dCosApparentLongitude));
_cache_sin_declination = Sin(dDeclination);
_cache_cos_declination = Cos(dDeclination);
_cache_tan_declination = Tan(dDeclination); // Declination is between -24 and +24 so no problem with Tan(90).
// Calculate the sidereal time at Greenwich, expressed in degrees.
const auto dTheta0 = 280.46061837 + (360.98564736629 * dJulianDayUT2000) +
(0.000387933 * dJulianCenturies2) - (dJulianCenturies3 / 38710000.0);
_cache_greenwich_sidereal_time = Unwind(dTheta0);
_julian_date_changed = false;
}
// Calculate the sun's local hour angle, expressed in degrees.
const auto dLocalHourAngle = _cache_greenwich_sidereal_time + _longitude - _cache_right_ascension;
// Calculate the sun's horizontal coordinates (azimuth and altitude).
const auto dSinLocalHourAngle = Sin(dLocalHourAngle);
const auto dCosLocalHourAngle = Cos(dLocalHourAngle);
_azimuth = Unwind(180.0 + ArcTan2(dSinLocalHourAngle,
dCosLocalHourAngle * _cache_sin_latitude -
_cache_tan_declination * _cache_cos_latitude));
_altitude = ArcSin(_cache_sin_latitude * _cache_sin_declination +
_cache_cos_latitude * _cache_cos_declination * dCosLocalHourAngle);
_modified = false;
}
ON_SunEngine::ON_SunEngine(Accuracy a)
{
_impl = new CImpl;
_impl->_accuracy = a;
}
ON_SunEngine::~ON_SunEngine()
{
delete _impl;
_impl = nullptr;
}
ON_SunEngine::ON_SunEngine(const ON_SunEngine& e)
{
_impl = new CImpl;
operator = (e);
}
const ON_SunEngine& ON_SunEngine::operator = (const ON_SunEngine& e)
{
_impl->_accuracy = e._impl->_accuracy;
_impl->_azimuth = e._impl->_azimuth;
_impl->_altitude = e._impl->_altitude;
_impl->_latitude = e._impl->_latitude;
_impl->_longitude = e._impl->_longitude;
_impl->_local_julian_day = e._impl->_local_julian_day;
_impl->_local_tz_hours = e._impl->_local_tz_hours;
_impl->_local_daylight_mins = e._impl->_local_daylight_mins;
_impl->_cache_right_ascension = e._impl->_cache_right_ascension;
_impl->_cache_sin_declination = e._impl->_cache_sin_declination;
_impl->_cache_cos_declination = e._impl->_cache_cos_declination;
_impl->_cache_tan_declination = e._impl->_cache_tan_declination;
_impl->_cache_sin_latitude = e._impl->_cache_sin_latitude;
_impl->_cache_cos_latitude = e._impl->_cache_cos_latitude;
_impl->_cache_greenwich_sidereal_time = e._impl->_cache_greenwich_sidereal_time;
_impl->_modified = e._impl->_modified;
_impl->_julian_date_changed = e._impl->_julian_date_changed;
return *this;
}
bool ON_SunEngine::operator == (const ON_SunEngine& e)
{
if (_impl->_azimuth != e._impl->_azimuth) return false;
if (_impl->_altitude != e._impl->_altitude) return false;
if (_impl->_latitude != e._impl->_latitude) return false;
if (_impl->_longitude != e._impl->_longitude) return false;
if (_impl->_local_julian_day != e._impl->_local_julian_day) return false;
if (_impl->_local_tz_hours != e._impl->_local_tz_hours) return false;
if (_impl->_local_daylight_mins != e._impl->_local_daylight_mins) return false;
return true;
}
bool ON_SunEngine::operator != (const ON_SunEngine& e)
{
return !(operator == (e));
}
int ON_SunEngine::DaysInMonth(int month, int year) // Static.
{
month = std::max(1, std::min(12, month));
if ((2 == month) && IsLeapYear(year))
return 29;
static const int tab[] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
return tab[month];
}
double ON_SunEngine::Latitude(void) const
{
return _impl->_latitude;
}
bool ON_SunEngine::SetLatitude(double lat)
{
if ((lat < -90.0) || (lat > 90.0))
return false;
if (_impl->_latitude != lat)
{
_impl->_latitude = lat;
_impl->_cache_sin_latitude = Sin(lat);
_impl->_cache_cos_latitude = Cos(lat);
_impl->_modified = true;
}
return true;
}
double ON_SunEngine::Longitude(void) const
{
return _impl->_longitude;
}
bool ON_SunEngine::SetLongitude(double dLong)
{
if ((dLong < -180.0) || (dLong > 180.0))
return false;
if (_impl->_longitude != dLong)
{
_impl->_longitude = dLong;
_impl->_modified = true;
}
return true;
}
bool ON_SunEngine::SetTimeZoneHours(double dHours)
{
if ((dHours < -12.0) || (dHours > 13.0))
return false;
const double dLocalTimeZoneHours = dHours;
if (_impl->_local_tz_hours != dLocalTimeZoneHours)
{
_impl->_local_tz_hours = dLocalTimeZoneHours;
_impl->_modified = true;
_impl->_julian_date_changed = true;
}
return true;
}
bool ON_SunEngine::SetDaylightSavingMinutes(int iMinutes)
{
if ((iMinutes < 0) || (iMinutes > 120))
return false;
const int iLocalDaylightMinutes = iMinutes;
if (_impl->_local_daylight_mins != iLocalDaylightMinutes)
{
_impl->_local_daylight_mins = iLocalDaylightMinutes;
_impl->_modified = true;
_impl->_julian_date_changed = true;
}
return true;
}
bool ON_SunEngine::SetLocalDateTime(int iYear, int iMonth, int iDay, double dHours)
{
if ((iYear < 1800) || (iYear > 2199) || (iMonth < 1) || (iMonth > 12))
return false;
if ((iDay < 1) || (iDay > DaysInMonth(iMonth, iYear)))
return false;
if ((dHours < 0.0) || (dHours > 24.0))
return false;
if (iMonth < 3)
{
iMonth += 12;
iYear--;
}
const int a = iYear / 100;
const int b = 2 - a + (a / 4);
const int iJulianDay = (36525 * (iYear + 4716)) / 100 + (306 * (iMonth + 1)) / 10 + iDay + b - 1524;
const double dJulianDay = iJulianDay + dHours / 24.0 - 0.5;
return SetLocalJulianDay(dJulianDay);
}
void ON_SunEngine::LocalDateTime(int& iYear, int& iMonth, int& iDay, double& dHours) const
{
const double jd = _impl->_local_julian_day + 0.5;
int b = (int)Int(jd);
const int a = (b * 100 - 186721625) / 3652425;
b += 1 + a - (a / 4) + 1524;
const int c = (b * 100 - 12210) / 36525;
const int d = (365 * c) + (c / 4);
const int e = (10000 * (b - d)) / 306001;
iDay = (int)(b - d - ((306001 * e) / 10000));
iMonth = (int)((e < 14) ? e - 1 : e - 13);
iYear = (int)((iMonth > 2) ? c - 4716 : c - 4715);
dHours = Frac(jd) * 24.0 + 1e-8;
}
bool ON_SunEngine::SetLocalJulianDay(double dLocalJulianDay)
{
if ((dLocalJulianDay < 2378496.5) || (dLocalJulianDay > 2524593.499999999))
return false;
if (_impl->_local_julian_day != dLocalJulianDay)
{
_impl->_local_julian_day = dLocalJulianDay;
_impl->_julian_date_changed = true;
_impl->_modified = true;
}
return true;
}
double ON_SunEngine::Azimuth(void) const
{
_impl->UpdateIfModified();
return _impl->_azimuth;
}
double ON_SunEngine::Altitude(void) const
{
_impl->UpdateIfModified();
return _impl->_altitude;
}
double ON_SunEngine::JulianDay(void) const
{
return _impl->JulianDay();
}
double ON_SunEngine::LocalJulianDay(void) const
{
return _impl->_local_julian_day;
}
double ON_SunEngine::TimeZoneHours(void) const
{
return _impl->_local_tz_hours;
}
int ON_SunEngine::DaylightSavingMinutes(void) const
{
return _impl->_local_daylight_mins;
}
void ON_SunEngine::ConvertHorizonCoordsToSolarVector(double dAzimuth, double dAltitude, double* dVector) // Static.
{
dVector[0] = -Cos(dAltitude) * Sin(dAzimuth);
dVector[1] = -Cos(dAltitude) * Cos(dAzimuth);
dVector[2] = -Sin(dAltitude);
}
void ON_SunEngine::ConvertSolarVectorToHorizonCoords(const double* dVector, double& dAzimuth, double& dAltitude) // Static.
{
dAltitude = ArcSin(-dVector[2]);
const double dY = dVector[0] / -Cos(dAltitude);
const double dX = dVector[1] / -Cos(dAltitude);
dAzimuth = ArcTan2(dY, dX);
if (dAzimuth < 0.0)
dAzimuth += 360.0;
}
static tm CurrentLocalTimeAsStructTM(void)
{
const time_t time = ON_SecondsSinceJanOne1970UTC();
tm ttm = { 0 };
#ifdef ON_RUNTIME_WIN
_localtime64_s(&ttm, &time);
#else
ttm = *localtime(&time);
#endif
return ttm;
}
void ON_SunEngine::GetCurrentLocalDateTime(int& y, int& m, int& d, double& h)
{
const tm ttm = CurrentLocalTimeAsStructTM();
y = ttm.tm_year + 1900;
m = ttm.tm_mon + 1;
d = ttm.tm_mday;
h = ttm.tm_hour + (ttm.tm_min / 60.0) + (ttm.tm_sec / 3600.0);
}
void ON_SunEngine::GetDefaultLocalDateTime(int& y, int& m, int& d, double& h)
{
const tm ttm = CurrentLocalTimeAsStructTM();
y = ttm.tm_year + 1900;
m = 3;
d = 21;
h = 12.0;
}
class ON_Sun::CImpl : public ON_InternalXMLImpl
{
public:
CImpl() { }
CImpl(ON_XMLNode& n) : ON_InternalXMLImpl(&n) { }
bool EnableAllowed(void) const;
void SetEnableAllowed(bool allowed);
bool EnableOn(void) const;
void SetEnableOn(bool on);
bool ManualControlAllowed(void) const;
void SetManualControlAllowed(bool allowed);
bool ManualControlOn(void) const;
void SetManualControlOn(bool manual);
double Azimuth(void) const;
void SetAzimuth(double azi);
double Altitude(void) const;
void SetAltitude(double alt);
double TimeZone(void) const;
void SetTimeZone(double hours);
double North(void) const;
void SetNorth(double);
double Latitude(void) const;
void SetLatitude(double);
double Longitude(void) const;
void SetLongitude(double);
bool DaylightSavingOn(void) const;
void SetDaylightSavingOn(bool on);
int DaylightSavingMinutes(void) const;
void SetDaylightSavingMinutes(int minutes);
void LocalDateTime(int& year, int& month, int& day, double& hours) const;
bool SetLocalDateTime(int year, int month, int day, double hours);
double Intensity(void) const;
void SetIntensity(double d);
double ShadowIntensity(void) const;
void SetShadowIntensity(double d);
bool IsUsingManualControl(void) const;
private:
void UpdateAziAlt(void) const;
void GetEarthAnchorPlane(ON_3dVector& anchor_north, ON_Plane& plane) const;
public:
mutable double _azimuth = 0.0;
mutable double _altitude = 0.0;
mutable bool _calc_dirty = true;
ON_EarthAnchorPoint* _earth_anchor_point = nullptr;
ON_SunEngine::Accuracy _accuracy = ON_SunEngine::Accuracy::Minimum;
};
bool ON_Sun::CImpl::IsUsingManualControl(void) const
{
// If manual control has been set on even if it's not 'allowed', consider it on anyway.
// This makes it easier for clients that set manual control on without knowing they are supposed
// to also allow it. I'm not even sure why it wouldn't be allowed nowadays. TODO: Ask Andy.
return ManualControlOn();
}
void ON_Sun::CImpl::GetEarthAnchorPlane(ON_3dVector& anchor_north, ON_Plane& plane) const
{
ON_3dVector anchor_east = _earth_anchor_point->ModelEast();
anchor_north = _earth_anchor_point->ModelNorth();
// Keeps original vector length if feasible.
double east_len = anchor_east .LengthAndUnitize();
double north_len = anchor_north.LengthAndUnitize();
// Compute normal of the plane P that contains m_model_east & m_model_north.
plane.zaxis = ON_CrossProduct(anchor_east, anchor_north);
if (plane.zaxis.IsTiny())
{
// Recompute a valid North and East vector.
if (!anchor_north.IsTiny())
{
// North and east are equal or east is tiny.
anchor_east = -PerpendicularVectorOnXYPlane(anchor_north);
east_len = north_len;
}
else
{
if (!anchor_east.IsTiny())
{
// North and east are equal or north is tiny.
anchor_north = PerpendicularVectorOnXYPlane(anchor_east);
north_len = east_len;
}
else
{
// Both are identity.
east_len = north_len = 1.0;
anchor_east = ON_3dVector::XAxis;
anchor_north = ON_3dVector::YAxis;
}
}
plane.zaxis = ON_CrossProduct(anchor_east, anchor_north);
}
plane.xaxis = PerpendicularVectorOnXYPlane(plane.zaxis);
plane.yaxis = ON_CrossProduct(plane.zaxis, plane.xaxis);
// Restores original vector length if were valid.
plane.xaxis *= east_len;
plane.yaxis *= north_len;
plane.zaxis *= east_len * north_len;
plane.origin = _earth_anchor_point->ModelPoint();
plane.UpdateEquation();
}
void ON_Sun::CImpl::UpdateAziAlt(void) const
{
if (_calc_dirty)
{
ON_SunEngine engine(_accuracy);
engine.SetLatitude (Latitude());
engine.SetLongitude(Longitude());
engine.SetTimeZoneHours(TimeZone());
const int dsm = DaylightSavingOn() ? DaylightSavingMinutes() : 0;
engine.SetDaylightSavingMinutes(dsm);
int y = 0, m = 0, d = 0; double h = 0.0;
LocalDateTime(y, m, d, h);
engine.SetLocalDateTime(y, m, d, h);
_azimuth = engine.Azimuth();
_altitude = engine.Altitude();
_calc_dirty = false;
}
}
bool ON_Sun::CImpl::EnableAllowed(void) const
{
return GetParameter(XMLPath_Sun(), ON_RDK_SUN_ENABLE_ALLOWED, false);
}
void ON_Sun::CImpl::SetEnableAllowed(bool allowed)
{
SetParameter(XMLPath_Sun(), ON_RDK_SUN_ENABLE_ALLOWED, allowed);
}
bool ON_Sun::CImpl::EnableOn(void) const
{
return GetParameter(XMLPath_Sun(), ON_RDK_SUN_ENABLE_ON, false);
}
void ON_Sun::CImpl::SetEnableOn(bool on)
{
SetParameter(XMLPath_Sun(), ON_RDK_SUN_ENABLE_ON, on);
}
bool ON_Sun::CImpl::ManualControlAllowed(void) const
{
return GetParameter(XMLPath_Sun(), ON_RDK_SUN_MANUAL_CONTROL_ALLOWED, false);
}
void ON_Sun::CImpl::SetManualControlAllowed(bool allowed)
{
SetParameter(XMLPath_Sun(), ON_RDK_SUN_MANUAL_CONTROL_ALLOWED, allowed);
}
bool ON_Sun::CImpl::ManualControlOn(void) const
{
return GetParameter(XMLPath_Sun(), ON_RDK_SUN_MANUAL_CONTROL_ON, false);
}
void ON_Sun::CImpl::SetManualControlOn(bool manual)
{
SetParameter(XMLPath_Sun(), ON_RDK_SUN_MANUAL_CONTROL_ON, manual);
}
double ON_Sun::CImpl::Azimuth(void) const
{
if (IsUsingManualControl())
return GetParameter(XMLPath_Sun(), ON_RDK_SUN_AZIMUTH, 0.0).AsDouble();
UpdateAziAlt();
return _azimuth;
}
void ON_Sun::CImpl::SetAzimuth(double azimuth)
{
SetParameter(XMLPath_Sun(), ON_RDK_SUN_AZIMUTH, azimuth);
}
double ON_Sun::CImpl::Altitude(void) const
{
if (IsUsingManualControl())
return GetParameter(XMLPath_Sun(), ON_RDK_SUN_ALTITUDE, 0.0).AsDouble();
UpdateAziAlt();
return _altitude;
}
void ON_Sun::CImpl::SetAltitude(double altitude)
{
SetParameter(XMLPath_Sun(), ON_RDK_SUN_ALTITUDE, altitude);
}
double ON_Sun::CImpl::TimeZone(void) const
{
return GetParameter(XMLPath_Sun(), ON_RDK_SUN_OBSERVER_TIMEZONE, 0.0);
}
void ON_Sun::CImpl::SetTimeZone(double hours)
{
SetParameter(XMLPath_Sun(), ON_RDK_SUN_OBSERVER_TIMEZONE, hours);
_calc_dirty = true;
}
bool ON_Sun::CImpl::DaylightSavingOn(void) const
{
return GetParameter(XMLPath_Sun(), ON_RDK_SUN_DAYLIGHT_SAVING_ON, false);
}
void ON_Sun::CImpl::SetDaylightSavingOn(bool on)
{
SetParameter(XMLPath_Sun(), ON_RDK_SUN_DAYLIGHT_SAVING_ON, on);
_calc_dirty = true;
}
int ON_Sun::CImpl::DaylightSavingMinutes(void) const
{
return GetParameter(XMLPath_Sun(), ON_RDK_SUN_DAYLIGHT_SAVING_MINUTES, 0);
}
void ON_Sun::CImpl::SetDaylightSavingMinutes(int minutes)
{
SetParameter(XMLPath_Sun(), ON_RDK_SUN_DAYLIGHT_SAVING_MINUTES, minutes);
_calc_dirty = true;
}
void ON_Sun::CImpl::LocalDateTime(int& year, int& month, int& day, double& hours) const
{
int dy = 0, dm = 0, dd = 0; double dh = 0.0;
ON_SunEngine::GetDefaultLocalDateTime(dy, dm, dd, dh);
const wchar_t* s = XMLPath_Sun();
year = GetParameter(s, ON_RDK_SUN_DATE_YEAR, dy);
month = GetParameter(s, ON_RDK_SUN_DATE_MONTH, dm);
day = GetParameter(s, ON_RDK_SUN_DATE_DAY, dd);
hours = GetParameter(s, ON_RDK_SUN_TIME_HOURS, dh);
}
bool ON_Sun::CImpl::SetLocalDateTime(int year, int month, int day, double hours)
{
year = std::max(MinYear(), std::min(MaxYear(), year));
month = std::max(1, std::min(12, month));
day = std::max(1, std::min(ON_SunEngine::DaysInMonth(month, year), day));
const wchar_t* s = XMLPath_Sun();
SetParameter(s, ON_RDK_SUN_DATE_YEAR, year);
SetParameter(s, ON_RDK_SUN_DATE_MONTH, month);
SetParameter(s, ON_RDK_SUN_DATE_DAY, day);
SetParameter(s, ON_RDK_SUN_TIME_HOURS, hours);
_calc_dirty = true;
return true;
}
double ON_Sun::CImpl::Intensity(void) const
{
return GetParameter(XMLPath_Sun(), ON_RDK_SUN_INTENSITY, 1.0);
}
void ON_Sun::CImpl::SetIntensity(double d)
{
SetParameter(XMLPath_Sun(), ON_RDK_SUN_INTENSITY, std::max(0.0, d));
}
double ON_Sun::CImpl::ShadowIntensity(void) const
{
// ShadowIntensity is currently unused. See [SHADOW_INTENSITY_UNUSED]
return GetParameter(XMLPath_Sun(), ON_RDK_SUN_SHADOW_INTENSITY, 1.0);
}
void ON_Sun::CImpl::SetShadowIntensity(double d)
{
// ShadowIntensity is currently unused. See [SHADOW_INTENSITY_UNUSED]
SetParameter(XMLPath_Sun(), ON_RDK_SUN_SHADOW_INTENSITY, std::max(0.0, std::min(1.0, d)));
}
double ON_Sun::CImpl::North(void) const
{
if (nullptr != _earth_anchor_point)
{
// Calculate north angle from earth anchor point.
ON_Plane plane;
ON_3dVector true_north;
GetEarthAnchorPlane(true_north, plane);
return ON_DegreesFromRadians(AngleFromVectors(plane.xaxis, true_north, plane.zaxis));
}
// No earth anchor point is set; just return the value in the XML.
return GetParameter(XMLPath_Sun(), ON_RDK_SUN_NORTH, 90.0).AsDouble();
}
void ON_Sun::CImpl::SetNorth(double north)
{
if (nullptr != _earth_anchor_point)
{
// Store the north in the earth anchor point. This is more complicated than just setting one value.
ON_Plane plane;
ON_3dVector true_north;
GetEarthAnchorPlane(true_north, plane);
// 29 January 2013 - Kike: Don't use ON_Plane::Rotate without origin.
// This function doesn't keep the vector length if the axis is the plane's z axis.
plane.Rotate(ON_RadiansFromDegrees(north - 90.0), plane.zaxis, plane.origin);
if (!IsVectorEqual(_earth_anchor_point->ModelEast(), plane.xaxis) ||
!IsVectorEqual(_earth_anchor_point->ModelNorth(), plane.yaxis))
{
_earth_anchor_point->SetModelEast (plane.xaxis);
_earth_anchor_point->SetModelNorth(plane.yaxis);
}
// Make sure this value never appears in the XML. This will clean old documents that were
// incorrectly saving this value in the XML as well as the earth anchor point.
RemoveParameter(XMLPath_Sun(), ON_RDK_SUN_NORTH);
}
else
{
// No earth anchor point is set; store the value in the XML.
SetParameter(XMLPath_Sun(), ON_RDK_SUN_NORTH, north);
}
_calc_dirty = true;
}
double ON_Sun::CImpl::Latitude(void) const
{
if (nullptr != _earth_anchor_point)
{
// Retrieve the latitude from the earth anchor point.
const double unset_latitude = 0.0;
return _earth_anchor_point->Latitude(unset_latitude);
}
// No earth anchor point is set; just return the value in the XML.
return GetParameter(XMLPath_Sun(), ON_RDK_SUN_OBSERVER_LATITUDE, 0.0).AsDouble();
}
void ON_Sun::CImpl::SetLatitude(double lat)
{
if (nullptr != _earth_anchor_point)
{
// Store the latitude in the earth anchor point.
_earth_anchor_point->SetLatitude(lat);
// Make sure this value never appears in the XML. This will clean old documents that were
// incorrectly saving this value in the XML as well as the earth anchor point.
RemoveParameter(XMLPath_Sun(), ON_RDK_SUN_OBSERVER_LATITUDE);
}
else
{
// No earth anchor point is set; store the value in the XML.
SetParameter(XMLPath_Sun(), ON_RDK_SUN_OBSERVER_LATITUDE, lat);
}
_calc_dirty = true;
}
double ON_Sun::CImpl::Longitude(void) const
{
if (nullptr != _earth_anchor_point)
{
// Retrieve the longitude from the earth anchor point.
const double unset_longitude = 0.0;
return _earth_anchor_point->Longitude(unset_longitude);
}
// No earth anchor point is set; just return the value in the XML.
return GetParameter(XMLPath_Sun(), ON_RDK_SUN_OBSERVER_LONGITUDE, 0.0).AsDouble();
}
void ON_Sun::CImpl::SetLongitude(double lon)
{
if (nullptr != _earth_anchor_point)
{
// Store the longitude in the earth anchor point.
_earth_anchor_point->SetLongitude(lon);
// Make sure this value never appears in the XML. This will clean old documents that were
// incorrectly saving this value in the XML as well as the earth anchor point.
RemoveParameter(XMLPath_Sun(), ON_RDK_SUN_OBSERVER_LONGITUDE);
}
else
{
// No earth anchor point is set; store the value in the XML.
SetParameter(XMLPath_Sun(), ON_RDK_SUN_OBSERVER_LONGITUDE, lon);
}
_calc_dirty = true;
}
ON_Sun::ON_Sun()
{
_impl = new CImpl;
}
ON_Sun::ON_Sun(ON_EarthAnchorPoint& eap)
{
_impl = new CImpl;
_impl->_earth_anchor_point = &eap;