forked from thargor6/mb3d
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCalcMissed.pas
831 lines (753 loc) · 29.9 KB
/
CalcMissed.pas
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
unit CalcMissed;
interface
uses
Classes, Math3D, TypeDefinitions;
type
TCalcMissed = class
private
{ Private-Deklarationen }
Iteration3D: TIteration3D;
VgradsFOV: TMatrix3;
procedure CalculateVgradsFOV(ix: Integer);
procedure CalculateNormals(DEstop: Double; var NN, R: Double; mPsiLight: TPsiLight5);
procedure maxLengthToCutPlane(var dLength: Double; var itmp: Integer; vPos: TPPos3D);
// procedure HSminLengthToCutPlane(var dLength: Double; vPos: TPPos3D);
procedure CalculateNormalsOnDE(DEstop: Double; var NN, R: Double; mPsiLight: TPsiLight5);
public
{ Public-Deklarationen }
MCTparas: TMCTparameter;
CMXpos, CMYpos: Integer;
procedure CalcMissed(n: Integer);
end;
var
cCalcMissed: TCalcMissed;
implementation
uses Mand, Math, LightAdjust, DivUtils, formulas, Forms, ImageProcess, CustomFormulas;
procedure TCalcMissed.maxLengthToCutPlane(var dLength: Double; var itmp: Integer; vPos: TPPos3D);
var dTmp: Double;
begin
with MCTparas do
begin
if iCutOptions > 0 then
begin
dLength := 0;
if ((iCutOptions and 1) > 0) and (Abs(VgradsFOV[2, 0]) > 1e-20) then
begin
dTmp := (dCOX - vPos^[0]) / VgradsFOV[2, 0];
if dTmp > dLength then
begin
dLength := dTmp;
itmp := 1;
end;
end;
if ((iCutOptions and 2) > 0) and (Abs(VgradsFOV[2, 1]) > 1e-20) then
begin
dTmp := (dCOY - vPos^[1]) / VgradsFOV[2, 1];
if dTmp > dLength then
begin
dLength := dTmp;
itmp := 2;
end;
end;
if ((iCutOptions and 4) > 0) and (Abs(VgradsFOV[2, 2]) > 1e-20) then
begin
dTmp := (dCOZ - vPos^[2]) / VgradsFOV[2, 2];
if dTmp > dLength then
begin
dLength := dTmp;
itmp := 3;
end;
end;
end;
end;
end;
{procedure TCalcMissed.HSminLengthToCutPlane(var dLength: Double; vPos: TPPos3D);
var dTmp: Double;
begin
with MCTparas do
begin
if iCutOptions > 0 then
begin
dLength := 1e40;
if ((iCutOptions and 1) > 0) and (Abs(HSvecs[0][0]) > 1e-20) then
begin
dTmp := (vPos^[0] - dCOX) / HSvecs[0][0];
if (dTmp > 0) and (dTmp < dLength) then dLength := dTmp;
end;
if ((iCutOptions and 2) > 0) and (Abs(HSvecs[0][1]) > 1e-20) then
begin
dTmp := (vPos^[1] - dCOY) / HSvecs[0][1];
if (dTmp > 0) and (dTmp < dLength) then dLength := dTmp;
end;
if ((iCutOptions and 4) > 0) and (Abs(HSvecs[0][2]) > 1e-20) then
begin
dTmp := (vPos^[2] - dCOZ) / HSvecs[0][2];
if (dTmp > 0) and (dTmp < dLength) then dLength := dTmp;
end;
end;
end;
end; }
procedure TCalcMissed.CalculateNormals(DEstop: Double; var NN, R: Double; mPsiLight: TPsiLight5);
var CT1: TVec3D;
dT2, StepSNorm, dM, dS, dSG, N3, N2, N1, NN2, NN1, Noffset: Double;
itmp, SmoothN: Integer;
V: TVec3D;
M{, MAFOV}: TMatrix3;
bN3neg: LongBool;
begin
with MCTparas do
begin
{ N1 := -CAFY; //antiFOV is not working, use absolute values + FOV on calcpixelcol, spec = Normals * (LightVec + ViewVecFOV)
N2 := -CAFX;
BuildRotMatrixFOV(N1, N2, @MAFOV);
Multiply2Matrix(@MAFOV, @Vgrads); }
Noffset := Min(DEstop * 0.5 / msDEStop, DEstop * 0.15);
Iteration3D.CalcSIT := True;
mCopyVec(@CT1, @Iteration3D.C1);
mMandFunction(@Iteration3D);
NN := Iteration3D.SmoothItD;
// Iteration3D.C3 := Iteration3D.C3 + StepWidth * 0.1; Absolute normals
mAddVecWeight(@Iteration3D.C1, @Vgrads[2, 0], -Noffset);
mMandFunction(@Iteration3D);
N3 := NN - Iteration3D.SmoothItD;// - NN; //Zgradient
// mCopyVec(@Iteration3D.C1, @CT1);
// Iteration3D.C1 := Iteration3D.C1 + StepWidth * 0.1;
mCopyAddVecWeight(@Iteration3D.C1, @CT1, @Vgrads[0, 0], Noffset);
mMandFunction(@Iteration3D);
N1 := NN - Iteration3D.SmoothItD; //Xgradient
// mCopyVec(@Iteration3D.C1, @CT1);
// Iteration3D.C2 := Iteration3D.C2 + StepWidth * 0.1;
mCopyAddVecWeight(@Iteration3D.C1, @CT1, @Vgrads[1, 0], Noffset);
mMandFunction(@Iteration3D);
N2 := NN - Iteration3D.SmoothItD; //Ygradient
if iSmNormals > 0 then //smoothed with estimation of roughness, eg deviation from mid val
begin // -> only 3 directions is not very good, more deviation if direct in DE direction to bulb
Iteration3D.maxIt := Iteration3D.ItResultI + 2;
Noffset := Noffset * 2;
mCopyAddVecWeight(@Iteration3D.C1, @CT1, @Vgrads[0, 0], -Noffset); //smooth mid point
mMandFunction(@Iteration3D);
NN := NN + Iteration3D.SmoothItD;
mAddVecWeight(@Iteration3D.C1, @Vgrads[0, 0], 2 * Noffset);
mMandFunction(@Iteration3D);
NN := NN + Iteration3D.SmoothItD;
mCopyAddVecWeight(@Iteration3D.C1, @CT1, @Vgrads[1, 0], -Noffset);
mMandFunction(@Iteration3D);
NN := NN + Iteration3D.SmoothItD;
mAddVecWeight(@Iteration3D.C1, @Vgrads[1, 0], 2 * Noffset);
mMandFunction(@Iteration3D);
NN := (NN + Iteration3D.SmoothItD) * 0.2;
SmoothN := iSmNormals;
StepSNorm := DEstop / ((SmoothN + 0.5) * msDEStop);
dM := SmoothN * 2;
bN3neg := N3 < 0;
if bN3neg then dS := -ArcTan2(N1, N3 - 1e-15) else dS := -ArcTan2(N1, N3 + 1e-15);
// dS := -ArcSin(N1 / Sqrt(N1 * N1 + N3 * N3 + 1e-15));
CopyVec(@V, @Vgrads[0]);
BuildRotMatrixY(dS, @M); //rotate dX on yaxis
RotateVector(@V, @M);
mCopyAddVecWeight(@Iteration3D.C1, @CT1, @V, -SmoothN * StepSNorm);
NN1 := 0;
dS := 0;
for itmp := -SmoothN to SmoothN do
begin
if itmp <> 0 then
begin
mMandFunction(@Iteration3D);
dT2 := (NN - Iteration3D.SmoothItD) / itmp;
NN1 := NN1 + dT2;
dS := dS + Sqr(dT2);
end;
mAddVecWeight(@Iteration3D.C1, @V, StepSNorm);
end;
dSG := dS * dM - Sqr(NN1);
if bN3neg then dS := ArcTan2(N2, N3 - 1e-15) else dS := ArcTan2(N2, N3 + 1e-15);
// dS := ArcSin(N2 / Sqrt(N2 * N2 + N3 * N3 + 1e-15));
CopyVec(@V, @Vgrads[1]);
BuildRotMatrixX(dS, @M);
RotateVector(@V, @M);
mCopyAddVecWeight(@Iteration3D.C1, @CT1, @V, -SmoothN * StepSNorm);
NN2 := 0;
dS := 0;
for itmp := -SmoothN to SmoothN do
begin
if itmp <> 0 then
begin
mMandFunction(@Iteration3D);
dT2 := (NN - Iteration3D.SmoothItD) / itmp;
NN2 := NN2 + dT2;
dS := dS + Sqr(dT2);
end;
mAddVecWeight(@Iteration3D.C1, @V, StepSNorm);
end;
dT2 := Noffset * 0.5 / (dM * StepSNorm);
dSG := (dSG + dS * dM - Sqr(NN2)) * 5 / Max(1e-10, Sqr(NN1) + Sqr(NN2) + Sqr(N3 / dT2));
sRoughness := Max(0, Min(1, Sqrt(dSG) - 0.05));
N1 := N1 + NN1 * dT2;
N2 := N2 + NN2 * dT2;
Iteration3D.maxIt := iMaxIt;
end;
R := 32767 / Sqrt(N1 * N1 + N2 * N2 + N3 * N3);
mPsiLight^.NormalX := Round(N1 * R); //3 normals a 16bit
mPsiLight^.NormalY := Round(N2 * R);
mPsiLight^.NormalZ := Round(N3 * R);
mCopyVec(@Iteration3D.C1, @CT1);
end;
end;
procedure TCalcMissed.CalculateNormalsOnDE(DEstop: Double; var NN, R: Double; mPsiLight: TPsiLight5);
var CT1: TVec3D;
dT2, StepSNorm, dM, dS, dSG, N3, N2, N1, NN2, NN1, Noffset: Double;
itmp, SmoothN: Integer;
V: TVec3D;
M: TMatrix3;
bN3neg: LongBool;
begin
with MCTparas do
begin
Noffset := Min(DEstop * 0.5 / msDEStop, DEstop * 0.15);
Iteration3D.CalcSIT := False;
mCopyVec(@CT1, @Iteration3D.C1);
NN := mMandFunctionDE(@Iteration3D);
mAddVecWeight(@Iteration3D.C1, @Vgrads[2, 0], -Noffset);
N3 := mMandFunctionDE(@Iteration3D) - NN; //Zgradient
mAddVecWeight(@Iteration3D.C1, @Vgrads[2, 0], 2 * Noffset); //Doublecheck because of steps
N1 := NN - mMandFunctionDE(@Iteration3D);
if Abs(N1) < Abs(N3) then N3 := N1;
mCopyAddVecWeight(@Iteration3D.C1, @CT1, @Vgrads[0, 0], Noffset);
N1 := mMandFunctionDE(@Iteration3D) - NN; //Xgradient
mAddVecWeight(@Iteration3D.C1, @Vgrads[0, 0], -2 * Noffset);
N2 := NN - mMandFunctionDE(@Iteration3D);
if Abs(N2) < Abs(N1) then N1 := N2;
mCopyAddVecWeight(@Iteration3D.C1, @CT1, @Vgrads[1, 0], Noffset);
N2 := mMandFunctionDE(@Iteration3D) - NN; //Ygradient
mAddVecWeight(@Iteration3D.C1, @Vgrads[1, 0], -2 * Noffset);
NN2 := NN - mMandFunctionDE(@Iteration3D);
if Abs(NN2) < Abs(N2) then N2 := NN2;
if iSmNormals > 0 then
begin
Iteration3D.maxIt := Iteration3D.ItResultI + 2;
Noffset := Noffset * 2;
mCopyAddVecWeight(@Iteration3D.C1, @CT1, @Vgrads[0, 0], -Noffset); //smooth mid point
NN := NN + mMandFunctionDE(@Iteration3D);
mAddVecWeight(@Iteration3D.C1, @Vgrads[0, 0], 2 * Noffset);
NN := NN + mMandFunctionDE(@Iteration3D);
mCopyAddVecWeight(@Iteration3D.C1, @CT1, @Vgrads[1, 0], -Noffset);
NN := NN + mMandFunctionDE(@Iteration3D);
mAddVecWeight(@Iteration3D.C1, @Vgrads[1, 0], 2 * Noffset);
NN := (NN + mMandFunctionDE(@Iteration3D)) * 0.2;
SmoothN := iSmNormals;
StepSNorm := DEstop / ((SmoothN + 0.5) * msDEStop);
dM := SmoothN * 2;
bN3neg := N3 < 0;
if bN3neg then dS := -ArcTan2(N1, N3 - 1e-15) else dS := -ArcTan2(N1, N3 + 1e-15);
// dS := -ArcSin(N1 / Sqrt(N1 * N1 + N3 * N3 + 1e-15));
CopyVec(@V, @Vgrads[0]);
BuildRotMatrixY(dS, @M); //rotate dX only on yaxis
RotateVector(@V, @M);
// dT := StepSNorm / (0.1 + Cos(dS - CAFX));
// mCopyAddVecWeight(@Iteration3D.C1, @CT1, @V, -SmoothN * dT);
mCopyAddVecWeight(@Iteration3D.C1, @CT1, @V, -SmoothN * StepSNorm);
NN1 := 0;
dS := 0;
for itmp := -SmoothN to SmoothN do
begin
if itmp <> 0 then
begin
dT2 := (mMandFunctionDE(@Iteration3D) - NN) / itmp;
NN1 := NN1 + dT2;
dS := dS + Sqr(dT2);
end;
mAddVecWeight(@Iteration3D.C1, @V, StepSNorm);
end;
dSG := dS * dM - Sqr(NN1);
if bN3neg then dS := ArcTan2(N2, N3 - 1e-15) else dS := ArcTan2(N2, N3 + 1e-15);
// dS := ArcSin(N2 / Sqrt(N2 * N2 + N3 * N3 + 1e-15));
CopyVec(@V, @Vgrads[1]);
BuildRotMatrixX(dS, @M); //rotate dY only on xaxis
RotateVector(@V, @M);
// dT := StepSNorm / (0.1 + Cos(dS - CAFY));
// mCopyAddVecWeight(@Iteration3D.C1, @CT1, @V, -SmoothN * dT);
mCopyAddVecWeight(@Iteration3D.C1, @CT1, @V, -SmoothN * StepSNorm);
NN2 := 0;
dS := 0;
for itmp := -SmoothN to SmoothN do
begin
if itmp <> 0 then
begin
dT2 := (mMandFunctionDE(@Iteration3D) - NN) / itmp;
NN2 := NN2 + dT2;
dS := dS + Sqr(dT2);
end;
mAddVecWeight(@Iteration3D.C1, @V, StepSNorm);
end;
dT2 := 0.5 * Noffset / (dM * StepSNorm);
dSG := (dSG + dS * dM - Sqr(NN2)) * 5 / Max(1e-10, Sqr(NN1) + Sqr(NN2) + Sqr(N3 / dT2));
sRoughness := Max(0, Min(1, Sqrt(dSG) - 0.05));
N1 := N1 + NN1 * dT2; //not accurate, only for small deltas (|NN1| << |N1|)
N2 := N2 + NN2 * dT2;
Iteration3D.maxIt := iMaxIt;
end;
R := 32767 / Sqrt(N1 * N1 + N2 * N2 + N3 * N3);
mPsiLight^.NormalX := Round(N1 * R); //3 normals a 16bit
mPsiLight^.NormalY := Round(N2 * R);
mPsiLight^.NormalZ := Round(N3 * R);
mCopyVec(@Iteration3D.C1, @CT1);
Iteration3D.CalcSIT := True;
mMandFunctionDE(@Iteration3D);
NN := Iteration3D.SmoothItD; //for coloring
Iteration3D.CalcSIT := False;
end;
end;
{
procedure TCalcMissed.CalculateNormals(DEstop: Double; var NN, R: Double; mPsiLight: TPsiLight5);
var CT1: TVec3D;
dT2, StepSNorm, dM, dS, dSG, N3, N2, N1, NN2, NN1: Double;
itmp, SmoothN: Integer;
M: TMatrix3;
V: TVec3D;
begin
with MCTparas do
begin
Iteration3D.CalcSIT := True; // Calculate Normals relativ to FOV direction
mCopyVec(@CT1, @Iteration3D.C1);
mMandFunction(@Iteration3D);
NN := Iteration3D.SmoothItD;
mAddVecWeight(@Iteration3D.C1, @Vgrads[2, 0], -0.1);
mMandFunction(@Iteration3D);
N3 := NN - Iteration3D.SmoothItD; //Zgradient
mCopyAddVecWeight(@Iteration3D.C1, @CT1, @Vgrads[0, 0], 0.1);
mMandFunction(@Iteration3D);
N1 := NN - Iteration3D.SmoothItD; //Xgradient
mCopyAddVecWeight(@Iteration3D.C1, @CT1, @Vgrads[1, 0], 0.1);
mMandFunction(@Iteration3D);
N2 := NN - Iteration3D.SmoothItD; //Ygradient
if iSmNormals > 0 then //smoothed with estimation of roughness, eg deviation from mid val
begin // -> only 3 directions is not very good, more deviation if direct in DE direction to bulb
Iteration3D.maxIt := Iteration3D.ItResultI + 2;
mCopyAddVecWeight(@Iteration3D.C1, @CT1, @Vgrads[0, 0], -0.2); //smooth mid point
mMandFunction(@Iteration3D);
NN := NN + Iteration3D.SmoothItD;
mAddVecWeight(@Iteration3D.C1, @Vgrads[0, 0], 0.4);
mMandFunction(@Iteration3D);
NN := NN + Iteration3D.SmoothItD;
mCopyAddVecWeight(@Iteration3D.C1, @CT1, @Vgrads[1, 0], -0.2);
mMandFunction(@Iteration3D);
NN := NN + Iteration3D.SmoothItD;
mAddVecWeight(@Iteration3D.C1, @Vgrads[1, 0], 0.4);
mMandFunction(@Iteration3D);
NN := (NN + Iteration3D.SmoothItD) * 0.2;
SmoothN := iSmNormals;
StepSNorm := DEstop / ((SmoothN + 0.5) * sDEStop);
dM := SmoothN * 2;
R := 1 / Sqrt(N1 * N1 + N2 * N2 + N3 * N3);
dS := -ArcSin(N1 * R);
CopyVec(@V, @Vgrads[0]);
BuildRotMatrixY(dS, @M); //rotate dX only on yaxis
RotateVector(@V, @M);
mCopyAddVecWeight(@Iteration3D.C1, @CT1, @V, -SmoothN * StepSNorm);
NN1 := 0;
dS := 0;
for itmp := -SmoothN to SmoothN do
begin
if itmp <> 0 then
begin
mMandFunction(@Iteration3D);
dT2 := (NN - Iteration3D.SmoothItD) / itmp;
NN1 := NN1 + dT2;
dS := dS + Sqr(dT2);
end;
mAddVecWeight(@Iteration3D.C1, @V, StepSNorm);
end;
dSG := dS * dM - Sqr(NN1);
dS := ArcSin(N2 * R);
CopyVec(@V, @Vgrads[1]);
BuildRotMatrixX(dS, @M);
RotateVector(@V, @M);
mCopyAddVecWeight(@Iteration3D.C1, @CT1, @V, -SmoothN * StepSNorm);
NN2 := 0;
dS := 0;
for itmp := -SmoothN to SmoothN do
begin
if itmp <> 0 then
begin
mMandFunction(@Iteration3D);
dT2 := (NN - Iteration3D.SmoothItD) / itmp;
NN2 := NN2 + dT2;
dS := dS + Sqr(dT2);
end;
mAddVecWeight(@Iteration3D.C1, @V, StepSNorm);
end;
dT2 := 0.1 / (dM * StepSNorm); //StepSNorm := DEstop / (SmoothN + 0.5);
dSG := (dSG + dS * dM - Sqr(NN2)) * 3 / Max(1e-10, Sqr(NN1) + Sqr(NN2) + Sqr(N3 / dT2));
sRoughness := Max(0, Min(1, Sqrt(dSG) - 0.13));
N1 := N1 + NN1 * dT2;
N2 := N2 + NN2 * dT2;
Iteration3D.maxIt := iMaxIt;
end;
R := 32767 / Sqrt(N1 * N1 + N2 * N2 + N3 * N3); //recip. length of 3Dgradient
mPsiLight^.NormalX := Round(N1 * R); //3 normals a 16bit
mPsiLight^.NormalY := Round(N2 * R);
mPsiLight^.NormalZ := Round(N3 * R);
mCopyVec(@Iteration3D.C1, @CT1);
end;
end;
procedure TCalcMissed.CalculateNormalsOnDE(DEstop: Double; var NN, R: Double; mPsiLight: TPsiLight5);
var CT1: TVec3D;
dT2, StepSNorm, dM, dS, dSG, N3, N2, N1, NN2, NN1: Double;
itmp, SmoothN: Integer;
M: TMatrix3;
V: TVec3D;
begin
with MCTparas do
begin
Iteration3D.CalcSIT := False;
mCopyVec(@CT1, @Iteration3D.C1);
NN := mMandFunctionDE(@Iteration3D);
mAddVecWeight(@Iteration3D.C1, @Vgrads[2, 0], -0.1);
N3 := mMandFunctionDE(@Iteration3D) - NN; //Zgradient
mAddVecWeight(@Iteration3D.C1, @Vgrads[2, 0], 0.2); //Doublecheck because of steps
N1 := NN - mMandFunctionDE(@Iteration3D);
if Abs(N1) < Abs(N3) then N3 := N1;
mCopyAddVecWeight(@Iteration3D.C1, @CT1, @Vgrads[0, 0], 0.1);
N1 := mMandFunctionDE(@Iteration3D) - NN; //Xgradient
mAddVecWeight(@Iteration3D.C1, @Vgrads[0, 0], -0.2);
N2 := NN - mMandFunctionDE(@Iteration3D);
if Abs(N2) < Abs(N1) then N1 := N2;
mCopyAddVecWeight(@Iteration3D.C1, @CT1, @Vgrads[1, 0], 0.1);
N2 := mMandFunctionDE(@Iteration3D) - NN; //Ygradient
mAddVecWeight(@Iteration3D.C1, @Vgrads[1, 0], -0.2);
NN2 := NN - mMandFunctionDE(@Iteration3D);
if Abs(NN2) < Abs(N2) then N2 := NN2;
if iSmNormals > 0 then
begin
Iteration3D.maxIt := Iteration3D.ItResultI + 2;
mCopyAddVecWeight(@Iteration3D.C1, @CT1, @Vgrads[0, 0], -0.2); //smooth mid point
NN := NN + mMandFunctionDE(@Iteration3D);
mAddVecWeight(@Iteration3D.C1, @Vgrads[0, 0], 0.4);
NN := NN + mMandFunctionDE(@Iteration3D);
mCopyAddVecWeight(@Iteration3D.C1, @CT1, @Vgrads[1, 0], -0.2);
NN := NN + mMandFunctionDE(@Iteration3D);
mAddVecWeight(@Iteration3D.C1, @Vgrads[1, 0], 0.4);
NN := (NN + mMandFunctionDE(@Iteration3D)) * 0.2;
SmoothN := iSmNormals;
StepSNorm := DEstop / ((SmoothN + 0.5) * sDEStop);
dM := SmoothN * 2;
R := 1 / Sqrt(N1 * N1 + N2 * N2 + N3 * N3);
dS := -ArcSin(N1 * R);
CopyVec(@V, @Vgrads[0]);
BuildRotMatrixY(dS, @M); //rotate dX only on yaxis
RotateVector(@V, @M);
mCopyAddVecWeight(@Iteration3D.C1, @CT1, @V, -SmoothN * StepSNorm);
NN1 := 0;
dS := 0;
for itmp := -SmoothN to SmoothN do
begin
if itmp <> 0 then
begin
dT2 := (mMandFunctionDE(@Iteration3D) - NN) / itmp;
NN1 := NN1 + dT2;
dS := dS + Sqr(dT2);
end;
mAddVecWeight(@Iteration3D.C1, @V, StepSNorm);
end;
dSG := dS * dM - Sqr(NN1);
dS := ArcSin(N2 * R);
CopyVec(@V, @Vgrads[1]);
BuildRotMatrixX(dS, @M); //rotate dY only on xaxis
RotateVector(@V, @M);
mCopyAddVecWeight(@Iteration3D.C1, @CT1, @V, -SmoothN * StepSNorm);
NN2 := 0;
dS := 0;
for itmp := -SmoothN to SmoothN do
begin
if itmp <> 0 then
begin
dT2 := (mMandFunctionDE(@Iteration3D) - NN) / itmp;
NN2 := NN2 + dT2;
dS := dS + Sqr(dT2);
end;
mAddVecWeight(@Iteration3D.C1, @V, StepSNorm);
end;
dT2 := 0.1 / (dM * StepSNorm);
dSG := (dSG + dS * dM - Sqr(NN2)) * 3 / Max(1e-10, Sqr(NN1) + Sqr(NN2) + Sqr(N3 / dT2));
sRoughness := Max(0, Min(1, Sqrt(dSG) - 0.13));
N1 := N1 + NN1 * dT2; //not accurate, only for small deltas (|NN1| << |N1|)
N2 := N2 + NN2 * dT2;
Iteration3D.maxIt := iMaxIt;
end;
R := 32767 / Sqrt(N1 * N1 + N2 * N2 + N3 * N3); //recip. length of 3Dgradient
mPsiLight^.NormalX := Round(N1 * R); //3 normals a 16bit
mPsiLight^.NormalY := Round(N2 * R);
mPsiLight^.NormalZ := Round(N3 * R);
mCopyVec(@Iteration3D.C1, @CT1);
Iteration3D.CalcSIT := True;
mMandFunctionDE(@Iteration3D);
NN := Iteration3D.SmoothItD; //for coloring
Iteration3D.CalcSIT := False;
end;
end; }
procedure TCalcMissed.CalculateVgradsFOV(ix: Integer);
begin
with MCTparas do
begin
CAFX := (0.5 * iMandWidth - ix) * FOVy / iMandHeight;
BuildRotMatrixFOV(CAFY, CAFX, @VgradsFOV);
Multiply2Matrix(@VgradsFOV, @Vgrads);
end;
end;
procedure TCalcMissed.CalcMissed(n: Integer);
var itmp, itmp2, itmp3, itmp4, itmp5, itmp6: Integer;
x, y, DEstepC: Integer;
MandFunction: TMandFunction;
MandFunctionDE: TMandFunctionDE;
// SL: PCardinal;
mPsiLight, mPsiLight2: TPsiLight5;
bIsCustomDE: LongBool;
dTmp, dT1, zh, ZZ, YP: Double;
CC1, CC2, CC3, R: Double;
DEstop, DEstopFactor, minDE, ZstepDiv12d, DEstopHL: Double;
DEscale, DEoffset{, dISW}: Double;
// PLV: TPaintLightVals;
function calcDE: Double; //Distance Estimation (DE)
var Rtmp, RD1, Rst: Double;
deCT1, deCT2, deCT3: Double;
Mtmp: Integer;
RStopTmp: Single;
begin
if bIsCustomDE then
begin
Iteration3D.CalcSIT := False;
Result := MandFunctionDE(@Iteration3D) * DEscale;
end
else with Iteration3D do
begin
CalcSIT := False; //3d gradient
MandFunction(@Iteration3D);
Mtmp := MaxIt;
MaxIt := ItResultI;
RStopTmp := RStop;
RStop := RStop * 8;
Rtmp := Rout;
Rst := DEoffset;
deCT1 := C1;
deCT2 := C2;
deCT3 := C3;
C1 := C1 + VgradsFOV[2, 0] * Rst;
C2 := C2 + VgradsFOV[2, 1] * Rst;
C3 := C3 + VgradsFOV[2, 2] * Rst;
MandFunction(@Iteration3D);
RD1 := Sqr(Rtmp - Rout);
C1 := deCT1 + VgradsFOV[1, 0] * Rst;
C2 := deCT2 + VgradsFOV[1, 1] * Rst;
C3 := deCT3 + VgradsFOV[1, 2] * Rst;
MandFunction(@Iteration3D);
RD1 := RD1 + Sqr(Rout - Rtmp);
C1 := deCT1 + VgradsFOV[0, 0] * Rst;
C2 := deCT2 + VgradsFOV[0, 1] * Rst;
C3 := deCT3 + VgradsFOV[0, 2] * Rst;
MandFunction(@Iteration3D);
RD1 := Sqrt(RD1 + Sqr(Rout - Rtmp)) + (Rst * 0.06);
Result := Rtmp * Ln(Rtmp) * DEscale / RD1;
MaxIt := Mtmp;
RStop := RStopTmp;
C1 := deCT1;
C2 := deCT2;
C3 := deCT3;
end;
if Result < minDE then Result := minDE;
end;
begin
with MCTparas do
try
Move(dJUx, Iteration3D.J1, 168);
MandFunction := mMandFunction;
MandFunctionDE := mMandFunctionDE;
zh := StepWidth;
bIsCustomDE := IsCustomDE;
ZstepDiv12d := sZstepDiv; //iZstepDiv=2..16 DEaccuracy
DEstopFactor := (1 + 0.3 / msDEStop) * Max(0, FOVy) / iMandHeight;
DEoffset := Min(msDEStop * 0.1, 0.01);
DEscale := dDEscale;
// dISW := 1 + iZstepDiv * 0.1;
y := Max(1, CMYpos - 2);
YXstart := YXstart + Vgrads[1, 0] * y;
YYstart := YYstart + Vgrads[1, 1] * y;
YZstart := YZstart + Vgrads[1, 2] * y;
while y < Min(CMYpos + 3, iMandHeight - 1) do
begin
x := Max(1, CMXpos - 2);
CC1 := YXstart + Vgrads[0, 0] * x;
CC2 := YYstart + Vgrads[0, 1] * x;
CC3 := YZstart + Vgrads[0, 2] * x;
mPsiLight := pSiLight;
Inc(mPsiLight, y * iMandWidth + x);
// SL := PCardinal(FSIstart + FSIoffset * y + 4 * x);
CAFY := (y - 0.5 * iMandHeight) * FOVy / iMandHeight;
while x < Min(CMXpos + 3, iMandWidth - 1) do
with Iteration3D do
begin
CalculateVgradsFOV(x + 1);
ZZ := Zstart;
YP := Zstart;
C1 := CC1;
C2 := CC2;
C3 := CC3;
CalcSIT := False;
itmp := 0;
DEstop := msDEStop;
minDE := DEstop * 0.5;
if iCutOptions > 0 then
begin
maxLengthToCutPlane(YP, itmp, @C1);
YP := ZZ + YP;
end;
//proof, if zpos of neighbourpixels are greater
itmp3 := 0;
for itmp := -1 to 1 do
begin
mPsiLight2 := mPsiLight;
Inc(mPsiLight2, itmp * iMandWidth - 1);
for itmp2 := -1 to 1 do
begin
if (mPsiLight2^.Zpos < 32768) and
(mPsiLight2^.Zpos > itmp3) and (random > 0.2) then itmp3 := mPsiLight2^.Zpos;
Inc(mPsiLight2); // to give lower Z's a chance
end;
end;
//zz to proof:
dT1 := ZZ + (Sqr((32767 - itmp3) * 256 / ZCmul + 1) - 1) / Zcorr;
if ((itmp3 > mPsiLight^.Zpos + 5) or (mPsiLight^.Zpos = 32768)) and (dT1 > YP) then
begin
//move to zpos:
mAddVecWeight(@C1, @VgradsFOV[2, 0], dT1 - ZZ);
ZZ := dT1;
itmp := 30; //bin search
dT1 := Random + 0.5;
dTmp := 0;
dTmp := calcDE;
R := dTmp; //LastDE
repeat
ZZ := ZZ + dT1;
mAddVecWeight(@C1, @VgradsFOV[2, 0], dT1);
if bVaryDEstop then
begin
DEstop := msDEStop * (1 + (ZZ - Zstart) * DEstopFactor);
minDE := DEstop * 0.5;
end;
dTmp := calcDE;
if dTmp < DEstop then dT1 := Abs(dT1) * -0.7 else
if dTmp < R then dT1 := dT1 * 0.8 else dT1 := -0.8 * dT1;
R := dTmp;
Dec(itmp);
until
(itmp < 1) or (Abs(dTmp - DEstop) < 0.001);
//only if DElimited
if ((dTmp - DEstop) < 0.01) and (ZZ > Zstart) and (Iteration3D.ItResultI < MaxIt) then
begin
DEstopHL := DEstop;
if bIsCustomDE then
CalculateNormalsOnDE(DEstop, dTmp, dT1, mPsiLight)
else
CalculateNormals(DEstop, dTmp, dT1, mPsiLight);
// if colItCou then // color on iteration count, fixed grad
// begin
dTmp := 32767 - (dTmp + dColPlus - iMinIt) *
32767 / (maxIt - iMinIt + 1);
// end
// else dTmp := 3e7 / (dT1 * maxIt * maxIt); // color on gradient
if dTmp > 32766.5 then mPsiLight^.SIgradient := 32767 else
if dTmp < 0 then mPsiLight^.SIgradient := 0 else
mPsiLight^.SIgradient := Round(dTmp);
dTmp := Iteration3D.OTrap * 4096;
if dTmp > 32766.5 then mPsiLight^.OTrap := 32767
else mPsiLight^.OTrap := Round(dTmp);
dTmp := 8388352 - ZcMul * (Sqrt((ZZ - Zstart) * Zcorr + 1) - 1);
if dTmp < 0 then iTmp := 0 else itmp := Round(dTmp);
mPsiLight^.Zpos := iTmp shr 8;
mPsiLight^.RoughZposFine := (iTmp and $FF) shl 8;
{ if calcHardShadow then
begin
dT1 := iMandHeight;
if iCutOptions > 0 then
begin
HSminLengthToCutPlane(dT1, @C1);
if dT1 > iMandHeight then dT1 := iMandHeight;
end;
YP := dT1;
dTmp := 0.5 / ZstepDiv12d;
mAddVecWeight(@C1, @VgradsFOV[2, 0], -0.3); // step 0.3 pixel(Z) forward
if YP > 0 then
repeat
dTmp := dTmp * ZstepDiv12d;
YP := YP - dTmp;
mAddVecWeight(@C1, @HSvecs[0], -dTmp);
dTmp := calcDE;
until
((ItResultI < iMinIt) and (ItResultI >= MaxIt) or
(dTmp < DEStopHL)) or (YP <= 0);
if YP <= 0 then mPsiLight^.Shadow := 32767 else
begin
itmp := Round(32767 * (dT1 - YP) / iMandHeight);
if itmp > 32767 then itmp := 32767;
mPsiLight^.Shadow := itmp;
end;
end else begin }
itmp4 := 100000;
itmp3 := mPsiLight^.Shadow;
itmp5 := mPsiLight^.AmbShadow;
itmp6 := mPsiLight^.RoughZposFine;
for itmp := -1 to 1 do
begin
mPsiLight2 := mPsiLight;
Inc(mPsiLight2, itmp * iMandWidth - 1);
for itmp2 := -1 to 1 do
begin
if (mPsiLight2^.Zpos < 32768) and (Abs(itmp) + Abs(itmp2) > 0) then
begin
if Abs(mPsiLight2^.Zpos - mPsiLight^.Zpos) < itmp4 then
begin
itmp4 := Abs(mPsiLight2^.Zpos - mPsiLight^.Zpos);
itmp3 := mPsiLight2^.Shadow;
itmp5 := mPsiLight2^.AmbShadow;
itmp6 := mPsiLight2^.RoughZposFine;
end;
end;
Inc(mPsiLight2);
end;
end;
mPsiLight^.Shadow := itmp3;
mPsiLight^.AmbShadow := itmp5;
mPsiLight^.RoughZposFine := itmp6;
// end;
if iSmNormals > 0 then
mPsiLight^.RoughZposFine := mPsiLight^.RoughZposFine or Round(sRoughness * 255);
{ PLV.xPos := x / iMandWidth;
PLV.yPos := y / iMandHeight;
dTmp := 1 / StepWidth;
PLV.ViewVec[0] := VGradsFOV[2,0] * dTmp;
PLV.ViewVec[1] := VGradsFOV[2,1] * dTmp;
PLV.ViewVec[2] := VGradsFOV[2,2] * dTmp; }
// BuildViewVectorFOV(CAFY, CAFX, @PLV.ViewVec); //to be changed, paint afterwards
// CalcPixelcolor(SL, mPsiLight, PLVals, @PLV);
end;
end;
CC1 := CC1 + Vgrads[0, 0];
CC2 := CC2 + Vgrads[0, 1];
CC3 := CC3 + Vgrads[0, 2];
Inc(x);
// Inc(SL);
Inc(mPsiLight);
end;
YXstart := YXstart + Vgrads[1, 0];
YYstart := YYstart + Vgrads[1, 1];
YZstart := YZstart + Vgrads[1, 2];
Inc(y);
UpdateScaledImage(y div ImageScale, y div ImageScale);
end;
finally
end;
end;
initialization
cCalcMissed := TCalcMissed.Create;
finalization
cCalcMissed.Free;
end.