-
Notifications
You must be signed in to change notification settings - Fork 35
/
make_images.py
1933 lines (1668 loc) · 57.9 KB
/
make_images.py
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
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Helper to make images that are intended for docs.
To actually execute these functions with the desired inputs, run:
.. code-block:: console
$ nox -s docs_images
"""
import os
try:
from matplotlib import patches
from matplotlib import path as _path_mod
import matplotlib.pyplot as plt
except ImportError:
patches = None
_path_mod = None
plt = None
import numpy as np
try:
import seaborn
except ImportError:
seaborn = None
import bezier
from bezier import _geometric_intersection
from bezier import _helpers
from bezier import _plot_helpers
from bezier.hazmat import clipping
from bezier.hazmat import geometric_intersection as _py_geometric_intersection
BLUE = "blue"
GREEN = "green"
RED = "red"
if seaborn is not None:
seaborn.set() # Required in ``seaborn >= 0.8``
# As of ``0.9.0``, this palette has
# (BLUE, ORANGE, GREEN, RED, PURPLE, BROWN).
_COLORS = seaborn.color_palette(palette="deep", n_colors=6)
BLUE = _COLORS[0]
GREEN = _COLORS[2]
RED = _COLORS[3]
del _COLORS
_DOCS_DIR = os.path.abspath(os.path.dirname(__file__))
IMAGES_DIR = os.path.join(_DOCS_DIR, "images")
NO_IMAGES = "GENERATE_IMAGES" not in os.environ
def save_image(figure, filename):
"""Save an image to the docs images directory.
Args:
filename (str): The name of the file (not containing
directory info).
"""
path = os.path.join(IMAGES_DIR, filename)
figure.savefig(path, bbox_inches="tight")
plt.close(figure)
def stack1d(*points):
"""Fill out the columns of matrix with a series of points.
This is because ``np.hstack()`` will just make another 1D vector
out of them and ``np.vstack()`` will put them in the rows.
Args:
points (Tuple[numpy.ndarray, ...]): Tuple of 1D points (i.e.
arrays with shape ``(2,)``.
Returns:
numpy.ndarray: The array with each point in ``points`` as its
columns.
"""
result = np.empty((2, len(points)), order="F")
for index, point in enumerate(points):
result[:, index] = point
return result
def linearization_error(nodes):
"""Image for :func:`.linearization_error` docstring."""
if NO_IMAGES:
return
curve = bezier.Curve.from_nodes(nodes)
line = bezier.Curve.from_nodes(nodes[:, (0, -1)])
midpoints = np.hstack([curve.evaluate(0.5), line.evaluate(0.5)])
ax = curve.plot(256, color=BLUE)
line.plot(256, ax=ax, color=GREEN)
ax.plot(
midpoints[0, :], midpoints[1, :], color="black", linestyle="dashed"
)
ax.axis("scaled")
save_image(ax.figure, "linearization_error.png")
def newton_refine1(s, new_s, curve1, t, new_t, curve2):
"""Image for :func:`.newton_refine` docstring."""
if NO_IMAGES:
return
points = np.hstack([curve1.evaluate(s), curve2.evaluate(t)])
points_new = np.hstack([curve1.evaluate(new_s), curve2.evaluate(new_t)])
ax = curve1.plot(256, color=BLUE)
curve2.plot(256, ax=ax, color=GREEN)
ax.plot(
points[0, :],
points[1, :],
color="black",
linestyle="None",
marker="o",
markeredgewidth=1,
markerfacecolor="None",
)
ax.plot(
points_new[0, :],
points_new[1, :],
color="black",
linestyle="None",
marker="o",
)
ax.axis("scaled")
save_image(ax.figure, "newton_refine1.png")
def newton_refine2(s_vals, curve1, curve2):
"""Image for :func:`.newton_refine` docstring."""
if NO_IMAGES:
return
ax = curve1.plot(256, color=BLUE)
ax.lines[-1].zorder = 1
curve2.plot(256, ax=ax, color=GREEN)
ax.lines[-1].zorder = 1
points = curve1.evaluate_multi(np.asfortranarray(s_vals))
colors = seaborn.dark_palette("blue", 5)
ax.scatter(
points[0, :], points[1, :], c=colors, s=20, alpha=0.75, zorder=2
)
ax.axis("scaled")
ax.set_xlim(0.0, 1.0)
ax.set_ylim(0.0, 1.0)
save_image(ax.figure, "newton_refine2.png")
def newton_refine3(s_vals, curve1, curve2):
"""Image for :func:`.newton_refine` docstring."""
if NO_IMAGES:
return
ax = curve1.plot(256, color=BLUE)
ax.lines[-1].zorder = 1
curve2.plot(256, ax=ax, color=GREEN)
ax.lines[-1].zorder = 1
points = curve1.evaluate_multi(np.asfortranarray(s_vals))
colors = seaborn.dark_palette("blue", 6)
ax.scatter(
points[0, :], points[1, :], c=colors, s=20, alpha=0.75, zorder=2
)
ax.axis("scaled")
ax.set_xlim(0.0, 1.0)
ax.set_ylim(0.0, 0.5625)
save_image(ax.figure, "newton_refine3.png")
def segment_intersection1(start0, end0, start1, end1, s):
"""Image for :func:`.segment_intersection` docstring."""
if NO_IMAGES:
return
line0 = bezier.Curve.from_nodes(stack1d(start0, end0))
line1 = bezier.Curve.from_nodes(stack1d(start1, end1))
ax = line0.plot(2, color=BLUE)
line1.plot(256, ax=ax, color=GREEN)
(x_val,), (y_val,) = line0.evaluate(s)
ax.plot([x_val], [y_val], color="black", marker="o")
ax.axis("scaled")
save_image(ax.figure, "segment_intersection1.png")
def segment_intersection2(start0, end0, start1, end1):
"""Image for :func:`.segment_intersection` docstring."""
if NO_IMAGES:
return
line0 = bezier.Curve.from_nodes(stack1d(start0, end0))
line1 = bezier.Curve.from_nodes(stack1d(start1, end1))
ax = line0.plot(2, color=BLUE)
line1.plot(2, ax=ax, color=GREEN)
ax.axis("scaled")
save_image(ax.figure, "segment_intersection2.png")
def helper_parallel_lines(start0, end0, start1, end1, filename):
"""Image for :func:`.parallel_lines_parameters` docstring."""
if NO_IMAGES:
return
figure = plt.figure()
ax = figure.gca()
points = stack1d(start0, end0, start1, end1)
ax.plot(points[0, :2], points[1, :2], marker="o", color=BLUE)
ax.plot(points[0, 2:], points[1, 2:], marker="o", color=GREEN)
ax.axis("scaled")
_plot_helpers.add_plot_boundary(ax)
save_image(figure, filename)
def add_patch(
ax, nodes, color, with_nodes=True, alpha=0.625, node_color="black"
):
# ``nodes`` is stored Fortran-contiguous with ``x-y`` points in each
# column but ``Path()`` wants ``x-y`` points in each row.
path = _path_mod.Path(nodes.T)
patch = patches.PathPatch(
path, edgecolor=color, facecolor=color, alpha=alpha
)
ax.add_patch(patch)
if with_nodes:
ax.plot(
nodes[0, :],
nodes[1, :],
color=node_color,
linestyle="None",
marker="o",
)
def curve_constructor(curve):
"""Image for :class`.Curve` docstring."""
if NO_IMAGES:
return
ax = curve.plot(256, color=BLUE)
line = ax.lines[0]
nodes = curve._nodes
ax.plot(
nodes[0, :], nodes[1, :], color="black", linestyle="None", marker="o"
)
add_patch(ax, nodes, line.get_color())
ax.axis("scaled")
ax.set_xlim(-0.125, 1.125)
ax.set_ylim(-0.0625, 0.5625)
save_image(ax.figure, "curve_constructor.png")
def curve_evaluate(curve):
"""Image for :meth`.Curve.evaluate` docstring."""
if NO_IMAGES:
return
ax = curve.plot(256, color=BLUE)
points = curve.evaluate_multi(np.asfortranarray([0.75]))
ax.plot(
points[0, :], points[1, :], color="black", linestyle="None", marker="o"
)
ax.axis("scaled")
ax.set_xlim(-0.125, 1.125)
ax.set_ylim(-0.0625, 0.5625)
save_image(ax.figure, "curve_evaluate.png")
def curve_evaluate_hodograph(curve, s):
"""Image for :meth`.Curve.evaluate_hodograph` docstring."""
if NO_IMAGES:
return
ax = curve.plot(256, color=BLUE)
points = curve.evaluate_multi(np.asfortranarray([s]))
if points.shape != (2, 1):
raise ValueError("Unexpected shape", points)
point = points[:, 0]
tangents = curve.evaluate_hodograph(s)
if tangents.shape != (2, 1):
raise ValueError("Unexpected shape", tangents)
tangent = tangents[:, 0]
ax.plot(
[point[0] - 2 * tangent[0], point[0] + 2 * tangent[0]],
[point[1] - 2 * tangent[1], point[1] + 2 * tangent[1]],
color=BLUE,
alpha=0.5,
)
ax.plot(
[point[0], point[0] + tangent[0]],
[point[1], point[1] + tangent[1]],
color="black",
linestyle="dashed",
marker="o",
markersize=5,
)
ax.axis("scaled")
ax.set_xlim(-0.125, 1.75)
ax.set_ylim(-0.0625, 0.75)
save_image(ax.figure, "curve_evaluate_hodograph.png")
def curve_subdivide(curve, left, right):
"""Image for :meth`.Curve.subdivide` docstring."""
if NO_IMAGES:
return
figure = plt.figure()
ax = figure.gca()
add_patch(ax, curve._nodes, "gray")
ax = left.plot(256, ax=ax, color=BLUE)
line = ax.lines[-1]
add_patch(ax, left._nodes, line.get_color())
right.plot(256, ax=ax, color=GREEN)
line = ax.lines[-1]
add_patch(ax, right._nodes, line.get_color())
ax.axis("scaled")
ax.set_xlim(-0.125, 2.125)
ax.set_ylim(-0.125, 3.125)
save_image(ax.figure, "curve_subdivide.png")
def curve_intersect(curve1, curve2, s_vals):
"""Image for :meth`.Curve.intersect` docstring."""
if NO_IMAGES:
return
ax = curve1.plot(256, color=BLUE)
curve2.plot(256, ax=ax, color=GREEN)
intersections = curve1.evaluate_multi(s_vals)
ax.plot(
intersections[0, :],
intersections[1, :],
color="black",
linestyle="None",
marker="o",
)
ax.axis("scaled")
ax.set_xlim(0.0, 0.75)
ax.set_ylim(0.0, 0.75)
save_image(ax.figure, "curve_intersect.png")
def triangle_constructor(triangle):
"""Image for :class`.Triangle` docstring."""
if NO_IMAGES:
return
ax = triangle.plot(256, color=BLUE, with_nodes=True)
line = ax.lines[0]
nodes = triangle._nodes
add_patch(ax, nodes[:, (0, 1, 2, 5)], line.get_color())
delta = 1.0 / 32.0
ax.text(
nodes[0, 0],
nodes[1, 0],
r"$v_0$",
fontsize=20,
verticalalignment="top",
horizontalalignment="right",
)
ax.text(
nodes[0, 1],
nodes[1, 1],
r"$v_1$",
fontsize=20,
verticalalignment="top",
horizontalalignment="center",
)
ax.text(
nodes[0, 2],
nodes[1, 2],
r"$v_2$",
fontsize=20,
verticalalignment="top",
horizontalalignment="left",
)
ax.text(
nodes[0, 3] - delta,
nodes[1, 3],
r"$v_3$",
fontsize=20,
verticalalignment="center",
horizontalalignment="right",
)
ax.text(
nodes[0, 4] + delta,
nodes[1, 4],
r"$v_4$",
fontsize=20,
verticalalignment="center",
horizontalalignment="left",
)
ax.text(
nodes[0, 5],
nodes[1, 5] + delta,
r"$v_5$",
fontsize=20,
verticalalignment="bottom",
horizontalalignment="center",
)
ax.axis("scaled")
ax.set_xlim(-0.125, 1.125)
ax.set_ylim(-0.125, 1.125)
save_image(ax.figure, "triangle_constructor.png")
def triangle_evaluate_barycentric(triangle, point):
"""Image for :meth`.Triangle.evaluate_barycentric` docstring."""
if NO_IMAGES:
return
ax = triangle.plot(256, color=BLUE)
ax.plot(
point[0, :], point[1, :], color="black", linestyle="None", marker="o"
)
ax.axis("scaled")
ax.set_xlim(-0.125, 1.125)
ax.set_ylim(-0.125, 1.125)
save_image(ax.figure, "triangle_evaluate_barycentric.png")
def triangle_evaluate_cartesian_multi(triangle, points):
"""Image for :meth`.Triangle.evaluate_cartesian_multi` docstring."""
if NO_IMAGES:
return
ax = triangle.plot(256, color=BLUE)
ax.plot(
points[0, :], points[1, :], color="black", linestyle="None", marker="o"
)
delta = 1.0 / 32.0
font_size = 18
ax.text(
points[0, 0],
points[1, 0],
r"$w_0$",
fontsize=font_size,
verticalalignment="top",
horizontalalignment="right",
)
ax.text(
points[0, 1] + 2 * delta,
points[1, 1],
r"$w_1$",
fontsize=font_size,
verticalalignment="center",
horizontalalignment="left",
)
ax.text(
points[0, 2],
points[1, 2] + delta,
r"$w_2$",
fontsize=font_size,
verticalalignment="bottom",
horizontalalignment="left",
)
ax.axis("scaled")
ax.set_xlim(-3.125, 2.375)
ax.set_ylim(-0.25, 2.125)
save_image(ax.figure, "triangle_evaluate_cartesian_multi.png")
def triangle_evaluate_barycentric_multi(triangle, points):
"""Image for :meth`.Triangle.evaluate_barycentric_multi` docstring."""
if NO_IMAGES:
return
ax = triangle.plot(256, color=BLUE)
ax.plot(
points[0, :], points[1, :], color="black", linestyle="None", marker="o"
)
delta = 1.0 / 32.0
font_size = 18
ax.text(
points[0, 0],
points[1, 0] + delta,
r"$w_0$",
fontsize=font_size,
verticalalignment="bottom",
horizontalalignment="center",
)
ax.text(
points[0, 1],
points[1, 1] - delta,
r"$w_1$",
fontsize=font_size,
verticalalignment="top",
horizontalalignment="right",
)
ax.text(
points[0, 2],
points[1, 2],
r"$w_2$",
fontsize=font_size,
verticalalignment="bottom",
horizontalalignment="left",
)
ax.text(
points[0, 3],
points[1, 3],
r"$w_3$",
fontsize=font_size,
verticalalignment="top",
horizontalalignment="right",
)
ax.axis("scaled")
ax.set_xlim(-3.125, 2.125)
ax.set_ylim(-0.3125, 2.125)
save_image(ax.figure, "triangle_evaluate_barycentric_multi.png")
def triangle_is_valid1(triangle):
"""Image for :meth`.Triangle.is_valid` docstring."""
if NO_IMAGES:
return
ax = triangle.plot(256, color=BLUE)
ax.axis("scaled")
ax.set_xlim(-0.125, 2.125)
ax.set_ylim(-0.125, 2.125)
save_image(ax.figure, "triangle_is_valid1.png")
def triangle_is_valid2(triangle):
"""Image for :meth`.Triangle.is_valid` docstring."""
if NO_IMAGES:
return
ax = triangle.plot(256, color=BLUE)
ax.axis("scaled")
ax.set_xlim(-0.125, 1.0625)
ax.set_ylim(-0.0625, 1.0625)
save_image(ax.figure, "triangle_is_valid2.png")
def triangle_is_valid3(triangle):
"""Image for :meth`.Triangle.is_valid` docstring."""
if NO_IMAGES:
return
edge1, edge2, edge3 = triangle.edges
N = 128
# Compute points on each edge.
std_s = np.linspace(0.0, 1.0, N + 1)
points1 = edge1.evaluate_multi(std_s)
points2 = edge2.evaluate_multi(std_s)
points3 = edge3.evaluate_multi(std_s)
# Compute the actual boundary where the Jacobian is 0.
s_vals = np.linspace(0.0, 0.2, N)
t_discrim = np.sqrt((1.0 - s_vals) * (1.0 - 5.0 * s_vals))
t_top = 0.5 * (1.0 - s_vals + t_discrim)
t_bottom = 0.5 * (1.0 - s_vals - t_discrim)
jacobian_zero_params = np.zeros((2 * N - 1, 2), order="F")
jacobian_zero_params[:N, 0] = s_vals
jacobian_zero_params[:N, 1] = t_top
jacobian_zero_params[N:, 0] = s_vals[-2::-1]
jacobian_zero_params[N:, 1] = t_bottom[-2::-1]
jac_edge = triangle.evaluate_cartesian_multi(jacobian_zero_params)
# Add the triangle to the plot and add a dashed line
# for each "true" edge.
figure = plt.figure()
ax = figure.gca()
(line,) = ax.plot(jac_edge[0, :], jac_edge[1, :], color=BLUE)
color = line.get_color()
ax.plot(points1[0, :], points1[1, :], color="black", linestyle="dashed")
ax.plot(points2[0, :], points2[1, :], color="black", linestyle="dashed")
ax.plot(points3[0, :], points3[1, :], color="black", linestyle="dashed")
polygon = np.hstack([points1[:, 1:], points2[:, 1:], jac_edge[:, 1:]])
add_patch(ax, polygon, color, with_nodes=False)
ax.axis("scaled")
ax.set_xlim(-0.0625, 1.0625)
ax.set_ylim(-0.0625, 1.0625)
save_image(ax.figure, "triangle_is_valid3.png")
def triangle_subdivide1():
"""Image for :meth`.Triangle.subdivide` docstring."""
if NO_IMAGES:
return
triangle = bezier.Triangle.from_nodes(
np.asfortranarray([[0.0, 1.0, 0.0], [0.0, 0.0, 1.0]])
)
triangle_a, triangle_b, triangle_c, triangle_d = triangle.subdivide()
figure, ((ax1, ax2), (ax3, ax4)) = plt.subplots(2, 2)
for ax in (ax1, ax2, ax3, ax4):
triangle.plot(2, ax=ax, color=BLUE)
triangle_a.plot(2, ax=ax1, color=GREEN)
ax1.text(
1.0 / 6.0,
1.0 / 6.0,
r"$A$",
fontsize=20,
verticalalignment="center",
horizontalalignment="center",
)
triangle_b.plot(2, ax=ax2, color=GREEN)
ax2.text(
1.0 / 3.0,
1.0 / 3.0,
r"$B$",
fontsize=20,
verticalalignment="center",
horizontalalignment="center",
)
triangle_c.plot(2, ax=ax3, color=GREEN)
ax3.text(
2.0 / 3.0,
1.0 / 6.0,
r"$C$",
fontsize=20,
verticalalignment="center",
horizontalalignment="center",
)
triangle_d.plot(2, ax=ax4, color=GREEN)
ax4.text(
1.0 / 6.0,
2.0 / 3.0,
r"$D$",
fontsize=20,
verticalalignment="center",
horizontalalignment="center",
)
for ax in (ax1, ax2, ax3, ax4):
ax.axis("scaled")
save_image(figure, "triangle_subdivide1")
def add_edges(ax, triangle, s_vals, color):
edge1, edge2, edge3 = triangle.edges
# Compute points on each edge.
points1 = edge1.evaluate_multi(s_vals)
points2 = edge2.evaluate_multi(s_vals)
points3 = edge3.evaluate_multi(s_vals)
# Add the points to the plot.
ax.plot(points1[0, :], points1[1, :], color=color)
ax.plot(points2[0, :], points2[1, :], color=color)
ax.plot(points3[0, :], points3[1, :], color=color)
def triangle_subdivide2(triangle, sub_triangle_b):
"""Image for :meth`.Triangle.subdivide` docstring."""
if NO_IMAGES:
return
# Plot set-up.
figure = plt.figure()
ax = figure.gca()
colors = seaborn.husl_palette(6)
N = 128
s_vals = np.linspace(0.0, 1.0, N + 1)
# Add edges from triangle.
add_edges(ax, triangle, s_vals, colors[4])
# Now do the same for triangle B.
add_edges(ax, sub_triangle_b, s_vals, colors[0])
# Add the control points polygon for the original triangle.
nodes = triangle._nodes[:, (0, 2, 4, 5, 0)]
add_patch(ax, nodes, colors[2], with_nodes=False)
# Add the control points polygon for the sub-triangle.
nodes = sub_triangle_b._nodes[:, (0, 1, 2, 5, 3, 0)]
add_patch(ax, nodes, colors[1], with_nodes=False)
# Plot **all** the nodes.
sub_nodes = sub_triangle_b._nodes
ax.plot(
sub_nodes[0, :],
sub_nodes[1, :],
color="black",
linestyle="None",
marker="o",
)
# Take those same points and add the boundary.
ax.plot(nodes[0, :], nodes[1, :], color="black", linestyle="dashed")
ax.axis("scaled")
ax.set_xlim(-1.125, 2.125)
ax.set_ylim(-0.125, 4.125)
save_image(ax.figure, "triangle_subdivide2")
def curved_polygon_constructor1(curved_poly):
"""Image for :class`.CurvedPolygon` docstring."""
if NO_IMAGES:
return
ax = curved_poly.plot(256, color=BLUE)
ax.axis("scaled")
ax.set_xlim(-0.125, 2.125)
ax.set_ylim(-0.625, 1.625)
save_image(ax.figure, "curved_polygon_constructor1.png")
def curved_polygon_constructor2(curved_poly):
"""Image for :class`.CurvedPolygon` docstring."""
if NO_IMAGES:
return
ax = curved_poly.plot(256, color=BLUE)
ax.axis("scaled")
ax.set_xlim(-0.125, 2.125)
ax.set_ylim(-0.125, 1.125)
save_image(ax.figure, "curved_polygon_constructor2.png")
def triangle_locate(triangle, point):
"""Image for :meth`.Triangle.locate` docstring."""
if NO_IMAGES:
return
ax = triangle.plot(256, color=BLUE)
ax.plot(
point[0, :], point[1, :], color="black", linestyle="None", marker="o"
)
ax.axis("scaled")
ax.set_xlim(-0.0625, 1.0625)
ax.set_ylim(-0.1875, 1.0625)
save_image(ax.figure, "triangle_locate.png")
def curve_specialize(curve, new_curve):
"""Image for :meth`.Curve.specialize` docstring."""
if NO_IMAGES:
return
ax = curve.plot(256, color=BLUE)
interval = r"$\left[0, 1\right]$"
line = ax.lines[-1]
line.set_label(interval)
color1 = line.get_color()
new_curve.plot(256, ax=ax, color=GREEN)
interval = r"$\left[-\frac{1}{4}, \frac{3}{4}\right]$"
line = ax.lines[-1]
line.set_label(interval)
ax.plot(
curve._nodes[0, (0, -1)],
curve._nodes[1, (0, -1)],
color=color1,
linestyle="None",
marker="o",
)
ax.plot(
new_curve._nodes[0, (0, -1)],
new_curve._nodes[1, (0, -1)],
color=line.get_color(),
linestyle="None",
marker="o",
)
ax.legend(loc="lower right", fontsize=12)
ax.axis("scaled")
ax.set_xlim(-0.375, 1.125)
ax.set_ylim(-0.75, 0.625)
save_image(ax.figure, "curve_specialize.png")
def newton_refine_triangle(triangle, x_val, y_val, s, t, new_s, new_t):
"""Image for :func:`.hazmat.triangle_helpers.newton_refine` docstring."""
if NO_IMAGES:
return
figure, (ax1, ax2) = plt.subplots(1, 2)
# Plot features of the parameter space in ax1.
linear_triangle = bezier.Triangle.from_nodes(
np.asfortranarray([[0.0, 1.0, 0.0], [0.0, 0.0, 1.0]])
)
linear_triangle.plot(2, ax=ax1, color=BLUE)
ax1.plot([0.25], [0.5], marker="H", color=GREEN)
ax1.plot([s], [t], color="black", linestyle="None", marker="o")
ax1.plot(
[new_s],
[new_t],
color="black",
linestyle="None",
marker="o",
markeredgewidth=1,
markerfacecolor="None",
)
# Plot the equivalent output in ax2.
triangle.plot(256, ax=ax2, color=BLUE)
points = triangle.evaluate_cartesian_multi(
np.asfortranarray([[s, t], [new_s, new_t]])
)
ax2.plot([x_val], [y_val], marker="H", color=GREEN)
ax2.plot(
points[0, [0]],
points[1, [0]],
color="black",
linestyle="None",
marker="o",
)
ax2.plot(
points[0, [1]],
points[1, [1]],
color="black",
linestyle="None",
marker="o",
markeredgewidth=1,
markerfacecolor="None",
)
# Set the axis bounds / scaling.
ax1.axis("scaled")
ax1.set_xlim(-0.0625, 1.0625)
ax1.set_ylim(-0.0625, 1.0625)
ax2.axis("scaled")
ax2.set_xlim(-0.125, 2.125)
ax2.set_ylim(-0.125, 2.125)
save_image(figure, "newton_refine_triangle.png")
def classify_help(s, curve1, triangle1, curve2, triangle2, interior, ax=None):
assert triangle1.is_valid
edge1, _, _ = triangle1.edges
assert np.all(edge1._nodes == curve1._nodes)
assert triangle2.is_valid
edge2, _, _ = triangle2.edges
assert np.all(edge2._nodes == curve2._nodes)
ax = triangle1.plot(256, ax=ax, color=BLUE)
# Manually reduce the alpha on the triangle patch(es).
ax.patches[-1].set_alpha(0.1875)
color1 = ax.lines[-1].get_color()
triangle2.plot(256, ax=ax, color=GREEN)
ax.patches[-1].set_alpha(0.1875)
color2 = ax.lines[-1].get_color()
# Remove the existing boundary (lines) and just add our edges.
while ax.lines:
ax.lines[-1].remove()
edge1.plot(256, ax=ax, color=color1)
edge2.plot(256, ax=ax, color=color2)
(int_x,), (int_y,) = curve1.evaluate(s)
if interior == 0:
color = color1
elif interior == 1:
color = color2
else:
color = RED
ax.plot([int_x], [int_y], color=color, linestyle="None", marker="o")
ax.axis("scaled")
return ax
def classify_intersection1(s, curve1, tangent1, curve2, tangent2):
"""Image for :func:`.hazmat.triangle_helpers.classify_intersection` doc."""
if NO_IMAGES:
return
triangle1 = bezier.Triangle.from_nodes(
np.asfortranarray(
[[1.0, 1.75, 2.0, 1.0, 1.5, 1.0], [0.0, 0.25, 1.0, 1.0, 1.5, 2.0]]
)
)
triangle2 = bezier.Triangle.from_nodes(
np.asfortranarray(
[
[0.0, 1.6875, 2.0, 0.25, 1.25, 0.5],
[0.0, 0.0625, 0.5, 1.0, 1.25, 2.0],
]
)
)
ax = classify_help(s, curve1, triangle1, curve2, triangle2, 0)
(int_x,), (int_y,) = curve1.evaluate(s)
# Remove the alpha from the color
color1 = ax.patches[0].get_facecolor()[:3]
color2 = ax.patches[1].get_facecolor()[:3]
ax.plot(
[int_x, int_x + tangent1[0, 0]],
[int_y, int_y + tangent1[1, 0]],
color=color1,
linestyle="dashed",
)
ax.plot(
[int_x, int_x + tangent2[0, 0]],
[int_y, int_y + tangent2[1, 0]],
color=color2,
linestyle="dashed",
)
ax.plot([int_x], [int_y], color=color1, linestyle="None", marker="o")
ax.axis("scaled")
ax.set_xlim(-0.125, 2.125)
ax.set_ylim(-0.125, 1.125)
save_image(ax.figure, "classify_intersection1.png")
def classify_intersection2(s, curve1, curve2):
"""Image for :func:`.hazmat.triangle_helpers.classify_intersection` doc."""
if NO_IMAGES:
return
triangle1 = bezier.Triangle.from_nodes(
np.asfortranarray(
[[1.0, 1.5, 2.0, 1.25, 1.75, 1.5], [0.0, 1.0, 0.0, 1.0, 1.0, 2.0]]
)
)
triangle2 = bezier.Triangle.from_nodes(
np.asfortranarray(
[[0.0, 1.5, 3.0, 0.75, 2.25, 1.5], [0.0, 1.0, 0.0, 2.0, 2.0, 4.0]]
)
)
ax = classify_help(s, curve1, triangle1, curve2, triangle2, 1)
ax.set_xlim(-0.0625, 3.0625)
ax.set_ylim(-0.0625, 0.5625)
save_image(ax.figure, "classify_intersection2.png")
def classify_intersection3(s, curve1, curve2):
"""Image for :func:`.hazmat.triangle_helpers.classify_intersection` doc."""
if NO_IMAGES:
return
triangle1 = bezier.Triangle.from_nodes(
np.asfortranarray(
[
[2.0, 1.5, 1.0, 1.75, 1.25, 1.5],
[0.0, 1.0, 0.0, -1.0, -1.0, -2.0],
]
)
)
triangle2 = bezier.Triangle.from_nodes(
np.asfortranarray(
[
[3.0, 1.5, 0.0, 2.25, 0.75, 1.5],
[0.0, 1.0, 0.0, -2.0, -2.0, -4.0],
]
)
)
ax = classify_help(s, curve1, triangle1, curve2, triangle2, 0)
ax.set_xlim(-0.0625, 3.0625)
ax.set_ylim(-0.0625, 0.5625)
save_image(ax.figure, "classify_intersection3.png")
def classify_intersection4(s, curve1, curve2):
"""Image for :func:`.hazmat.triangle_helpers.classify_intersection` doc."""
if NO_IMAGES:
return
triangle1 = bezier.Triangle.from_nodes(
np.asfortranarray(
[
[2.0, 1.5, 1.0, 1.75, 1.25, 1.5],
[0.0, 1.0, 0.0, -1.0, -1.0, -2.0],
]
)
)
triangle2 = bezier.Triangle.from_nodes(
np.asfortranarray(
[[0.0, 1.5, 3.0, 0.75, 2.25, 1.5], [0.0, 1.0, 0.0, 2.0, 2.0, 4.0]]
)
)
ax = classify_help(s, curve1, triangle1, curve2, triangle2, None)
ax.set_xlim(-0.0625, 3.0625)
ax.set_ylim(-0.0625, 0.5625)
save_image(ax.figure, "classify_intersection4.png")
def classify_intersection5(s, curve1, curve2):
"""Image for :func:`.hazmat.triangle_helpers.classify_intersection` doc."""
if NO_IMAGES:
return
triangle1 = bezier.Triangle.from_nodes(
np.asfortranarray(
[
[1.0, 1.5, 2.0, 1.25, 1.75, 1.5],
[0.0, 1.0, 0.0, 0.9375, 0.9375, 1.875],
]
)
)
triangle2 = bezier.Triangle.from_nodes(
np.asfortranarray(
[
[3.0, 1.5, 0.0, 2.25, 0.75, 1.5],
[0.0, 1.0, 0.0, -2.0, -2.0, -4.0],
]
)
)
figure, (ax1, ax2) = plt.subplots(2, 1)
classify_help(s, curve1, triangle1, curve2, triangle2, 0, ax=ax1)
classify_help(s, curve1, triangle1, curve2, triangle2, 1, ax=ax2)
# Remove the alpha from the color
color1 = ax1.patches[0].get_facecolor()[:3]
color2 = ax1.patches[1].get_facecolor()[:3]
# Now add the "degenerate" intersection polygons. The first
# comes from specializing to
# left1(0.5, 1.0)-left2(0.0, 0.25)-right1(0.375, 0.5)
triangle3 = bezier.Triangle.from_nodes(
np.asfortranarray(
[
[1.5, 1.75, 2.0, 1.6875, 1.9375, 1.875],