-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paths.c
1571 lines (1009 loc) · 36.7 KB
/
s.c
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
/*
Adam Majewski
c console progam
trace curve in the binary image ( array )
curves can be :
* radial = external ray ( segments ) from infinity to the boubdary of Mandelbrot set
* ( potentialy closed ) = eqipotentials, which tend to the boundary of Mandelbrot set
gcc s.c -lm -Wall
gcc optimisation:
gcc s.c -lm -Wall -march=native -O2
OpenMP
gcc s.c -lm -Wall -march=native -O2 -fopenmp
- without : l 3m16,391s
- with : 0m30,239s
!!!!
./a.out
time ./a.out
time ./a.out>a.txt
============ git ==========
cd existing_folder
git init
git remote add origin git@gitlab.com:adammajewski/curve-tracing.git
git add .
git commit -m "Initial commit"
git push -u origin master
=============================
convert 10_100001.pgm -resize 600x600 10_100001.png
// tests
good :
draw equipotential curve thru point c = (0.5000000000000000; 0.0000000000000000)
Start point ix = 1694 iy = 1000 i = 2001694 potential = 0.2130028091546024
end point ix = 1694 iy = 1000 i = 2003694 potential = 0.2129910754510925
curve is closed = stop ( good) after 3659 steps (pixels)
bad :
draw equipotential curve thru point c = (0.3500000000000000; 0.0000000000000000)
Start point ix = 1610 iy = 1000 i = 2001610 potential = 0.0183852927674834
problem: FCCode not found
drawing stopped because of bad FCCode = -1 after 8806 steps ( pixels)
File 1000_1000.pgm saved.
Parameter plane with Mandelbrot set
exterior = CPM/M
IterationMax = 1000
EscapeRadius = 1000
iPixelRadius = ixMax* 0.002 = 1 so big pixel = 4 (small) pixels
*/
#include <stdio.h>
#include <stdlib.h> // malloc
#include <math.h> // M_PI; needs -lm also
#include <complex.h>
#include <string.h> // strncat
#include <omp.h> //OpenM
// ------------ global variables, see also give_t ==================================================
double TwoPi = 2.0*M_PI;
const int IterationMax=70000;
/* bail-out value for the bailout test for exaping points
radius of circle centered ad the origin, exterior of such circle is a target set */
const double EscapeRadius=101; // it should be >> 2 because then "equipotentials" are crossing at c= -2
double ER2; // = EscapeRadius*EscapeRadius;
double K = log(2.0);
// virtual 2D array and integer ( screen) coordinate
// Indexes of array starts from 0 not 1
//unsigned int ix, iy; // var
unsigned int ixMin = 0; // Indexes of array starts from 0 not 1
unsigned int ixMax ; //
unsigned int iWidth = 2100; // horizontal dimension of array
unsigned int iyMin = 0; // Indexes of array starts from 0 not 1
unsigned int iyMax ; //
unsigned int iHeight = 2100; // odd number !!!!!! = (iyMax -iyMin + 1) = iyAboveAxisLength + iyBelowAxisLength +1
// The size of array has to be a positive constant integer
unsigned int iSize ; // = iWidth*iHeight;
int iPixelRadius; // =ixMax* 0.002; /* half of width or height of big pixel */
double AspectRatio; // width : height https://en.wikipedia.org/wiki/Aspect_ratio_(image)
/* world ( double) coordinate = parameter plane */
// center and radius : https://en.wikibooks.org/wiki/Fractals/Computer_graphic_techniques/2D/plane#radius
//
complex double center = -0.75 +0.0*I; //
double radius = 1.8;
// corners: https://en.wikibooks.org/wiki/Fractals/Computer_graphic_techniques/2D/plane#Corners
// parameter plane
double CxMin ;
double CxMax;
double CyMin;
double CyMax;
double PixelWidth; // =(CxMax-CxMin)/iXmax;
double PixelHeight; // =(CyMax-CyMin)/iYmax;
// memmory 1D arrays
unsigned char *AColor;
double *APotential;
int *ABoundary;
double NoiseMeasureThreshold = 0.045; // arbitrary for c = 0.365000000000000 +0.000000000000000 i period = 0
double BoundaryMeasure = 1.15; // higher value = thinner boundary
/*
directional features(Chain Codes)
8 directional chain code
the Freeman Chain Code of Eight Directions[1] (FCCE)
https://en.wikipedia.org/wiki/Chain_code
http://www.cis.hut.fi/research/IA/paper/publications/bmvc97/node2.html
3 2 1
4 c 0
5 6 7
where c = center
check all pixels of the 8-point neighborhood on a 2D grid
find most similar value to p0
it is the direction
----
but there should be 2 directions for equipotential curve ?????
why it gives the only one :
it exludes previous point : if (i>0 && ABoundary[i]==0){ // inside bounds of 1D array and not visited
2D lattice ( array) and coorinate
+----------+----------+----------+
| | | |
|(x-1,y+1) | (x,y+1) |(x+1,y+1) |
+----------+----------+----------+
|(x-1,y) | (x,y) |(x+1,y) |
| | | |
+----------+----------+----------+
| | (x,y-1) |(x+1,y-1) |
|(x-1,y-1) | | |
+----------+----------+----------
only (dx,dy) = offset
+----------+----------+----------+
| | | |
|(-1, 1) | (0, 1) |(1, 1) |
+----------+----------+----------+
|(-1, 0) | (0, 0) |(1, 0) |
| | | |
+----------+----------+----------+
| | (0,-1) |(1,-1) |
|(-1,-1) | | |
+----------+----------+----------
so coordinate (FCCE) = (x + dx, y+dy)
dx = dir[FCCode][0]
dy = dir[FCCode][1]
the Moore 8 neighborhood
comprises eight cells which surround center C
+----------+----------+----------+
| | | |
| NW | N | NE |
+----------+----------+----------+
| W | C | E |
| | | |
+----------+----------+----------+
| | S | SE |
| SW | | |
+----------+----------+----------
the Freeman Chain Code of Eight directions (FCCE) in counterclockwise direction
+----------+----------+----------+
| | | |
| 3 | 2 | 1 |
+----------+----------+----------+
| 4 | C | 0 |
| | | |
+----------+----------+----------+
| | 6 | 7 |
| 5 | | |
+----------+----------+----------
but note that y axis is inverted in give_c procedure !!!!!
offset[FCCode][0] = dx offset[FCCode][1] = dy
starts from angle 0 ( = E) ang goes counterclockwise
*/
int offset[8][2] = {//
{ 1, 0}, // 0 = East = right center
{ 1, 1}, // 1 = NE = right upper
{ 0, 1}, // 2 = center upper
{-1, 1}, // 3 = left upper
{-1, 0}, // 4 = left center
{-1,-1}, // 5 = lower left
{ 0,-1}, // 6 = center lower
{+1,-1}};// 7 = lower right
// ---------------------- functions ===========================================
double cnorm(double complex z)
{
return creal(z) * creal(z) + cimag(z) * cimag(z);
}
// arrray functions ===============================================
/*
gives position of 2D point (iX,iY) in 1D array ;
uses also global variables: ixMax, iyMax, iWidth
check bounds
*/
int give_i(int ix, int iy)
{
int i;
// check input
if (ix < 0 || ix > ixMax) {printf("\terror from giv_i : bad input: ix = %d \n", ix); return -1;}
if (iy < 0 || iy > iyMax) {printf("\terror from giv_i : bad input: iy = %d \n", iy); return -2;}
// compute return value
i= ix + iy*iWidth;
// second check ? not needed ?
//if (i>iSize || i<0) {printf("error ix = %d iy = %d i = %d\n", ix, iy, i); return -1;}
return i; }
int iPlotBigPoint(int ixSeed, int iySeed, unsigned char iColor, unsigned char A[]){
int i; /* index of memory dynamic 1D array */
int ix, iy;
int ixLMin = ixSeed-iPixelRadius;
int ixLMax = ixSeed+iPixelRadius;
int iyLMin = iySeed-iPixelRadius;
int iyLMax = iySeed+iPixelRadius;
/* draw big pixel */
for(iy= iyLMin; iy<= iyLMax; ++iy)
for(ix= ixLMin; ix<= ixLMax; ++ix)
// do not draw corners = "circle"
if (!(
(ix == ixLMin && iy == iyLMax) ||
(ix == ixLMin && iy == iyLMin) ||
(ix == ixLMax && iy == iyLMax) ||
(ix == ixLMax && iy == iyLMin)
))
{
i= give_i(ix,iy); /* index of _data array */
//printf(" ix = %d iy = %d i = %d\n", ix, iy, i);
// && i<=iSize
if (i > 0 )A[i]=iColor ; // A[i]=255 -A[i] ;//
}
//printf(" iPlotBigPoint %d \n", A[i]);
//printf(" ix = %d iy = %d i = %d\n", ix, iy, i);
return 0;
}
// plots raster point (ix,iy)
int dPlotPoint(complex double c, unsigned char iColor, unsigned char A[])
{
// indices of virtual 2D array
unsigned int ix;
unsigned int iy;
double Cx = creal(c);
double Cy = cimag(c);
//compute integer coordinate
if ( Cx<=CxMax && Cx>=CxMin && Cy>=CyMin && Cy<=CyMax ) // check bounds, comment it if you are sure that will be no erors
// for n = 9 p/q = 333333333 / 1000000000 t = 0.3333333330000000 c = (-0.1249999981862006 ; +0.6495190528383290) Naruszenie ochrony pamięci !!! error
// point z = (-0.4365786152835205 ; -0.1839796132996155) is out of drawing rectangle
// error from DrawCriticalOrbit for i = 999999998
{ix = (int)round((Cx-CxMin)/PixelWidth);
iy = (int)round((CyMax-Cy)/PixelHeight); // reverse y axis
iPlotBigPoint(ix, iy, iColor, A);
//printf(" PlotPoint function : point C = (%+.16f ; %+.16f) is inside of drawing rectangle \n",creal(c), cimag(c));
return 0;}
printf(" error from PlotPoint function : point C = (%+.16f ; %+.16f) is out of drawing rectangle \n",creal(c), cimag(c));
return 1;
}
double complex give_c(int iX, int iY){
double Cx,Cy;
Cy=CyMax - iY*PixelHeight; // Invert
if (fabs(Cy)< PixelHeight/2.0) Cy=0.0; /* Main antenna */
Cx=CxMin + iX*PixelWidth;
return Cx+Cy*I;
}
//
// output
// potential(c,n) = log2(abs(zn))/2^n
// https://en.wikibooks.org/wiki/Fractals/Iterations_in_the_complex_plane/MandelbrotSetExterior#Real_potential_.3D_CPM.2FM
double GivePotential(double complex C )
{
int i=0; // iteration
double d = 1.0; // 2^n computed by iteration
double complex Z = 0.0; // initial value for iteration Z0
double potential = FP_ZERO; // inside
double z_radius2; // = r*r
for(i=0;i<IterationMax;i++)
{
Z=Z*Z+C; // https://stackoverflow.com/questions/6418807/how-to-work-with-complex-numbers-in-c
d *= 2.0;
z_radius2 = cnorm(Z); //cabs(Z);
if(z_radius2 > ER2) { // exterior of M set
potential = log2(z_radius2*z_radius2)/d;
break;
}
}
return potential;
}
int ComputeAndSavePixelColor(int ix, int iy, unsigned char A[]){
unsigned char color = 0;
double potential;
complex double c;
double p;
int i;
c = give_c(ix,iy);
potential = GivePotential(c);
i = give_i(ix, iy);
if (potential == FP_ZERO)
color = 0;
else {
p = log(potential)/K;
color = 255* (1+cos(TwoPi*p))/2.0;
//color = 255; // simply white for black equipotentials
}
A[i] = color; // save color
APotential[i] = potential; // save also potential
return 0;
}
/* draw to the array and save it to pgm image */
int MakeImage(unsigned char A[]){
int ix,iy;
// draw
printf("render image = compute and write image data bytes to the array \n");
#pragma omp parallel for schedule(dynamic) private(ix,iy) shared(ixMax , iyMax)
for(iy=0;iy<iyMax;iy++)
for(ix=0;ix<ixMax;ix++)
ComputeAndSavePixelColor(ix, iy, A);
return 0;
}
/*
Gives direction ( coded in FCCode) to the left equal pixel , so it trace conterclockwise
*/
int GiveNextChainCodeEqual(int ix0, int iy0, double p0){
//double pTemp;
double dpTemp;
double p = 100000.0; // big
double dp = 100.0; // big
int ix, iy;
int i;
int f = -1; // result = final FCCode
int FCCode;//
// find FCCode for which dp is the smallest
for (FCCode = 0; FCCode < 8; FCCode ++){ //
// translate FCCode t0 ix, iy
ix = ix0 + offset[FCCode][0];
iy = iy0 + offset[FCCode][1];
// (ix,iy) -> i
i = give_i(ix,iy);// new point
if (i>0 && ABoundary[i]==0){ // inside bounds of 1D array and not visited
p = APotential[i];
if (p != FP_ZERO) { // not interior
dpTemp = fabs(p - p0);
if (dpTemp<dp){
f = FCCode; //
dp = dpTemp;}}}
}
if (f == -1) {printf("\tproblem: FCCode not found \n"); return -1;}
if(i<0 ) f = -2;
else if( ABoundary[i]>0) FCCode = -3; // check if point was visitred
return f;
}
// https://en.wikibooks.org/wiki/Fractals/Image_noise
double GiveNoiseMeasure (int ix0, int iy0, int i0){
int FCCode;//
int i;
int ix, iy;
double p0;
double p;
double dp; // abs(p - p0)
double dpMax = 0.0; // max dp
double dpArithmeticMean = 0.0;
double n = 0.0;
p0 = APotential[i0];
for (FCCode = 0; FCCode < 8; FCCode ++){
// translate FCCode t0 ix, iy
ix = ix0 + offset[FCCode][0];
iy = iy0 + offset[FCCode][1];
// (ix,iy) -> i
i = give_i(ix,iy);// new point
if (i>0){ // inside bounds of 1D array
//if( ABoundary[i]!=0) printf(" point was visited \n");
p = APotential[i];
// compute dp
if ( p == FP_ZERO)
{if (p0 == FP_ZERO)
dp = 0.0;
else dp = p0;}
else dp = fabs(p-p0); // (p && p0) != FP_ZERO
if (dp>dpMax) dpMax = dp;
dpArithmeticMean += dp;
//printf("FCCode = %d , p = %.16f dp = %.16f\n", FCCode,p, fabs(p-p0) );
n +=1.0;
}
}
dpArithmeticMean /= n; //
return dpArithmeticMean/p0; // ratio
}
// input is a (double) complex number so D
double GiveNoiseMeasureD(double complex c){
int i;
int ix, iy;
double m;
double p;
// world to screen conversion
ix = (int)round((creal(c)-CxMin)/PixelWidth);
iy = (int)round((CyMax-cimag(c))/PixelHeight); // reverse y axis
i = give_i(ix,iy);
m = GiveNoiseMeasure(ix,iy,i);
p = APotential[i];
if (p == FP_ZERO) p = 0.0;
printf("for c = (%f;%f)\tnoise measure = %.16f\tpotential = %.16f\n", creal(c), cimag(c), m, p );
return m;
}
/*
n to FCCode translation used in CheckMooreNeighborhood function
number of cell from 8 point neighborhood
1,2,3
4,5,6
7,8,9
so 5 = center
*/
int n2FCCode(int n){
int FCCode = -1;
if (n<1 || n> 9) {printf("\t bad n \t"); return -2;}
switch (n) {
case 1: FCCode = 3; break;
case 2: FCCode = 2; break;
case 3: FCCode = 1; break;
case 4: FCCode = 4; break;
case 5: FCCode = 8; break;
case 6: FCCode = 0; break;
case 7: FCCode = 5; break;
case 8: FCCode = 6; break;
case 9: FCCode = 7; break;
}
return FCCode;
}
/* used for testing
CheckMooreNeighborhood
FCCode = 3 , p = 0.0172499795393015 dp = -0.0014810494136638 FCCode = 2 , p = 0.0183472494146965 dp = -0.0003837795382688 FCCode = 1 , p = 0.0194811798131318 dp = 0.0007501508601665
FCCode = 4 , p = 0.0172866177310314 dp = -0.0014444112219338 center pixel = (1610,1000) p0 = 0.0187310289529653 FCCode = 0 , p = 0.0195177500282344 dp = 0.0007867210752691
FCCode = 5 , p = 0.0172911963906845 dp = -0.0014398325622808 FCCode = 6 , p = 0.0183884519130351 dp = -0.0003425770399302 FCCode = 7 , p = 0.0195223204303752 dp = 0.0007912914774100
CheckMooreNeighborhood end, dpMax = 0.0007912914774100 so FCCodeAsc = 7
*/
int CheckMooreNeighborhood(int ix0, int iy0){
int FCCode;//
int FCCodeAsc;//for dpMaxPos
int FCCodeDesc;// for dpMaxNeg
int FCCodeEqual;// for dpMin
int i;
int ix, iy;
int n; // number of cell :
/* 1,2,3
4,5,6
7,8,9
so 5 = center
*/
double p= 0.0;
double p0;
//
double dpMaxPos = 0.0;
double dpMaxNeg = -100.0;
double dpMin = 100.0;
double dpTemp = 0.0;
printf("\tCheckMooreNeighborhood \n");
// (ix,iy) -> i
i = give_i(ix0,iy0);// center
p0 = APotential[i];
for (n = 1; n < 10; n ++){
// translate n to FCCode
FCCode = n2FCCode( n);
//printf("\tFCCode = %d , p = %.16f dp = %.16f\t", FCCode,p, fabs(p-p0) );
if (FCCode !=8)
{
// translate FCCode t0 ix, iy
ix = ix0 + offset[FCCode][0];
iy = iy0 + offset[FCCode][1];
// (ix,iy) -> i
i = give_i(ix,iy);// new point
if (i>0){ // inside bounds of 1D array
//if( ABoundary[i]!=0) printf(" point was visited \n");
p = APotential[i];
if (p == FP_ZERO) p = 0.0; //
dpTemp = p - p0;
//
if ( dpTemp > dpMaxPos ) {
dpMaxPos = dpTemp;
FCCodeAsc = FCCode;}
//
if ( dpTemp < 0.0 && dpTemp < dpMaxNeg ) {
dpMaxNeg = dpTemp;
FCCodeDesc = FCCode;}
//
if ( fabs(dpTemp) < dpMin ) {
dpMin = dpTemp;
FCCodeEqual = FCCode;}
printf("\tFCCode = %d , p = %.16f dp = %.16f\t", FCCode,p,dpTemp );
if (n % 3 == 0 ) printf("\n");// change row
}
}
else printf("\tcenter pixel = (%d,%d) p0 = %.16f \t\t", ix0, iy0, p0);
}
printf("\tdpMaxPos = %.16f so FCCodeAsc = %d\n", dpMaxPos, FCCodeAsc);
printf("\tdpMaxNeg = %.16f so FCCodeDesc = %d\n", dpMaxNeg, FCCodeDesc);
printf("\tdpMin = %.16f so FCCodeEqual = %d\n", dpMin, FCCodeEqual);
printf("\tCheckMooreNeighborhood end\n");
return 0;
}
/*
give_i_AndDescribePixel
extended give_i
*/
int give_i_(int ix, int iy){
int i;
double potential;
if (ix==ixMin || ix == ixMax || iy==iyMin || iy == iyMax)
printf("\tc is on the array boundary: ix = %d iy = %d", ix, iy);
else printf("\tc is inside the array : iy = %d iy = %d", ix, iy);
// (ix,iy) -> i
i = give_i(ix,iy);// new point
//
if (i>0){ // inside bounds of 1D array
potential = APotential[i];
if (potential == FP_ZERO)
printf("\tand inside M set\n");
else printf("\tand outside M set\n");
}
return i;
}
int DrawEquipotential(double complex c, unsigned char iColor, unsigned char A[]){
double potential;
double p0; // potential of the whole equipotential line
// world to screen conversion
int ix0 = (int)round((creal(c)-CxMin)/PixelWidth);
int iy0 = (int)round((CyMax-cimag(c))/PixelHeight); // reverse y axis
int i0; // = give_i(ix0,iy0);
int i;
int ix = ix0;
int iy = iy0;
int n=0;
int nMax = 20000;
int FCCode;
printf("\ndraw equipotential curve thru point c = (%.16f; %.16f) pixel = (%d, %d)\n ", creal(c), cimag(c), ix0,iy0);
printf("\tstart point\n\t");
GiveNoiseMeasureD(c);
i0 = give_i_(ix0,iy0); // compute and describe
if ( i0<0){ printf("\tc is out of drawing rectangle. End. \n"); return 1;}
p0 = APotential[i0];
if (p0 == FP_ZERO) p0 = 0.0; //
ABoundary[i0]=1; // mark as visited, but not starting point because it wil be omited in GiveNextChainCodeEqual and curve do not close
//if (p0 == FP_ZERO) { printf("\tstart point c is inside. End.\n"); return 1;}
//else
//printf("\tplot point n = %d\n", n);
iPlotBigPoint(ix0, iy0, iColor, A);
//A[i0] = 255 - A[i0];
// printf("\tStart point\tix = %d iy = %d i = %d potential = %.16f\n", ix, iy, i0 , p0);
potential = p0;
// draw next points
for(n=0; n<nMax; n++){
//printf("\t n = %d\n", n);
if (n == 2) ABoundary[i0]=0; // mark start point as not visited, because it wil be omited in GiveNextChainCodeEqual and curve do not close
//CheckMooreNeighborhood(ix, iy,p0);
// choose next point
FCCode = GiveNextChainCodeEqual( ix, iy, p0);
if (FCCode <0 || FCCode>7) {printf("\tdrawing stopped because of bad FCCode = %d after %d steps ( pixels)\n ", FCCode, n); return 1;}
// translate FCCode t0 ix, iy
ix += offset[FCCode][0];
iy += offset[FCCode][1];
// (ix,iy) -> i
i = give_i(ix,iy); // new point
if ( i<0){ printf("\tpoint out of drawing rectangle. End. \n"); return 1;}
// Termination Condition = stoping criteria
if(ix==ix0 && iy == iy0 )
{printf("\tend point\tix = %d iy = %d i = %d potential = %.16f\n", ix, iy, i, potential );
printf("\tcurve is closed = stop ( good) after %d steps (pixels)\n\n", n);
return 0;}
if (i>0)
{
//printf("\tn = %d FCCode = %d ix = %d iy = %d i = %d potential = %.16f\n", n ,FCCode, ix, iy, i, potential );
if (ABoundary[i]==1) { printf("\tproblem: next point c was visited = loop ?\n");
//CheckMooreNeighborhood(ix, iy,p0);
return 2;}
potential = APotential[i];
if (potential == FP_ZERO) { printf("\tc is inside. End. \n"); return 3;}
//
iPlotBigPoint(ix, iy, iColor, A);
//A[i] = 255 - A[i]; //iColor;
ABoundary[i]=1; // mark as visited
}
//else {printf("\terror from DrawEquipotential : bad i \n"); return -1; }
}
// end after n = nMax
potential = APotential[give_i(ix, iy)];
if (potential ==FP_ZERO) potential = 0.0;
printf("\tend point\tix = %d iy = %d i = %d potential = %.16f\n", ix, iy, i, potential );
printf("\tcurve is not closed = stop ( bad !!) after %d steps (pixels)\n\n", n);
return 0; //
}
/*
Gives direction ( coded in FCCode) to the pixel with most greater potential
gradient ascent, hill climbing
External ray out
!!!!!! not works !!!!!
*/
int GiveNextChainCodeAscent(int ix0, int iy0, double p0){
//double pTemp;
double dpTemp;
double p = 0.0; // low
double dp = 0.0; // low
int ix, iy;
int i;
int f = -1; // result = final FCCode
int FCCode;//
// find FCCode for which dp is the smallest
if (p0 == FP_ZERO) {printf("\tproblem from GiveNextChainCodeAscent: p0 = FP_ZERO \n");return -1;}
for (FCCode = 0; FCCode < 8; FCCode ++){ //
// translate FCCode t0 ix, iy
ix = ix0 + offset[FCCode][0];
iy = iy0 + offset[FCCode][1];
// check border
if (ix == 0 || ix == ixMax) {printf("\tend of image, stop : ix = %d \n", ix); return -2;}
if (iy == 0 || iy == iyMax) {printf("\tend of image, stop : iy = %d \n", iy); return -3;}
// (ix,iy) -> i
i = give_i(ix,iy);// new point
if (i>0 ){ // inside bounds of 1D array and not visited
p = APotential[i];
if (p != FP_ZERO) { // not interior
dpTemp = (p - p0);
if (dpTemp>dp && dpTemp > 0.0){
f = FCCode; //
dp = dpTemp;}}}
}
if (f == -1) {printf("\tproblem from GiveNextChainCodeAscent: FCCode not found \n"); return -2;}
//if(i<0 ) f = -2;
//else if( ABoundary[i]>0) FCCode = -3; // check if point was visitred
return f;
}
/*
cases:
* A = ascending
* E = equal
* D = descending
* C = center
* A *
E C E
* D *
E A E
* C *
* D *
* A *
* C *
E D E
*/
int GiveAscent(int ix0, int iy0, double p0){
int FCCode;//
int ix, iy, i;
double p;
// equipotential
FCCode = GiveNextChainCodeEqual(ix0,iy0,p0);
if (FCCode <0 || FCCode>7) {printf("\tGiveAscent: bad FCCode = %d \n ", FCCode); return -1;}
// perpendicular to equipotential
// but it not works in case of concave/convex
// compute bot equal code and find code between them !!!!
// see cases : normal = ( e_left + e_right )/2
FCCode = (FCCode - 2) ;
if (FCCode < 0) FCCode += 7;