lcsuperformulaoriginal.pas 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561
  1. unit LCSuperformulaOriginal;
  2. {$mode objfpc}{$H+}
  3. interface
  4. uses
  5. Classes, SysUtils, BGRALayerOriginal, BGRABitmap, BGRABitmapTypes, Math,
  6. Types;
  7. const
  8. MaxDenominator = 20;
  9. type
  10. { TSuperformulaOriginal }
  11. TSuperformulaOriginal = class(TBGRALayerCustomOriginal)
  12. private
  13. FSpikeOverlap: boolean;
  14. Fa: double;
  15. Fb: double;
  16. FBackColor: TBGRAPixel;
  17. FPenColor: TBGRAPixel;
  18. FLineWidth: double;
  19. Fm: double;
  20. FMRational: boolean;
  21. FMultiplier: double;
  22. Fn1: double;
  23. Fn2: double;
  24. Fn3: double;
  25. FDiff: TBGRAOriginalStorageDiff;
  26. FUpdateCount: integer;
  27. function FloatToFraction(ARatio: single; out num, denom: integer; AMaxDenominator: integer): string;
  28. function GetRadius: double;
  29. function GetSize: double;
  30. function GetSizeWithoutMultiplier: double;
  31. procedure SetA(AValue: double);
  32. procedure SetB(AValue: double);
  33. procedure SetBackColor(AValue: TBGRAPixel);
  34. procedure SetPenColor(AValue: TBGRAPixel);
  35. procedure SetLineWidth(AValue: double);
  36. procedure SetM(AValue: double);
  37. procedure SetMultiplier(AValue: double);
  38. procedure SetN1(AValue: double);
  39. procedure SetN2(AValue: double);
  40. procedure SetN3(AValue: double);
  41. procedure SetMRational(AValue: boolean);
  42. procedure SetSize(AValue: double);
  43. procedure SetSpikeOverlap(AValue: boolean);
  44. protected
  45. procedure GetCurve(AMatrix: TAffineMatrix; out ABackPoints: ArrayOfTPointF;
  46. out APenOutlinePoints: ArrayOfTPointF);
  47. function PenVisible: boolean;
  48. function BackVisible: boolean;
  49. procedure BeginUpdate;
  50. procedure EndUpdate;
  51. public
  52. constructor Create; override;
  53. procedure Render(ADest: TBGRABitmap; AMatrix: TAffineMatrix;
  54. ADraft: boolean); override;
  55. function GetRenderBounds(ADestRect: TRect; {%H-}AMatrix: TAffineMatrix): TRect; override;
  56. procedure GetMFraction(out ANumerator, ADenominator: integer);
  57. procedure LoadFromStorage(AStorage: TBGRACustomOriginalStorage); override;
  58. procedure SaveToStorage(AStorage: TBGRACustomOriginalStorage); override;
  59. class function StorageClassName: RawByteString; override;
  60. property SpikeOverlap: boolean read FSpikeOverlap write SetSpikeOverlap;
  61. property a: double read Fa write SetA;
  62. property b: double read Fb write SetB;
  63. property m: double read Fm write SetM;
  64. property mRational: boolean read FMRational write SetMRational;
  65. property n1: double read Fn1 write SetN1;
  66. property n2: double read Fn2 write SetN2;
  67. property n3: double read Fn3 write SetN3;
  68. property Radius: double read GetRadius;
  69. property LineWidth: double read FLineWidth write SetLineWidth;
  70. property PenColor: TBGRAPixel read FPenColor write SetPenColor;
  71. property BackColor: TBGRAPixel read FBackColor write SetBackColor;
  72. property Multiplier: double read FMultiplier write SetMultiplier;
  73. property Size: double read GetSize write SetSize;
  74. end;
  75. implementation
  76. uses BGRATransform, BGRAPen, BGRAGraphics;
  77. var
  78. MAX_LOG: double = 0.0;
  79. MIN_LOG: double = 0.0;
  80. MAX_R: double = 100.0;
  81. function SafePower(a, b: double; out c: double): boolean;
  82. var
  83. tmp: double;
  84. begin
  85. if a < 0 then
  86. begin
  87. result := SafePower(-a, b, c);
  88. c := -c;
  89. exit;
  90. end;
  91. Result := True;
  92. if a = 0 then
  93. begin
  94. if b = 0 then
  95. c := 1
  96. else
  97. c := 0;
  98. exit;
  99. end;
  100. if MAX_LOG = 0.0 then
  101. MAX_LOG := ln(MaxDouble);
  102. if MIN_LOG = 0.0 then
  103. MIN_LOG := ln(MinDouble);
  104. // ln(a^b) = b ln(a)
  105. tmp := b * ln(a);
  106. if tmp > MAX_LOG then
  107. Result := False
  108. else
  109. if tmp < MIN_LOG then
  110. c := 0.0
  111. else
  112. c := exp(tmp);
  113. end;
  114. function ComputeR(theta, a, b, m, n1, n2, n3: double): double;
  115. const
  116. EPS = 1E-9;
  117. var
  118. c, pc, s, ps: double;
  119. begin
  120. if (a = 0) or (b = 0) or (m = 0) or (n1 = 0) or (n2 = 0) or (n3 = 0) then
  121. exit(0);
  122. c := abs(cos(m * theta / 4) / a);
  123. if c < EPS then
  124. pc := 0
  125. else
  126. if not SafePower(c, n2, pc) then
  127. begin
  128. Result := MAX_R;
  129. exit;
  130. end;
  131. s := abs(sin(m * theta / 4) / b);
  132. if s < EPS then
  133. ps := 0
  134. else
  135. if not SafePower(s, n3, ps) then
  136. begin
  137. Result := MAX_R;
  138. exit;
  139. end;
  140. if pc + ps < EPS then
  141. Result := 0
  142. else
  143. if not SafePower(pc + ps, -1 / n1, Result) then
  144. Result := MAX_R;
  145. if Result > MAX_R then
  146. Result := MAX_R;
  147. end;
  148. { TSuperformulaOriginal }
  149. procedure TSuperformulaOriginal.SetA(AValue: double);
  150. begin
  151. if Fa = AValue then
  152. Exit;
  153. BeginUpdate;
  154. Fa := AValue;
  155. EndUpdate;
  156. end;
  157. procedure TSuperformulaOriginal.SetB(AValue: double);
  158. begin
  159. if Fb = AValue then
  160. Exit;
  161. BeginUpdate;
  162. Fb := AValue;
  163. EndUpdate;
  164. end;
  165. procedure TSuperformulaOriginal.SetBackColor(AValue: TBGRAPixel);
  166. begin
  167. if FBackColor = AValue then
  168. Exit;
  169. BeginUpdate;
  170. FBackColor := AValue;
  171. EndUpdate;
  172. end;
  173. procedure TSuperformulaOriginal.SetPenColor(AValue: TBGRAPixel);
  174. begin
  175. if FPenColor = AValue then
  176. Exit;
  177. BeginUpdate;
  178. FPenColor := AValue;
  179. EndUpdate;
  180. end;
  181. procedure TSuperformulaOriginal.SetLineWidth(AValue: double);
  182. begin
  183. if FLineWidth = AValue then
  184. Exit;
  185. BeginUpdate;
  186. FLineWidth := AValue;
  187. EndUpdate;
  188. end;
  189. procedure TSuperformulaOriginal.SetM(AValue: double);
  190. begin
  191. if Fm = AValue then
  192. Exit;
  193. BeginUpdate;
  194. Fm := AValue;
  195. EndUpdate;
  196. end;
  197. procedure TSuperformulaOriginal.SetMultiplier(AValue: double);
  198. begin
  199. if FMultiplier = AValue then
  200. Exit;
  201. BeginUpdate;
  202. FMultiplier := AValue;
  203. EndUpdate;
  204. end;
  205. procedure TSuperformulaOriginal.SetMRational(AValue: boolean);
  206. begin
  207. if FMRational=AValue then Exit;
  208. BeginUpdate;
  209. FMRational:=AValue;
  210. EndUpdate;
  211. end;
  212. procedure TSuperformulaOriginal.SetSize(AValue: double);
  213. var
  214. curSizeWithoutMultiplier: Double;
  215. begin
  216. curSizeWithoutMultiplier := GetSizeWithoutMultiplier;
  217. if curSizeWithoutMultiplier = 0 then exit;
  218. Multiplier:= AValue/curSizeWithoutMultiplier;
  219. end;
  220. procedure TSuperformulaOriginal.SetN1(AValue: double);
  221. begin
  222. if Fn1 = AValue then
  223. Exit;
  224. BeginUpdate;
  225. Fn1 := AValue;
  226. EndUpdate;
  227. end;
  228. procedure TSuperformulaOriginal.SetN2(AValue: double);
  229. begin
  230. if Fn2 = AValue then
  231. Exit;
  232. BeginUpdate;
  233. Fn2 := AValue;
  234. EndUpdate;
  235. end;
  236. procedure TSuperformulaOriginal.SetN3(AValue: double);
  237. begin
  238. if Fn3 = AValue then
  239. Exit;
  240. BeginUpdate;
  241. Fn3 := AValue;
  242. EndUpdate;
  243. end;
  244. procedure TSuperformulaOriginal.SetSpikeOverlap(AValue: boolean);
  245. begin
  246. if FSpikeOverlap=AValue then Exit;
  247. BeginUpdate;
  248. FSpikeOverlap:=AValue;
  249. EndUpdate;
  250. end;
  251. procedure TSuperformulaOriginal.GetCurve(AMatrix: TAffineMatrix;
  252. out ABackPoints: ArrayOfTPointF; out APenOutlinePoints: ArrayOfTPointF);
  253. var
  254. i, num, denom, precision, turns: integer;
  255. r, theta, usedM, approxM, correction: double;
  256. stroker: TBGRACustomPenStroker;
  257. begin
  258. ABackPoints := nil;
  259. APenOutlinePoints := nil;
  260. FloatToFraction(m, num, denom, MaxDenominator);
  261. approxM := num/denom;
  262. precision := max(num * 100, 100 * 3);
  263. if precision > 3000 then
  264. precision := (3000 div num)*num;
  265. if mRational then
  266. begin
  267. usedM := approxM;
  268. correction := 1;
  269. end else
  270. begin
  271. usedM:= m;
  272. correction := approxM / m;
  273. end;
  274. turns := denom * (1 + integer(SpikeOverlap and odd(num) and ((a <> b) or (n2 <> n3))));
  275. SetLength(ABackPoints, precision * turns);
  276. for i := 0 to precision * turns - 1 do
  277. begin
  278. theta := i * 2 * Pi * correction / precision;
  279. r := ComputeR(theta, a, b, usedM, n1, n2, n3) * multiplier;
  280. ABackPoints[i] := AMatrix * PointF(r * cos(theta), r * sin(theta));
  281. end;
  282. if PenVisible then
  283. begin
  284. stroker := TBGRAPenStroker.Create;
  285. try
  286. stroker.StrokeMatrix := AMatrix;
  287. stroker.JoinStyle := pjsMiter;
  288. APenOutlinePoints := stroker.ComputePolygon(ABackPoints, LineWidth);
  289. finally
  290. stroker.Free;
  291. end;
  292. end;
  293. if not BackVisible then
  294. ABackPoints := nil;
  295. end;
  296. function TSuperformulaOriginal.PenVisible: boolean;
  297. begin
  298. result := (LineWidth > 0) and (PenColor.alpha > 0);
  299. end;
  300. function TSuperformulaOriginal.BackVisible: boolean;
  301. begin
  302. result := BackColor.alpha > 0;
  303. end;
  304. procedure TSuperformulaOriginal.BeginUpdate;
  305. begin
  306. if FUpdateCount = 0 then
  307. begin
  308. FDiff := TBGRAOriginalStorageDiff.Create(self);
  309. end;
  310. Inc(FUpdateCount);
  311. end;
  312. procedure TSuperformulaOriginal.EndUpdate;
  313. begin
  314. if FUpdateCount = 0 then exit;
  315. Dec(FUpdateCount);
  316. if FUpdateCount = 0 then
  317. begin
  318. if Assigned(FDiff) then
  319. FDiff.ComputeDifference(self);
  320. NotifyChange(FDiff);
  321. FDiff := nil;
  322. end;
  323. end;
  324. constructor TSuperformulaOriginal.Create;
  325. begin
  326. inherited Create;
  327. FSpikeOverlap:= true;
  328. Fa := 1;
  329. Fb := 1;
  330. Fm := 24;
  331. Fn1 := 2;
  332. Fn2 := 1;
  333. Fn3 := 2;
  334. FLineWidth := 0;
  335. FPenColor := BGRA($00, $40, $80);
  336. FBackColor := BGRA($00, $80, $C0);
  337. FMultiplier := 200;
  338. FMRational:= true;
  339. FUpdateCount:= 0;
  340. FDiff := nil;
  341. end;
  342. function TSuperformulaOriginal.FloatToFraction(ARatio: single; out num,
  343. denom: integer; AMaxDenominator: integer): string;
  344. procedure InvFrac;
  345. var temp: integer;
  346. begin
  347. temp := num;
  348. num := denom;
  349. denom := temp;
  350. end;
  351. procedure AddFrac(AValue: integer);
  352. begin
  353. inc(num, AValue*denom);
  354. end;
  355. const MaxDev = 6;
  356. var
  357. dev: array[1..MaxDev] of integer;
  358. devCount, i: integer;
  359. curVal, remain: Single;
  360. begin
  361. if ARatio < 0 then ARatio := -ARatio;
  362. curVal := ARatio;
  363. devCount := 0;
  364. repeat
  365. inc(devCount);
  366. dev[devCount] := trunc(curVal);
  367. remain := frac(curVal);
  368. if abs(remain) < 1e-3 then break;
  369. if devCount = MaxDev then
  370. begin
  371. if remain > 0.5 then inc(dev[devCount]);
  372. break;
  373. end;
  374. curVal := 1/remain;
  375. until false;
  376. repeat
  377. num := dev[devCount];
  378. denom := 1;
  379. for i := devCount-1 downto 1 do
  380. begin
  381. InvFrac;
  382. AddFrac(dev[i]);
  383. end;
  384. if ((num >= denom) and (denom <= AMaxDenominator))
  385. or ((num < denom) and (num <= AMaxDenominator))
  386. or (devCount = 1) then break;
  387. dec(devCount);
  388. until false;
  389. result := IntToStr(num)+':'+IntToStr(denom);
  390. end;
  391. function TSuperformulaOriginal.GetRadius: double;
  392. begin
  393. result := 1;
  394. end;
  395. function TSuperformulaOriginal.GetSize: double;
  396. begin
  397. result := GetSizeWithoutMultiplier * Multiplier;
  398. end;
  399. function TSuperformulaOriginal.GetSizeWithoutMultiplier: double;
  400. const SizePrecision = 50;
  401. var
  402. r, factor: Double;
  403. i: Integer;
  404. begin
  405. if m = 0 then exit(0);
  406. factor := 2*Pi/m/SizePrecision;
  407. r := 0;
  408. for i := 0 to SizePrecision-1 do
  409. begin
  410. r += ComputeR(i * factor, a, b, m, n1, n2, n3);
  411. end;
  412. r /= SizePrecision;
  413. result := r;
  414. end;
  415. procedure TSuperformulaOriginal.Render(ADest: TBGRABitmap;
  416. AMatrix: TAffineMatrix; ADraft: boolean);
  417. var
  418. backPoints, penOutlinePoints: ArrayOfTPointF;
  419. begin
  420. try
  421. GetCurve(AMatrix, backPoints, penOutlinePoints);
  422. if ADraft then
  423. begin
  424. if backPoints <> nil then
  425. ADest.FillPoly(backPoints, FBackColor, dmDrawWithTransparency, false);
  426. if penOutlinePoints <> nil then
  427. ADest.FillPoly(penOutlinePoints, FPenColor, dmDrawWithTransparency, false);
  428. end else
  429. begin
  430. if backPoints <> nil then
  431. ADest.FillPolyAntialias(backPoints, FBackColor, false);
  432. if penOutlinePoints <> nil then
  433. ADest.FillPolyAntialias(penOutlinePoints, FPenColor, false);
  434. end;
  435. except
  436. // ignore exceptions
  437. end;
  438. end;
  439. function TSuperformulaOriginal.GetRenderBounds(ADestRect: TRect;
  440. AMatrix: TAffineMatrix): TRect;
  441. var
  442. backPoints, penOutlinePoints: ArrayOfTPointF;
  443. resultF: TRectF;
  444. ptF: TPointF;
  445. begin
  446. GetCurve(AMatrix, backPoints, penOutlinePoints);
  447. if (backPoints = nil) and (penOutlinePoints = nil) then
  448. exit(EmptyRect);
  449. resultF.Left := MaxSingle;
  450. resultF.Top := MaxSingle;
  451. resultF.Right := -MaxSingle;
  452. resultF.Bottom := -MaxSingle;
  453. if backPoints <> nil then
  454. for ptF in backPoints do resultF.Include(ptF);
  455. if penOutlinePoints <> nil then
  456. for ptF in penOutlinePoints do resultF.Include(ptF);
  457. result.Left := floor(resultF.Left);
  458. result.Top := floor(resultF.Top);
  459. result.Right := ceil(resultF.Right);
  460. result.Bottom := ceil(resultF.Bottom);
  461. end;
  462. procedure TSuperformulaOriginal.GetMFraction(out ANumerator,
  463. ADenominator: integer);
  464. begin
  465. FloatToFraction(m, ANumerator, ADenominator, MaxDenominator);
  466. end;
  467. procedure TSuperformulaOriginal.LoadFromStorage(
  468. AStorage: TBGRACustomOriginalStorage);
  469. begin
  470. FSpikeOverlap:= AStorage.BoolDef['spike-overlap', false];
  471. Fa := AStorage.Float['a'];
  472. Fb := AStorage.Float['b'];
  473. Fm := AStorage.Float['m'];
  474. FMRational:= AStorage.BoolDef['m-rational', true];
  475. Fn1 := AStorage.Float['n1'];
  476. Fn2 := AStorage.Float['n2'];
  477. Fn3 := AStorage.Float['n3'];
  478. FMultiplier := AStorage.Float['multiplier'];
  479. FLineWidth := AStorage.Float['line-width'];
  480. FPenColor := AStorage.Color['pen-color'];
  481. FBackColor := AStorage.Color['back-color'];
  482. end;
  483. procedure TSuperformulaOriginal.SaveToStorage(
  484. AStorage: TBGRACustomOriginalStorage);
  485. begin
  486. AStorage.Bool['spike-overlap'] := FSpikeOverlap;
  487. AStorage.Float['a'] := Fa;
  488. AStorage.Float['b'] := Fb;
  489. AStorage.Float['m'] := Fm;
  490. AStorage.Bool['m-rational'] := FMRational;
  491. AStorage.Float['n1'] := Fn1;
  492. AStorage.Float['n2'] := Fn2;
  493. AStorage.Float['n3'] := Fn3;
  494. AStorage.Float['multiplier'] := FMultiplier;
  495. AStorage.Float['line-width'] := FLineWidth;
  496. AStorage.Color['pen-color'] := FPenColor;
  497. AStorage.Color['back-color'] := FBackColor;
  498. end;
  499. class function TSuperformulaOriginal.StorageClassName: RawByteString;
  500. begin
  501. Result := 'superformula';
  502. end;
  503. initialization
  504. RegisterLayerOriginal(TSuperformulaOriginal);
  505. end.