/
fpnoguigraphicsbridge.pas
5307 lines (4581 loc) · 170 KB
/
fpnoguigraphicsbridge.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
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
unit FPNoGUIGraphicsBridge;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils,
FPCanvas, FPImgCanv, FPWritePNG, FPReadPNG, FPImage, FTFont, freetype,
types, GeometryUtilsBridge, FPColorBridge, ViewPort;
(*
From: http://wiki.freepascal.org/Developing_with_Graphics
"You can draw images which won't be displayed in the screen without the LCL, by just using fcl-image directly.
For example a program running on a webserver without X11 could benefit from not having a visual library
as a dependency.
FPImage (alias fcl-image) is a very generic image and drawing library written completely in pascal.
In fact the LCL uses FPImage too for all the loading and saving from/to files and implements the
drawing function through calls to the widgetset (winapi, gtk, carbon, ...).
Fcl-image on the other hand also has drawing routines.
For more information, please read the article about fcl-image" (http://wiki.freepascal.org/fcl-image).
*)
const
MAXPOINTS = 599; // indeed 600! 0--99
type
jbyte = shortint; // signed 8 bits
Pjbyte =^jbyte;
TPoint3D = record
X,Y,Z : Real;
end;
TLine3D = record
P1, P2: TPoint3D;
end;
TSlice = record
Data: real;
Caption: string;
Color: TTFPColorBridge;
end;
TLegend = record
x: real;
y: real;
Caption: string;
Color: TTFPColorBridge;
end;
TBar = record
Data: real;
Category: string;
Color: TTFPColorBridge;
end;
THistogram = record
Data: real;
Color: TTFPColorBridge;
end;
TColorRGB=packed record {copy from ...\fcl-image\src\BMPcomn.pp}
B,G,R:Byte;
end;
TColorRGBA=packed record {copy from ...\fcl-image\src\BMPcomn.pp}
case Boolean of
False:(B,G,R,A:Byte);
True:(RGB:TColorRGB);
end;
TEntityState = (etNone, etText, etPoint, etLine, etParallelLine,etParallelLineHor,etParallelLineVer,
etHorizontalLine, etVerticalLine, etAngularLine, etTrimLine, etExtendLine, etTranslateLine,
etCircle, etRectangle, etPolyline, etArc, etArrow, etLinearAlignedDim, etLinearVerticalDim,
etLinearHorizontalDim,etRadialDim, etDiameterDim, etArcDim, etAngleDim,etSelect, etErase,
etZoomCapture, etPan, etConnectPoly);
TLineToggleSide = (tsSide1, tsSide2);
TRealPoints = array[0..MAXPOINTS] of TRealPoint;
TFX = function(X: real): real;
TDesignFX = procedure(x: real; out y:real; out skip: boolean) of object;
TDesignParamFT = procedure(t: real; out x: real; out y:real; out skip: boolean) of object;
TFCLImageBridge = class(TObject)
private
FImgMem: TFPCustomImage;
FPngImgReader: TFPCustomImageReader;
FPngImgWriter: TFPCustomImageWriter;
FFreeTypeFont: TFreeTypeFont;
FThresholdGray: word;
FBinaryscaleGrayThreshold: TFPColor;
function FPColorToRGBA(Const Color : TFPColor) : TColorRGBA;
function RGBAToFPColor(Const Color : TColorRGBA) : TFPColor;
function Grayscale(color: TFPColor): TFPColor;
function Binaryscale(color: TFPColor): TFPColor; //; threshold: integer
function GetPixel(col, row: integer): TFPColor;
procedure SetPixel(col, row: integer; color: TFPColor);
procedure SetBinaryscaleGrayThreshold(value: TFPColor);
public
Width: integer;
Height: integer;
PRGBAImg: PByte; //array of byte... That's where the RGBA array goes.
Canvas: TFPCustomCanvas;
constructor Create(W,H: integer);
destructor Destroy; override;
procedure SaveToFilePNG(path: string);
procedure LoadFromFilePNG(path: string);
procedure CopyToCanvas(ShiftX,ShiftY: integer; ACanvas: TFPCustomCanvas);
procedure CopyFromCanvas(ShiftX,ShiftY: integer; ACanvas: TFPCustomCanvas);
procedure CopyFromCanvas(ACanvas: TFPCustomCanvas);
function GetRGBAGraphics(const buffer: PByte): boolean; overload;
function GetRGBAGraphics(const buffer: PJByte): boolean; overload; //android
function SetRGBAGraphics(const buffer: PByte): boolean; overload;
function SetRGBAGraphics(const buffer: PJByte): boolean; overload; //android
function GetInvertedscaleRGBAGraphics(const buffer: PByte): boolean;
function GetInvertedscaleRGBAGraphics(const buffer: PJByte): boolean; //android
function Brightness(color: TFPColor): integer;
function GetGrayscaleRGBAGraphics(const buffer: PJByte): boolean;
function GetGrayscaleRGBAGraphics(const buffer: PByte): boolean;
function GetBinaryscaleRGBAGraphics(const buffer: PJByte): boolean; //TODO
function GetBinaryscaleRGBAGraphics(const buffer: PByte): boolean;
procedure SetSize(W,H: integer);
procedure SetSize(backgroundPNGFile: string);
procedure SetFont(pathToFile: string);
property FreeTypeFont: TFreeTypeFont read FFreeTypeFont write FFreeTypeFont;
property BinaryscaleGrayThreshold: TFPColor read FBinaryscaleGrayThreshold write SetBinaryscaleGrayThreshold;
//http://docwiki.embarcadero.com/RADStudio/Berlin/en/Properties_(Delphi)
property Pixel[col,row: integer]: TFPColor read GetPixel write SetPixel; default;
end;
TFunction = class
private
public
FX: TFX;
MinX, MaxX: real;
CountPoints: integer;
Points: array of TRealPoint;
function GetMinY:real;
function GetMaxY:real;
function GetMinX:real;
function GetMaxX:real;
procedure Draw(VP: TViewPort; Canvas: TFPCustomCanvas);
procedure MakePoints(nPoints: integer; xmin, xmax: real);
procedure CrossHatch(VP: TViewPort; Canvas: TFPCustomCanvas; a,b: real);
procedure CrossHatch(VP: TViewPort; Canvas: TFPCustomCanvas; a,b: real; objF: TFunction );
function IntegralSimpson(a, b: real; n: integer): real;
constructor Create(F: TFX; xmix, xmax: real);
constructor Create(Pts: Array of TRealPoint);
destructor Destroy; override;
end;
TEntityData = class(TPersistent)
private
FDimExtensionLineGAP: integer; //TOD0: event drive here!
FDimArrowSize: integer;
FDimArrowWidth: integer;
FDimLineOffset: integer;
FDimTextHeight: integer;
FLineToggleSide: TLineToggleSide;
FTitle: string;
FLayer: string;
FPolylineClosed: boolean;
FAngleOfText: real; //degrees
FOffset: real;
FTagX: integer;
FTagY: integer;
FTagLabel: string;
FFieldSeparator: char;
FNameValueSeparator: char;
public
Vertice: TVertice;
constructor Create;
Destructor Destroy; override;
published
property DimExtensionLineGAP: integer read FDimExtensionLineGAP write FDimExtensionLineGAP;
property DimArrowSize: integer read FDimArrowSize write FDimArrowSize;
property DimArrowWidth: integer read FDimArrowWidth write FDimArrowWidth;
property DimLineOffset: integer read FDimLineOffset write FDimLineOffset;
property DimTextHeight: integer read FDimTextHeight write FDimTextHeight;
property LineToggleSide: TLineToggleSide read FLineToggleSide write FLineToggleSide;
property Title: string read FTitle write FTitle;
property Layer: string read FLayer write FLayer;
property PolylineClosed: boolean read FPolylineClosed write FPolylineClosed;
property AngleOfText: real read FAngleOfText write FAngleOfText;
property Offset: real read FOffset write FOffset;
property TagX: integer read FTagX write FTagX;
property TagY: integer read FTagY write FTagY;
property TagLabel: string read FTagLabel write FTagLabel;
property FieldSeparator: char read FFieldSeparator write FFieldSeparator;
property NameValueSeparator: char read FNameValueSeparator write FNameValueSeparator;
end;
TEntity = class
private
FEntityData: TEntityData;
FEntityState: TEntityState;
procedure ParallelLineByXYPoint(px, py: real);
procedure ParallelRectangleByXYPoint(px, py: real);
procedure ParallelPolygonByXYPoint(px, py: real);
procedure ParallelEllipseByXYPoint(px, py: real);
procedure ParallelTextByXYPoint(px, py: real);
procedure ParallelArcByXYPoint(px, py: real);
procedure ParallelLinearAlignedDimByXYPoint(px, py: real);
procedure MiddlePointText(out x: real; out y: real);
procedure MiddlePointLine(out x: real; out y: real);
procedure MiddlePointCircle(out x: real; out y: real);
procedure MiddlePointArc(out x: real; out y: real);
procedure MiddlePointRectangle(out x: real; out y: real);
procedure MiddlePointPolygon(out x: real; out y: real);
procedure MiddlePointLinearAlignedDim(out x: real; out y: real);
function AreaPolygon: real;
function AreaCircle: real;
function GetCX: real;
function GetCY: real;
function GetRadius: real;
function GetX1: real;
function GetY1: real;
function GetX2: real;
function GetY2: real;
function GetX3: real;
function GetY3: real;
public
constructor Create(layerName: string; entityType: TEntityState; V: array of TRealPoint; sTitle, sTag: string);
Destructor Destroy; override;
procedure ReSizeLine(dxy: real; refPoint: integer);
procedure TranslateLine(dxy: real);
function DataToString(fieldSeparator: char; nameValueSeparator: char): string;
function DataToDXF: string;
function DataToSVG: string;
function DataToXML: string;
function DataToNameSpace(fieldSeparator: char; nameValueSeparator: char): string;
procedure MiddlePoint(out x: real; out y: real); //generic middlepoint
procedure TranslateByXY(VP:TViewPort; X, Y:integer); //generic translate
function Area: real; //generic area
procedure Draw(VP: TViewPort; Canvas: TFPCustomCanvas); //generic draw
procedure DrawPoint(Canvas: TFPCustomCanvas; x,y: integer; sTitle: string);
procedure DrawPoint(VP: TViewPort; Canvas: TFPCustomCanvas);
procedure DrawText(Canvas: TFPCustomCanvas; x,y: integer; sTitle: string);
procedure DrawText(VP: TViewPort; Canvas: TFPCustomCanvas);
procedure DrawLine(Canvas: TFPCustomCanvas; px1, py1, px2, py2: integer; sTitle: string);
procedure DrawLine(VP: TViewPort; Canvas: TFPCustomCanvas);
procedure DrawPolyline(VP: TViewPort; Canvas: TFPCustomCanvas);
procedure DrawRectangle(Canvas: TFPCustomCanvas; px1, py1, px2, py2: integer; sTitle: string);
procedure DrawRectangle(VP: TViewPort; Canvas: TFPCustomCanvas);
procedure DrawCircle(VP: TViewPort; Canvas: TFPCustomCanvas);
procedure DrawCircle(Canvas: TFPCustomCanvas; centerX, centerY, radius: integer; sTitle: string);
procedure DrawArc(Canvas: TFPCustomCanvas; px1, py1, px2, py2, px3, py3:integer; sTitle: string);
procedure DrawArc(VP: TViewPort; Canvas: TFPCustomCanvas);
procedure DrawArcDimension(VP: TViewPort; Canvas: TFPCustomCanvas; dx1, dy1, dx2, dy2, dx3, dy3: integer; sTitle: string);
procedure DrawArcDimension(VP: TViewPort; Canvas: TFPCustomCanvas);
procedure DrawParallelLine(VP: TViewPort; Canvas: TFPCustomCanvas; dx1, dy1, dx2, dy2: real; sTitle: string);
procedure DrawParallelLine(VP: TViewPort; Canvas: TFPCustomCanvas);
procedure DrawLinearDimension(VP: TViewPort; Canvas: TFPCustomCanvas; dx1, dy1, dx2, dy2: integer; sTitle: string);
procedure DrawLinearDimension(VP: TViewPort; Canvas: TFPCustomCanvas);
procedure DrawLineArrow(VP: TViewPort; Canvas: TFPCustomCanvas; dx1, dy1, dx2, dy2: integer; sTitle: string; refPoint: integer {1 or 2});
procedure DrawRadialDimension(VP: TViewPort; Canvas: TFPCustomCanvas; cx, cy, r: integer; sTitle: string);
procedure DrawRadialDimension(VP: TViewPort; Canvas: TFPCustomCanvas);
procedure DrawArrow(Canvas: TFPCustomCanvas; V: array of TPoint; sTitle: string);
procedure DrawArrow(VP: TViewPort; Canvas: TFPCustomCanvas);
procedure ResetPoints(V: array of TRealPoint);
property EntityState: TEntityState read FEntityState write FEntityState;
property CX :real read GetCX;
property CY: real read GetCY;
property R: real read GetRadius;
property X1: real read GetX1;
property Y1: real read GetY1;
property X2: real read GetX2;
property Y2: real read GetY2;
property X3: real read GetX3;
property Y3: real read GetY3;
property EntityData: TEntityData read FEntityData write FEntityData;
end;
TFPNoGUIGraphicsBridge = class(TComponent)
private
FWidth: integer;
FHeight: integer;
FPathToFontFile: string;
FEntityData: TEntityData;
FViewPort: TViewPort;
FOnDesignFunction: TDesignFX;
FOnDesignParamFunction: TDesignParamFT;
procedure SetViewPort(AValue: TViewPort);
procedure DoDesignFunction(x: real; out y:real; out skip: boolean);
procedure DoDesignParamFunction(t: real; out x: real; out y:real; out skip: boolean);
procedure SetWidth(AValue: integer);
procedure SetHeight(AValue: integer);
procedure SetPathToFontFile(pathToFile: string);
procedure SetSize(W,H: integer);
procedure SetSize(backgroundPNGFile: string);
procedure DrawArrow(V: array of TPoint);
public
Surface: TFCLImageBridge;
EntityList: TList;
FunctionList: TList;
Lines3DDynArray: Array of TLine3D;
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
procedure SetSurfaceSize(W,H: integer);
procedure SetSurfaceSize(backgroundPNGFile: string);
procedure PaintGrid(VP: TViewPort; clearscr: boolean);
procedure PaintGrid(clearscr: boolean);
procedure PaintViewPort(VP: TViewPort);
procedure PaintViewPort;
procedure DrawFunction(clearscr: boolean; SelectedIndex: integer);
procedure DrawFunction(clearscr: boolean; VP:TViewPort; SelectedIndex: integer);
procedure DrawFunction(clearscr: boolean; xmin, xmax: real); //event driven
procedure DrawFunction(clearscr: boolean); //event driven
procedure DrawParameterizedFunction(clearscr: boolean; tmin, tmax: real); //event driven
procedure CopyToCanvas(Canvas: TFPCustomCanvas);
function DrawEntity(VP: TViewPort; SelectedIndex: integer): TEntity;
function DrawEntity(SelectedIndex: integer): TEntity;
function DrawEntity(layerName, entityName: string; V: array of TRealPoint; sTitle, sTag: string): integer;
function DrawEntity(entity: TEntityState; V: array of TRealPoint; sTitle, sTag: string): integer;
procedure DrawEntity(index: integer; bridgeColor: TTFPColorBridge; thickness: integer);
procedure DrawEntities;
procedure DrawEntities(layerName: string);
procedure LoadEntitiesFromFile(fileName: string);
procedure SaveEntitiesToFile(fileName: string);
function AddEntity(layerName:string; Entity: TEntityState; V: array of TRealPoint; sTitle, sTag: string): integer;
function AddEntity(layerName, entityName: string; V: array of TRealPoint; sTitle, sTag: string): integer;
function AddEntity(stringData: string; sTag: string): integer;
function AddEntity(AEntity: TEntity): integer;
function AddFunction(F: TFX; xmin, xmax: real): integer;
function AddFunction(Pts: array of TRealPoint): integer;
procedure ClearEntityList;
procedure ClearEntityListByLayer(layer: string);
procedure DeleteEntity(index: integer);
procedure ClearFunctionList;
procedure ClearSurface;
procedure ClearSurfaceByViewPort(VP: TViewPort);
function GetFunction(index: integer): TFunction;
function GetEntity(index: integer): TEntity;
function GetEntity(VP:TViewPort; X,Y: integer; out index: integer): TEntity;
function GetEntity(VP:TViewPort; X,Y: integer; out index: integer; layerName: string): TEntity;
function TranslateEntityXY(VP:TViewPort; index: integer; X,Y: integer): TEntity;
function TranslateEntityXY(index: integer; X,Y: integer): TEntity;
procedure PanX(VP:TViewPort; dx: real);
procedure PanXY(VP:TViewPort; X0, Y0, X, Y: integer);
procedure PanY(VP:TViewPort; dy: real);
procedure SetBackGroundColor(colorBridge: TTFPColorBridge);
procedure SetPenThickness(penThickness: integer);
procedure SetPenColor(colorBridge: TTFPColorBridge);
procedure SetFontColor(colorBridge: TTFPColorBridge);
procedure SetFontHeight(fontHeight: integer);
procedure SetFontAntiAliased(antiAliased: boolean);
procedure SetFontResolution(dpiResolution: integer);
procedure DrawPath(VP: TViewPort; Points: array of TRealPoint);
procedure DrawPath(Points: array of TRealPoint);
procedure DrawCircle(VP: TViewPort; Points: array of TRealPoint);
procedure DrawCircle(Points: array of TRealPoint);
procedure DrawFillCircle(VP: TViewPort; Points: array of TRealPoint);
procedure DrawFillCircle(Points: array of TRealPoint);
procedure DrawRectangle(VP: TViewPort; Points: array of TRealPoint);
procedure DrawRectangle(Points: array of TRealPoint);
procedure DrawFillRectangle(VP: TViewPort; Points: array of TRealPoint);
procedure DrawFillRectangle(Points: array of TRealPoint);
procedure TextOut(P: TRealPoint; txt: string);
procedure TextOut(VP: TViewPort; P: TRealPoint; txt: string);
procedure TextOut(P: TRealPoint; txt: string; fontSize: integer; textColor: TTFPColorBridge);
procedure TextOut(VP: TViewPort; P: TRealPoint; txt: string; fontSize: integer; textColor: TTFPColorBridge);
procedure TextOut(P: TRealPoint; txt: string; fontSize: integer);
procedure TextOut(VP: TViewPort; P: TRealPoint; txt: string; fontSize: integer);
procedure DrawEllipse(P: array of TRealPoint);
procedure DrawEllipse(VP: TViewPort; P: array of TRealPoint);
procedure DrawPolygon(P: array of TRealPoint);
procedure DrawPolygon(VP: TViewPort; P: array of TRealPoint);
procedure DrawFillEllipse(P: array of TRealPoint);
procedure DrawFillEllipse(VP: TViewPort; P: array of TRealPoint);
procedure DrawLineArrow(VP: TViewPort; x1, y1, x2, y2: real; refLinePoint: integer {1 or 2 or any});
procedure DrawLineArrow(x1, y1, x2, y2: real; refLinePoint: integer {1 or 2 or any});
procedure DrawLine(VP: TViewPort; x1, y1, x2, y2: real);
procedure DrawLine(x1, y1, x2, y2: real);
procedure DrawDataPieSlices(EllipseRec: array of TRealPoint; slices: array of TSlice; showData: boolean);
procedure DrawDataPieSlices(VP: TViewPort; EllipseRec: array of TRealPoint; slices: array of TSlice; showData: boolean);
procedure DrawDataBars(bars: array of TBar);
procedure DrawDataBars(VP: TViewPort; bars: array of TBar);
procedure DrawDataHistograms(histograms: array of THistogram; range: real);
procedure DrawDataHistograms(VP: TViewPort; histograms: array of THistogram; range: real);
procedure DrawDataLine(data: array of TRealPoint; legend: TLegend);
procedure DrawDataLine(VP: TViewPort; data: array of TRealPoint; legend: TLegend);
//https://tianjara.net/blog/tags/projections/
Procedure Projection3Dxy(P: TPoint3D; radAng1, radAng2: real; var x, y:real);
Procedure Rotate3Dy(P: TPoint3D; Rad: Real; var x, z: real); overload;
Procedure Rotate3Dz(P: TPoint3D; Rad: Real; var x, y: real); overload;
Procedure Rotate3Dy(var lines3D: array of TLine3D; Deg: Real); overload;
Procedure Rotate3Dz(var lines3D: array of TLine3D; Deg: Real); overload;
Procedure Rotate3Dy(Deg: Real); overload;
Procedure Rotate3Dz(Deg: Real); overload;
Procedure Draw3D(lines3D: Array of TLine3D); overload;
Procedure Draw3D(); overload;
procedure AddLine3D(line3D: TLine3D); overload;
procedure AddLine3D(x1, y1, z1: real; x2, y2, z2: real); overload;
procedure ChangeLine3D(index: integer; x1, y1, z1: real; x2, y2, z2: real); overload;
procedure ChangeLine3D(var lines3D: Array of TLine3D;
index: integer; x1, y1, z1: real; x2, y2, z2: real); overload;
property PathToFontFile: string read FPathToFontFile write SetPathToFontFile;
protected
procedure Loaded; override;
procedure Notification(AComponent: TComponent; Operation: TOperation); override;
published
property Width: integer read FWidth write SetWidth;
property Height: integer read FHeight write setHeight;
property EntityData: TEntityData read FEntityData write FEntityData;
property ActiveViewPort: TViewPort read FViewPort write SetViewPort;
property OnDrawFunction: TDesignFX read FOnDesignFunction write FOnDesignFunction;
property OnDrawParameterizedFunction: TDesignParamFT read FOnDesignParamFunction write FOnDesignParamFunction;
end;
function ReplaceChar(query: string; oldchar, newchar: char):string;
function SplitStr(var theString : string; delimiter : string) : string;
function ToSlice(partNumber: real; caption: string; partColor: TTFPColorBridge): TSlice;
function ToBar(partNumber: real; category: string; partColor: TTFPColorBridge): TBar;
function ToLegend(caption: string; partColor: TTFPColorBridge; x: real; y: real): TLegend;
function ToHistogram(partNumber: real; partColor: TTFPColorBridge): THistogram;
function ToLine3D(x1, y1, z1: real; x2, y2, z2: real): TLine3D;
implementation
function ToLine3D(x1, y1, z1: real; x2, y2, z2: real): TLine3D;
begin
Result.P1.x:= x1;
Result.P1.y:= y1;
Result.P1.z:= z1;
Result.P2.x:= x2;
Result.P2.y:= y2;
Result.P2.z:= z2;
end;
function ToSlice(partNumber: real; caption: string; partColor: TTFPColorBridge): TSlice;
begin
Result.Data:= partNumber;
Result.Color:= partColor;
Result.Caption:= caption;
end;
function ToLegend(caption: string; partColor: TTFPColorBridge; x: real; y: real): TLegend;
begin
Result.x:= x;
Result.y:= y;
Result.Caption:= caption;
Result.Color:= partColor;
end;
function ToBar(partNumber: real; category: string; partColor: TTFPColorBridge): TBar;
begin
Result.Data:= partNumber;
Result.Color:= partColor;
Result.Category:= category;
end;
function ToHistogram(partNumber: real; partColor: TTFPColorBridge): THistogram;
begin
Result.Data:= partNumber;
Result.Color:= partColor;
end;
{TFEntityData}
constructor TEntityData.Create;
begin
FDimExtensionLineGAP:= 4;
FDimArrowSize:= 12;
FDimArrowWidth:= 4;
FDimLineOffset:= 20;
FDimTextHeight:= 9;
LineToggleSide:= tsSide1;
DimExtensionLineGAP:= 4;
DimArrowSize:= 12;
DimArrowWidth:= 4;
DimLineOffset:= 20;
DimTextHeight:= 9;
FLineToggleSide:= tsSide1;
FTitle:= 'Title1';
FTagLabel:= 'Label1';
FPolylineClosed:= True; {Polyline Closed} //TODO
FAngleOfText:= 0; //TODO
FOffset:= 0;
FLayer:='0'; //default dxf layer
FFieldSeparator:= ';';
FNameValueSeparator:= '#';
end;
Destructor TEntityData.Destroy;
begin
//
SetLength(Vertice, 0);
Vertice:= nil;
inherited Destroy;
end;
{TFunction}
procedure TFunction.CrossHatch(VP: TViewPort; Canvas: TFPCustomCanvas; a,b: real);
var
i,dx: real;
X1,Y1,X2,Y2: integer;
begin
i:=a;
dx:= (b-a)/30;
while i <= b do
begin
VP.WorldToSurfaceXY(i,FX(i),X1,Y1);
VP.WorldToSurfaceXY(i,0,X2,Y2);
Canvas.Line(X1,Y1,X2,Y2);
i:= i + dx;
end;
end;
procedure TFunction.CrossHatch(VP: TViewPort; Canvas: TFPCustomCanvas; a,b: real; objF: TFunction );
var
i,dx: real;
X1,Y1,X2,Y2: integer;
begin
i:=a;
dx:= (b-a)/30;
while i <= b do
begin
VP.WorldToSurfaceXY(i,FX(i),X1,Y1);
VP.WorldToSurfaceXY(i,objF.FX(i),X2,Y2);
Canvas.Line(X1,Y1,X2,Y2);
i:= i + dx;
end;
end;
procedure TFunction.Draw(VP: TViewPort; Canvas: TFPCustomCanvas);
var
i,X,Y: integer;
inside: boolean;
begin
VP.WorldToSurfaceXY(Points[0].x, FX(Points[0].x),X,Y);
Canvas.MoveTo(X,Y);
for i:= 1 to CountPoints-1 do
begin
inside:= VP.WorldToSurfaceXY(Points[i].x,Points[i].y ,X,Y);
if not VP.Cliping then
begin
Canvas.LineTo(X,Y)
end
else //clip
begin
if not inside then Canvas.MoveTo(X,Y) else Canvas.LineTo(X,Y);
end;
end;
end;
function TFunction.GetMaxY: real;
var
temp:real;
i:integer;
begin
temp:= Points[0].y;
for i:=1 to CountPoints-1 do if(temp < Points[i].y) then temp:= Points[i].y;
Result:= temp;
end;
function TFunction.GetMinY: real;
var
temp:real;
i: integer;
begin
temp:= Points[0].y;
for i:=1 to CountPoints-1 do if(temp > Points[i].y) then temp:= Points[i].y;
Result:=temp;
end;
function TFunction.GetMaxX: real;
var
temp:real;
i:integer;
begin
temp:= Points[0].x;
for i:=1 to CountPoints-1 do if(temp < Points[i].x) then temp:= Points[i].x;
Result:= temp;
end;
function TFunction.GetMinX: real;
var
temp:real;
i: integer;
begin
temp:= Points[0].x;
for i:=1 to CountPoints-1 do if(temp > Points[i].x) then temp:= Points[i].x;
Result:=temp;
end;
function TFunction.IntegralSimpson(a, b: real; n: integer): real;
var {Role 1/3 Simpson}
I1,I2,I3, h: real;
i,j,m: integer;
X: array of real;
begin
m:= n;
if Odd(m)then inc(m); {m even!}
if n < 10 then m:= 10;
SetLength(X, m);
h := (b - a)/(m); {m even!}
for i:=0 to m do
begin
X[i]:= a+i*h;
end;
I1:= FX(X[0])+ FX(X[m]);
I2:= 0;
I3:= 0;
for j:=1 to m-1 do
begin
if Odd(j) then // odd
I3:= I3 + FX(X[j])
else //even
I2:= I2 + FX(X[j]);
end;
SetLength(X, 0);
Result:=(h/3)*(I1+2*I2+4*I3);
end;
procedure TFunction.MakePoints(nPoints: integer; xmin, xmax: real);
var
i: integer;
begin
MaxX:= xmax;
MinX:= xmin;
CountPoints:= nPoints;
SetLength(Points, CountPoints);
for i:=1 to CountPoints do
begin
Points[i-1].x:= xmin + (i-1)*(xmax-xmin)/(CountPoints);
Points[i-1].y:= FX(Points[i-1].x);
end;
end;
constructor TFunction.Create(F: TFX; xmix, xmax: real);
begin
CountPoints:= MAXPOINTS;
FX:= F;
MakePoints(CountPoints, xmix, xmax);
end;
constructor TFunction.Create(Pts: Array of TRealPoint);
var
i: integer;
begin
CountPoints:= High(Pts) + 1;
SetLength(Points, CountPoints);
FX:= nil;
for i:=0 to CountPoints-1 do
begin
Points[i].x:= Pts[0].x;
Points[i].y:= Pts[0].y;
end;
end;
destructor TFunction.Destroy;
begin
SetLength(Points, 0);
Points:= nil;
inherited Destroy;
end;
{TEntity}
function TEntity.GetRadius: real;
begin
Result:= Abs(FEntityData.Vertice[1].x - FEntityData.Vertice[0].x);
end;
function TEntity.GetCX: real;
begin
Result:= FEntityData.Vertice[0].x;
end;
function TEntity.GetCY: real;
begin
Result:= FEntityData.Vertice[0].y;
end;
function TEntity.GetX1: real;
begin
Result:= FEntityData.Vertice[0].x;
end;
function TEntity.GetY1: real;
begin
Result:= FEntityData.Vertice[0].y;
end;
function TEntity.GetX2: real;
begin
Result:= FEntityData.Vertice[1].x;
end;
function TEntity.GetY2: real;
begin
Result:= FEntityData.Vertice[1].y;
end;
function TEntity.GetX3: real;
begin
Result:= FEntityData.Vertice[2].x;
end;
function TEntity.GetY3: real;
begin
Result:= FEntityData.Vertice[2].y;
end;
procedure TEntity.ParallelLineByXYPoint(px, py: real);
var
pmx, pmy: real;
dx,dy: real;
begin
MiddlePointLine(pmx,pmy);
dx:= px - pmx;
dy:= py - pmy;
FEntityData.Vertice[0].x:=FEntityData.Vertice[0].x+dx;
FEntityData.Vertice[0].y:=FEntityData.Vertice[0].y+dy;
FEntityData.Vertice[1].x:=FEntityData.Vertice[1].x+dx;
FEntityData.Vertice[1].y:=FEntityData.Vertice[1].y+dy;
end;
procedure TEntity.ParallelRectangleByXYPoint(px, py: real);
var
pmx, pmy: real;
dx,dy: real;
begin
MiddlePointLine(pmx,pmy);
dx:= px - pmx;
dy:= py - pmy;
FEntityData.Vertice[0].x:=FEntityData.Vertice[0].x+dx;
FEntityData.Vertice[0].y:=FEntityData.Vertice[0].y+dy;
FEntityData.Vertice[1].x:=FEntityData.Vertice[1].x+dx;
FEntityData.Vertice[1].y:=FEntityData.Vertice[1].y+dy;
end;
procedure TEntity.ParallelLinearAlignedDimByXYPoint(px, py: real);
var
pmx, pmy: real;
dx,dy: real;
begin
MiddlePointLine(pmx,pmy);
dx:= px - pmx;
dy:= py - pmy;
FEntityData.Vertice[0].x:=FEntityData.Vertice[0].x+dx;
FEntityData.Vertice[0].y:=FEntityData.Vertice[0].y+dy;
FEntityData.Vertice[1].x:=FEntityData.Vertice[1].x+dx;
FEntityData.Vertice[1].y:=FEntityData.Vertice[1].y+dy;
end;
procedure TEntity.ParallelPolygonByXYPoint(px, py: real{; V: array of TRealPoint});
var
pmx, pmy: real;
dx,dy: real;
count, i: integer;
begin
MiddlePointPolygon(pmx,pmy);
dx:= px - pmx;
dy:= py - pmy;
count := High(FEntityData.Vertice) + 1;
for i := 0 to count - 1 do
begin
FEntityData.Vertice[i].x:= FEntityData.Vertice[i].x + dx;
FEntityData.Vertice[i].y:= FEntityData.Vertice[i].y + dy;
end;
end;
procedure TEntity.ParallelEllipseByXYPoint(px, py: real);
var
pmx, pmy: real;
dx,dy: real;
begin
MiddlePointCircle(pmx,pmy);
dx:= px - pmx;
dy:= py - pmy;
FEntityData.Vertice[0].x:=FEntityData.Vertice[0].x+dx;
FEntityData.Vertice[0].y:=FEntityData.Vertice[0].y+dy;
FEntityData.Vertice[1].x:=FEntityData.Vertice[1].x+dx;
FEntityData.Vertice[1].y:=FEntityData.Vertice[1].y+dy;
end;
procedure TEntity.ParallelArcByXYPoint(px, py: real);
var
pmx, pmy: real;
dx,dy: real;
begin
MiddlePointArc(pmx,pmy);
dx:= px - pmx;
dy:= py - pmy;
FEntityData.Vertice[0].x:=FEntityData.Vertice[0].x+dx;
FEntityData.Vertice[0].y:=FEntityData.Vertice[0].y+dy;
FEntityData.Vertice[1].x:=FEntityData.Vertice[1].x+dx;
FEntityData.Vertice[1].y:=FEntityData.Vertice[1].y+dy;
end;
procedure TEntity.ParallelTextByXYPoint(px, py: real);
var
pmx, pmy: real;
dx,dy: real;
begin
MiddlePointText(pmx,pmy);
dx:= px - pmx;
dy:= py - pmy;
FEntityData.Vertice[0].x:= FEntityData.Vertice[0].x+dx;
FEntityData.Vertice[0].y:= FEntityData.Vertice[0].y+dy;
end;
procedure TEntity.MiddlePointText(out x: real; out y: real);
begin
x:= FEntityData.Vertice[0].x;
y:= FEntityData.Vertice[0].y;
end;
procedure TEntity.MiddlePointLine(out x: real; out y: real);
begin
x:= (FEntityData.Vertice[0].x + FEntityData.Vertice[1].x)/2;
y:= (FEntityData.Vertice[0].y + FEntityData.Vertice[1].y)/2;
end;
{
procedure TEntity.MiddlePointCircle(out x: real; out y: real);
begin
x:= (FEntityData.Vertice[0].x + FEntityData.Vertice[1].x)/2;
y:= (FEntityData.Vertice[0].y + FEntityData.Vertice[1].y)/2;
end;
}
procedure TEntity.MiddlePointCircle(out x: real; out y: real);
begin
x:= FEntityData.Vertice[0].x; //(FEntityData.Vertice[0].x + FEntityData.Vertice[1].x)/2;
y:= FEntityData.Vertice[0].y; //(FEntityData.Vertice[0].y + FEntityData.Vertice[1].y)/2;
end;
procedure TEntity.MiddlePointArc(out x: real; out y: real);
begin
x:= (FEntityData.Vertice[1].x + FEntityData.Vertice[2].x)/2;
y:= (FEntityData.Vertice[1].y + FEntityData.Vertice[2].y)/2;
end;
procedure TEntity.MiddlePointRectangle(out x: real; out y: real);
begin
x:= (FEntityData.Vertice[0].x + FEntityData.Vertice[1].x)/2;
y:= (FEntityData.Vertice[0].y + FEntityData.Vertice[1].y)/2;
end;
procedure TEntity.MiddlePointLinearAlignedDim(out x: real; out y: real);
begin
x:= (FEntityData.Vertice[0].x + FEntityData.Vertice[1].x)/2;
y:= (FEntityData.Vertice[0].y + FEntityData.Vertice[1].y)/2;
end;
//http://dan-scientia.blogspot.com.br/2009/10/centroide-de-um-poligono.html
procedure TEntity.MiddlePointPolygon(out x: real; out y: real);
var
i, countV, countVX: integer;
S, Scx, Scy, A, pcx, pcy: real;
VX: array of TRealPoint;
begin
countV := High(FEntityData.Vertice) + 1;
countVX:= countV +1; //extend V
SetLength(VX, countVX);
for i:=0 to countV-1 do
begin
VX[i].x:=FEntityData.Vertice[i].x;
VX[i].y:=FEntityData.Vertice[i].y;
end;
VX[countV].x:=FEntityData.Vertice[0].x; //close polygon VX
VX[countV].y:=FEntityData.Vertice[0].y;
S:=0;
for i:=0 to countVX-1 do //0,1,2
begin
S:= S + ( VX[i].x*VX[i+1].y - VX[i+1].x*VX[i].y );
end;
A:= S/2;
Scx:=0;
for i:=0 to countVX-1 do
begin
Scx:= Scx +(VX[i].x+VX[i+1].x)*
(VX[i].x*VX[i+1].y -
VX[i+1].x*VX[i].y)
end;
pcx:= (1/(6*A))*Scx;
Scy:=0;
for i:=0 to countVX-1 do
begin
Scy:= Scy +(VX[i].y+VX[i+1].y)*
(VX[i].x*VX[i+1].y -
VX[i+1].x*VX[i].y)
end;
pcy:= (1/(6*A))*Scy;
x:=pcx;
y:=pcy;
end;
{
VP.WorldToSurfaceXY(FEntityData.Vertice[0].x, FEntityData.Vertice[0].y, X1,Y1); //cx,cy, r= X2-X1
VP.WorldToSurfaceXY(, FEntityData.Vertice[1].y, X2,Y2);