-
-
Notifications
You must be signed in to change notification settings - Fork 395
/
index_face_set.pyx
1629 lines (1363 loc) · 57.6 KB
/
index_face_set.pyx
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
"""
Indexed Face Sets
Graphics3D object that consists of a list of polygons, also used for
triangulations of other objects.
Usually these objects are not created directly by users.
AUTHORS:
- Robert Bradshaw (2007-08-26): initial version
- Robert Bradshaw (2007-08-28): significant optimizations
.. TODO::
Smooth triangles using vertex normals
"""
# ****************************************************************************
# Copyright (C) 2007 Robert Bradshaw <robertwb@math.washington.edu>
#
# Distributed under the terms of the GNU General Public License (GPL)
#
# This code is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# The full text of the GPL is available at:
#
# https://www.gnu.org/licenses/
# ****************************************************************************
from textwrap import dedent
from sage.misc.superseded import deprecation
from libc.math cimport isfinite, INFINITY
from libc.string cimport memset, memcpy
from cysignals.memory cimport check_calloc, check_allocarray, check_reallocarray, sig_free
from cysignals.signals cimport sig_check, sig_on, sig_off
cdef extern from *:
int sprintf_3d "sprintf" (char*, char*, double, double, double)
int sprintf_3i "sprintf" (char*, char*, int, int, int)
int sprintf_4i "sprintf" (char*, char*, int, int, int, int)
int sprintf_5i "sprintf" (char*, char*, int, int, int, int, int)
int sprintf_6i "sprintf" (char*, char*, int, int, int, int, int, int)
int sprintf_7i "sprintf" (char*, char*, int, int, int, int, int, int, int)
int sprintf_9d "sprintf" (char*, char*, double, double, double, double, double, double, double, double, double)
from cpython.list cimport *
from cpython.bytes cimport *
include "point_c.pxi"
from math import sin, cos, sqrt
from random import randint
from sage.cpython.string cimport bytes_to_str
from sage.rings.real_double import RDF
from sage.matrix.constructor import matrix
from sage.modules.free_module_element import vector
from sage.plot.colors import Color, float_to_integer
from sage.plot.plot3d.base import Graphics3dGroup
from sage.plot.plot3d.texture import Texture
from .transform cimport Transformation
# --------------------------------------------------------------------
# Fast routines for generating string representations of the polygons.
# --------------------------------------------------------------------
cdef inline format_tachyon_texture(color_c rgb):
cdef char rs[200]
cdef Py_ssize_t cr = sprintf_3d(rs,
"TEXTURE\n AMBIENT 0.3 DIFFUSE 0.7 SPECULAR 0 OPACITY 1.0\n COLOR %g %g %g \n TEXFUNC 0",
rgb.r, rgb.g, rgb.b)
return bytes_to_str(PyBytes_FromStringAndSize(rs, cr))
cdef inline format_tachyon_triangle(point_c P, point_c Q, point_c R):
cdef char ss[250]
# PyBytes_FromFormat doesn't do floats?
cdef Py_ssize_t r = sprintf_9d(ss,
"TRI V0 %g %g %g V1 %g %g %g V2 %g %g %g",
P.x, P.y, P.z,
Q.x, Q.y, Q.z,
R.x, R.y, R.z )
return bytes_to_str(PyBytes_FromStringAndSize(ss, r))
cdef inline format_json_vertex(point_c P):
cdef char ss[100]
cdef Py_ssize_t r = sprintf_3d(ss, '{"x":%g,"y":%g,"z":%g}', P.x, P.y, P.z)
return bytes_to_str(PyBytes_FromStringAndSize(ss, r))
cdef inline format_json_face(face_c face):
s = "[{}]".format(",".join([str(face.vertices[i])
for i from 0 <= i < face.n]))
return s
cdef inline format_obj_vertex(point_c P):
cdef char ss[100]
# PyBytes_FromFormat doesn't do floats?
cdef Py_ssize_t r = sprintf_3d(ss, "v %g %g %g", P.x, P.y, P.z)
return bytes_to_str(PyBytes_FromStringAndSize(ss, r))
cdef inline format_obj_face(face_c face, int off):
cdef char ss[100]
cdef Py_ssize_t r, i
if face.n == 3:
r = sprintf_3i(ss, "f %d %d %d", face.vertices[0] + off, face.vertices[1] + off, face.vertices[2] + off)
elif face.n == 4:
r = sprintf_4i(ss, "f %d %d %d %d", face.vertices[0] + off, face.vertices[1] + off, face.vertices[2] + off, face.vertices[3] + off)
else:
return "f " + " ".join([str(face.vertices[i] + off) for i from 0 <= i < face.n])
# PyBytes_FromFormat is almost twice as slow
return bytes_to_str(PyBytes_FromStringAndSize(ss, r))
cdef inline format_obj_face_back(face_c face, int off):
cdef char ss[100]
cdef Py_ssize_t r, i
if face.n == 3:
r = sprintf_3i(ss, "f %d %d %d", face.vertices[2] + off, face.vertices[1] + off, face.vertices[0] + off)
elif face.n == 4:
r = sprintf_4i(ss, "f %d %d %d %d", face.vertices[3] + off, face.vertices[2] + off, face.vertices[1] + off, face.vertices[0] + off)
else:
return "f " + " ".join([str(face.vertices[i] + off) for i from face.n > i >= 0])
return bytes_to_str(PyBytes_FromStringAndSize(ss, r))
cdef inline format_pmesh_vertex(point_c P):
cdef char ss[100]
# PyBytes_FromFormat doesn't do floats?
cdef Py_ssize_t r = sprintf_3d(ss, "%g %g %g", P.x, P.y, P.z)
return bytes_to_str(PyBytes_FromStringAndSize(ss, r))
cdef inline format_pmesh_face(face_c face, int has_color):
cdef char ss[100]
cdef Py_ssize_t r, i
cdef int color
# if the face has an individual color, has_color is -1
# otherwise it is 1
if has_color == -1:
color = float_to_integer(face.color.r,
face.color.g,
face.color.b)
# it seems that Jmol does not like the 0 color at all
if color == 0:
color = 1
if face.n == 3:
if has_color == 1:
r = sprintf_5i(ss, "%d\n%d\n%d\n%d\n%d", has_color * 4,
face.vertices[0],
face.vertices[1],
face.vertices[2],
face.vertices[0])
else:
r = sprintf_6i(ss, "%d\n%d\n%d\n%d\n%d\n%d", has_color * 4,
face.vertices[0],
face.vertices[1],
face.vertices[2],
face.vertices[0], color)
elif face.n == 4:
if has_color == 1:
r = sprintf_6i(ss, "%d\n%d\n%d\n%d\n%d\n%d", has_color * 5,
face.vertices[0],
face.vertices[1],
face.vertices[2],
face.vertices[3],
face.vertices[0])
else:
r = sprintf_7i(ss, "%d\n%d\n%d\n%d\n%d\n%d\n%d", has_color * 5,
face.vertices[0],
face.vertices[1],
face.vertices[2],
face.vertices[3],
face.vertices[0], color)
else:
# Naive triangulation
all = []
if has_color == 1:
for i from 1 <= i < face.n - 1:
r = sprintf_5i(ss, "%d\n%d\n%d\n%d\n%d", has_color * 4,
face.vertices[0],
face.vertices[i],
face.vertices[i + 1],
face.vertices[0])
PyList_Append(all, PyBytes_FromStringAndSize(ss, r))
else:
for i from 1 <= i < face.n - 1:
r = sprintf_6i(ss, "%d\n%d\n%d\n%d\n%d\n%d", has_color * 4,
face.vertices[0],
face.vertices[i],
face.vertices[i + 1],
face.vertices[0], color)
PyList_Append(all, PyBytes_FromStringAndSize(ss, r))
return bytes_to_str(b"\n".join(all))
# PyBytes_FromFormat is almost twice as slow
return bytes_to_str(PyBytes_FromStringAndSize(ss, r))
def midpoint(pointa, pointb, w):
"""
Return the weighted mean of two points in 3-space.
INPUT:
- ``pointa``, ``pointb`` -- two points in 3-dimensional space
- ``w`` -- a real weight between 0 and 1.
If the weight is zero, the result is ``pointb``. If the weight is
one, the result is ``pointa``.
EXAMPLES::
sage: from sage.plot.plot3d.index_face_set import midpoint
sage: midpoint((1,2,3),(4,4,4),0.8)
(1.60000000000000, 2.40000000000000, 3.20000000000000)
"""
xa, ya, za = pointa
xb, yb, zb = pointb
v = 1 - w
return ((w * xa + v * xb), (w * ya + v * yb), (w * za + v * zb))
cdef class IndexFaceSet(PrimitiveObject):
"""
Graphics3D object that consists of a list of polygons, also used for
triangulations of other objects.
Polygons (mostly triangles and quadrilaterals) are stored in the
c struct ``face_c`` (see transform.pyx). Rather than storing
the points directly for each polygon, each face consists a list
of pointers into a common list of points which are basically triples
of doubles in a ``point_c``.
Moreover, each face has an attribute ``color`` which is used to
store color information when faces are colored. The red/green/blue
components are then available as floats between 0 and 1 using
``color.r,color.g,color.b``.
Usually these objects are not created directly by users.
EXAMPLES::
sage: from sage.plot.plot3d.index_face_set import IndexFaceSet
sage: S = IndexFaceSet([[(1,0,0),(0,1,0),(0,0,1)],[(1,0,0),(0,1,0),(0,0,0)]])
sage: S.face_list()
[[(1.0, 0.0, 0.0), (0.0, 1.0, 0.0), (0.0, 0.0, 1.0)], [(1.0, 0.0, 0.0), (0.0, 1.0, 0.0), (0.0, 0.0, 0.0)]]
sage: S.vertex_list()
[(1.0, 0.0, 0.0), (0.0, 1.0, 0.0), (0.0, 0.0, 1.0), (0.0, 0.0, 0.0)]
sage: def make_face(n): return [(0,0,n),(0,1,n),(1,1,n),(1,0,n)]
sage: S = IndexFaceSet([make_face(n) for n in range(10)])
sage: S.show()
sage: point_list = [(1,0,0),(0,1,0)] + [(0,0,n) for n in range(10)]
sage: face_list = [[0,1,n] for n in range(2,10)]
sage: S = IndexFaceSet(face_list, point_list, color='red')
sage: S.face_list()
[[(1.0, 0.0, 0.0), (0.0, 1.0, 0.0), (0.0, 0.0, 0.0)],
[(1.0, 0.0, 0.0), (0.0, 1.0, 0.0), (0.0, 0.0, 1.0)],
[(1.0, 0.0, 0.0), (0.0, 1.0, 0.0), (0.0, 0.0, 2.0)],
[(1.0, 0.0, 0.0), (0.0, 1.0, 0.0), (0.0, 0.0, 3.0)],
[(1.0, 0.0, 0.0), (0.0, 1.0, 0.0), (0.0, 0.0, 4.0)],
[(1.0, 0.0, 0.0), (0.0, 1.0, 0.0), (0.0, 0.0, 5.0)],
[(1.0, 0.0, 0.0), (0.0, 1.0, 0.0), (0.0, 0.0, 6.0)],
[(1.0, 0.0, 0.0), (0.0, 1.0, 0.0), (0.0, 0.0, 7.0)]]
sage: S.show()
A simple example of colored IndexFaceSet (:trac:`12212`)::
sage: from sage.plot.plot3d.index_face_set import IndexFaceSet
sage: from sage.plot.plot3d.texture import Texture
sage: point_list = [(2,0,0),(0,2,0),(0,0,2),(0,1,1),(1,0,1),(1,1,0)]
sage: face_list = [[0,4,5],[3,4,5],[2,3,4],[1,3,5]]
sage: col = rainbow(10, 'rgbtuple')
sage: t_list = [Texture(col[i]) for i in range(10)]
sage: S = IndexFaceSet(face_list, point_list, texture_list=t_list)
sage: S.show(viewer='tachyon')
"""
def __init__(self, faces, point_list=None,
enclosed=False, texture_list=None, **kwds):
if 'alpha' in kwds:
opacity = float(kwds.pop('alpha'))
kwds['opacity'] = opacity
PrimitiveObject.__init__(self, **kwds)
self._set_extra_kwds(kwds)
self.global_texture = (texture_list is None)
self.enclosed = enclosed
if point_list is None:
face_list = faces
faces = []
point_list = []
point_index = {}
for face in face_list:
iface = []
for p in face:
try:
ix = point_index[p]
except KeyError:
ix = len(point_list)
point_index[p] = ix
point_list.append(p)
iface.append(ix)
faces.append(iface)
cdef Py_ssize_t i
cdef Py_ssize_t index_len = 0
for i from 0 <= i < len(faces):
index_len += len(faces[i])
self.realloc(len(point_list), len(faces), index_len)
for i from 0 <= i < self.vcount:
self.vs[i].x, self.vs[i].y, self.vs[i].z = point_list[i]
cdef int cur_pt = 0
for i from 0 <= i < self.fcount:
self._faces[i].n = len(faces[i])
self._faces[i].vertices = &self.face_indices[cur_pt]
if self.global_texture:
self._faces[i].color.r, self._faces[i].color.g, self._faces[i].color.b = self.texture.color
else:
self._faces[i].color.r, self._faces[i].color.g, self._faces[i].color.b = texture_list[i].color
for ix in faces[i]:
self.face_indices[cur_pt] = ix
cur_pt += 1
cdef int realloc(self, Py_ssize_t vcount, Py_ssize_t fcount, Py_ssize_t icount) except -1:
r"""
Allocates memory for vertices, faces, and face indices. Can
only be called from Cython, so the doctests must be indirect.
EXAMPLES::
sage: var('x,y,z')
(x, y, z)
sage: G = implicit_plot3d(x^2+y^2+z^2 - 1, (x, -2, 2), (y, -2, 2), (z, -2, 2), plot_points=6)
sage: G.triangulate() # indirect doctest
sage: len(G.face_list())
44
sage: len(G.vertex_list())
132
sage: G = implicit_plot3d(x^2+y^2+z^2 - 100, (x, -2, 2), (y, -2, 2), (z, -2, 2), plot_points=6)
sage: G.triangulate() # indirect doctest
sage: len(G.face_list())
0
sage: len(G.vertex_list())
0
"""
self.vs = <point_c*>check_reallocarray(self.vs, vcount, sizeof(point_c))
self.vcount = vcount
self._faces = <face_c*>check_reallocarray(self._faces, fcount, sizeof(face_c))
self.fcount = fcount
self.face_indices = <int*>check_reallocarray(self.face_indices, icount, sizeof(int))
self.icount = icount
def _clean_point_list(self):
"""
Clean up the vertices and faces as follows:
- Remove all vertices with a coordinate which is NaN or
infinity.
- If a removed vertex occurs in a face, remove it from that
face, but keep other vertices in that face.
- Remove faces with less than 3 vertices.
- Remove unused vertices.
- Free unused memory for vertices and faces (not indices).
"""
cdef Py_ssize_t i, j, v
# point_map is an array old vertex index -> new vertex index.
# The special value -1 means that the vertex is not mapped yet.
# The special value -2 means that the vertex must be deleted
# because a coordinate is NaN or infinity.
# When we are done, all vertices with negative indices are not
# used and will be removed.
cdef int* point_map = <int*>check_allocarray(self.vcount, sizeof(int))
cdef Py_ssize_t nv = 0 # number of new vertices
for i in range(self.vcount):
point_map[i] = -1
# Process all faces
cdef Py_ssize_t nf = 0 # number of new faces
cdef Py_ssize_t fv # number of new vertices on face
for i in range(self.fcount):
face = &self._faces[i]
# Process vertices in face
fv = 0
for j in range(face.n):
v = face.vertices[j]
if point_map[v] == -1:
if point_c_isfinite(self.vs[v]):
point_map[v] = nv
nv += 1
else:
point_map[v] = -2
if point_map[v] == -2:
continue
face.vertices[fv] = point_map[face.vertices[j]]
fv += 1
# Skip faces with less than 3 vertices
if fv < 3:
continue
# Store in newface
newface = &self._faces[nf]
newface.n = fv
if newface is not face:
newface.vertices = face.vertices
newface.color = face.color
nf += 1
# Realloc face array
if nf < self.fcount:
self._faces = <face_c*>check_reallocarray(self._faces, nf, sizeof(face_c))
self.fcount = nf
# Realloc and map vertex array
# We cannot copy in-place since we permuted the vertices
new_vs = <point_c*>check_allocarray(nv, sizeof(point_c))
for i in range(self.vcount):
j = point_map[i]
if j >= 0:
new_vs[j] = self.vs[i]
sig_free(point_map)
sig_free(self.vs)
self.vs = new_vs
self.vcount = nv
def _separate_creases(self, threshold):
"""
Some rendering engines Gouraud shading, which is great for smooth
surfaces but looks bad if one actually has a polyhedron.
INPUT:
``threshold`` -- the minimum cosine of the angle between adjacent
faces a higher threshold separates more, all faces if >= 1, no
faces if <= -1
"""
cdef Py_ssize_t i, j, k
cdef face_c *face
cdef int v, count, total = 0
cdef int* point_counts = <int *>check_calloc(self.vcount * 2 + 1, sizeof(int))
# For each vertex, get number of faces
cdef int* running_point_counts = &point_counts[self.vcount]
for i from 0 <= i < self.fcount:
face = &self._faces[i]
total += face.n
for j from 0 <= j < face.n:
point_counts[face.vertices[j]] += 1
# Running used as index into face list
cdef int running = 0
cdef int max = 0
for i from 0 <= i < self.vcount:
running_point_counts[i] = running
running += point_counts[i]
if point_counts[i] > max:
max = point_counts[i]
running_point_counts[self.vcount] = running
# Create an array, indexed by running_point_counts[v], to the list of faces containing that vertex.
cdef face_c** point_faces
try:
point_faces = <face_c **>check_allocarray(total, sizeof(face_c*))
except MemoryError:
sig_free(point_counts)
raise
sig_on()
memset(point_counts, 0, sizeof(int) * self.vcount)
for i from 0 <= i < self.fcount:
face = &self._faces[i]
for j from 0 <= j < face.n:
v = face.vertices[j]
point_faces[running_point_counts[v]+point_counts[v]] = face
point_counts[v] += 1
# Now, for each vertex, see if all faces are close enough,
# or if it is a crease.
cdef face_c** faces
cdef int start = 0
cdef bint any
# We compare against face 0, and if it's not flat enough we push it to the end.
# Then we come around again to compare everything that was put at the end, possibly
# pushing stuff to the end again (until no further changes are needed).
while start < self.vcount:
ix = self.vcount
# Find creases
for i from 0 <= i < self.vcount - start:
faces = &point_faces[running_point_counts[i]]
any = 0
for j from point_counts[i] > j >= 1:
if cos_face_angle(faces[0][0], faces[j][0], self.vs) < threshold:
any = 1
face = faces[j]
point_counts[i] -= 1
if j != point_counts[i]:
faces[j] = faces[point_counts[i]] # swap
faces[point_counts[i]] = face
if any:
ix += 1
# Reallocate room for vertices at end
if ix > self.vcount:
try:
self.vs = <point_c *>check_reallocarray(self.vs, ix, sizeof(point_c))
except MemoryError:
sig_free(point_counts)
sig_free(point_faces)
self.vcount = self.fcount = self.icount = 0 # so we don't get segfaults on bad points
sig_off()
raise
ix = self.vcount
running = 0
for i from 0 <= i < self.vcount - start:
if point_counts[i] != running_point_counts[i+1] - running_point_counts[i]:
# We have a new vertex
self.vs[ix] = self.vs[i+start]
# Update the point_counts and point_faces arrays for the next time around.
count = running_point_counts[i+1] - running_point_counts[i] - point_counts[i]
faces = &point_faces[running]
for j from 0 <= j < count:
faces[j] = point_faces[running_point_counts[i] + point_counts[i] + j]
face = faces[j]
for k from 0 <= k < face.n:
if face.vertices[k] == i + start:
face.vertices[k] = ix
point_counts[ix-self.vcount] = count
running_point_counts[ix-self.vcount] = running
running += count
ix += 1
running_point_counts[ix-self.vcount] = running
start = self.vcount
self.vcount = ix
sig_free(point_counts)
sig_free(point_faces)
sig_off()
def _mem_stats(self):
return self.vcount, self.fcount, self.icount
def __dealloc__(self):
sig_free(self.vs)
sig_free(self._faces)
sig_free(self.face_indices)
def is_enclosed(self):
"""
Whether or not it is necessary to render the back sides of the polygons.
One is assuming, of course, that they have the correct orientation.
This is may be passed in on construction. It is also
calculated in
:class:`sage.plot.plot3d.parametric_surface.ParametricSurface`
by verifying the opposite edges of the rendered domain either
line up or are pinched together.
EXAMPLES::
sage: from sage.plot.plot3d.index_face_set import IndexFaceSet
sage: IndexFaceSet([[(0,0,1),(0,1,0),(1,0,0)]]).is_enclosed()
False
"""
return self.enclosed
def index_faces(self):
"""
Return the list over all faces of the indices of the vertices.
EXAMPLES::
sage: from sage.plot.plot3d.shapes import *
sage: S = Box(1,2,3)
sage: S.index_faces()
[[0, 1, 2, 3],
[0, 4, 5, 1],
[0, 3, 6, 4],
[5, 4, 6, 7],
[6, 3, 2, 7],
[2, 1, 5, 7]]
"""
cdef Py_ssize_t i, j
return [[self._faces[i].vertices[j]
for j from 0 <= j < self._faces[i].n]
for i from 0 <= i < self.fcount]
def has_local_colors(self):
"""
Return ``True`` if and only if every face has an individual color.
EXAMPLES::
sage: from sage.plot.plot3d.index_face_set import IndexFaceSet
sage: from sage.plot.plot3d.texture import Texture
sage: point_list = [(2,0,0),(0,2,0),(0,0,2),(0,1,1),(1,0,1),(1,1,0)]
sage: face_list = [[0,4,5],[3,4,5],[2,3,4],[1,3,5]]
sage: col = rainbow(10, 'rgbtuple')
sage: t_list=[Texture(col[i]) for i in range(10)]
sage: S = IndexFaceSet(face_list, point_list, texture_list=t_list)
sage: S.has_local_colors()
True
sage: from sage.plot.plot3d.shapes import *
sage: S = Box(1,2,3)
sage: S.has_local_colors()
False
"""
return not(self.global_texture)
def index_faces_with_colors(self):
"""
Return the list over all faces of (indices of the vertices, color).
This only works if every face has its own color.
.. SEEALSO::
:meth:`has_local_colors`
EXAMPLES:
A simple colored one::
sage: from sage.plot.plot3d.index_face_set import IndexFaceSet
sage: from sage.plot.plot3d.texture import Texture
sage: point_list = [(2,0,0),(0,2,0),(0,0,2),(0,1,1),(1,0,1),(1,1,0)]
sage: face_list = [[0,4,5],[3,4,5],[2,3,4],[1,3,5]]
sage: col = rainbow(10, 'rgbtuple')
sage: t_list=[Texture(col[i]) for i in range(10)]
sage: S = IndexFaceSet(face_list, point_list, texture_list=t_list)
sage: S.index_faces_with_colors()
[([0, 4, 5], '#ff0000'),
([3, 4, 5], '#ff9900'),
([2, 3, 4], '#cbff00'),
([1, 3, 5], '#33ff00')]
When the texture is global, an error is raised::
sage: from sage.plot.plot3d.shapes import *
sage: S = Box(1,2,3)
sage: S.index_faces_with_colors()
Traceback (most recent call last):
...
ValueError: the texture is global
"""
cdef Py_ssize_t i, j
if self.global_texture:
raise ValueError('the texture is global')
return [([self._faces[i].vertices[j]
for j from 0 <= j < self._faces[i].n],
Color(self._faces[i].color.r,
self._faces[i].color.g,
self._faces[i].color.b).html_color())
for i from 0 <= i < self.fcount]
def faces(self):
"""
An iterator over the faces.
EXAMPLES::
sage: from sage.plot.plot3d.shapes import *
sage: S = Box(1,2,3)
sage: list(S.faces()) == S.face_list()
True
"""
return FaceIter(self)
def face_list(self):
"""
Return the list of faces.
Every face is given as a tuple of vertices.
EXAMPLES::
sage: from sage.plot.plot3d.shapes import *
sage: S = Box(1,2,3)
sage: S.face_list()[0]
[(1.0, 2.0, 3.0), (-1.0, 2.0, 3.0), (-1.0, -2.0, 3.0), (1.0, -2.0, 3.0)]
"""
points = self.vertex_list()
cdef Py_ssize_t i, j
return [[points[self._faces[i].vertices[j]]
for j from 0 <= j < self._faces[i].n]
for i from 0 <= i < self.fcount]
def edges(self):
"""
An iterator over the edges.
EXAMPLES::
sage: from sage.plot.plot3d.shapes import *
sage: S = Box(1,2,3)
sage: list(S.edges())[0]
((1.0, -2.0, 3.0), (1.0, 2.0, 3.0))
"""
return EdgeIter(self)
def edge_list(self):
"""
Return the list of edges.
EXAMPLES::
sage: from sage.plot.plot3d.shapes import *
sage: S = Box(1,2,3)
sage: S.edge_list()[0]
((1.0, -2.0, 3.0), (1.0, 2.0, 3.0))
"""
return list(self.edges())
def vertices(self):
"""
An iterator over the vertices.
EXAMPLES::
sage: from sage.plot.plot3d.shapes import *
sage: S = Cone(1,1)
sage: list(S.vertices()) == S.vertex_list()
True
"""
return VertexIter(self)
def vertex_list(self):
"""
Return the list of vertices.
EXAMPLES::
sage: from sage.plot.plot3d.shapes import *
sage: S = polygon([(0,0,1), (1,1,1), (2,0,1)])
sage: S.vertex_list()[0]
(0.0, 0.0, 1.0)
"""
cdef Py_ssize_t i
return [(self.vs[i].x, self.vs[i].y, self.vs[i].z) for i from 0 <= i < self.vcount]
def x3d_geometry(self):
"""
Return the x3d data.
EXAMPLES:
A basic test with a triangle::
sage: G = polygon([(0,0,1), (1,1,1), (2,0,1)])
sage: print(G.x3d_geometry())
<BLANKLINE>
<IndexedFaceSet coordIndex='0,1,2,-1'>
<Coordinate point='0.0 0.0 1.0,1.0 1.0 1.0,2.0 0.0 1.0'/>
</IndexedFaceSet>
<BLANKLINE>
A simple colored one::
sage: from sage.plot.plot3d.index_face_set import IndexFaceSet
sage: from sage.plot.plot3d.texture import Texture
sage: point_list = [(2,0,0),(0,2,0),(0,0,2),(0,1,1),(1,0,1),(1,1,0)]
sage: face_list = [[0,4,5],[3,4,5],[2,3,4],[1,3,5]]
sage: col = rainbow(10, 'rgbtuple')
sage: t_list=[Texture(col[i]) for i in range(10)]
sage: S = IndexFaceSet(face_list, point_list, texture_list=t_list)
sage: print(S.x3d_geometry())
<BLANKLINE>
<IndexedFaceSet solid='False' colorPerVertex='False' coordIndex='0,4,5,-1,3,4,5,-1,2,3,4,-1,1,3,5,-1'>
<Coordinate point='2.0 0.0 0.0,0.0 2.0 0.0,0.0 0.0 2.0,0.0 1.0 1.0,1.0 0.0 1.0,1.0 1.0 0.0'/>
<Color color='1.0 0.0 0.0,1.0 0.6000000000000001 0.0,0.7999999999999998 1.0 0.0,0.20000000000000018 1.0 0.0' />
</IndexedFaceSet>
<BLANKLINE>
"""
cdef Py_ssize_t i
vs = self.vs
fs = self._faces
points = ",".join(["%r %r %r" % (vs[i].x, vs[i].y, vs[i].z)
for i from 0 <= i < self.vcount])
coord_idx = ",-1,".join([",".join([repr(fs[i].vertices[j])
for j from 0 <= j < fs[i].n])
for i from 0 <= i < self.fcount])
if not self.global_texture:
color_idx = ",".join(['%r %r %r' % (fs[i].color.r, fs[i].color.g, fs[i].color.b)
for i from 0 <= i < self.fcount])
# Note: Don't use f-strings, since Sage on Python 2 still expects
# this to return a plain str instead of a unicode
return dedent("""
<IndexedFaceSet solid='False' colorPerVertex='False' coordIndex='{coord_idx},-1'>
<Coordinate point='{points}'/>
<Color color='{color_idx}' />
</IndexedFaceSet>
""".format(coord_idx=coord_idx, points=points, color_idx=color_idx))
return dedent("""
<IndexedFaceSet coordIndex='{coord_idx},-1'>
<Coordinate point='{points}'/>
</IndexedFaceSet>
""".format(coord_idx=coord_idx, points=points))
def bounding_box(self):
r"""
Calculate the bounding box for the vertices in this object
(ignoring infinite or NaN coordinates).
OUTPUT:
a tuple ( (low_x, low_y, low_z), (high_x, high_y, high_z)),
which gives the coordinates of opposite corners of the
bounding box.
EXAMPLES::
sage: x,y = var('x,y')
sage: p = plot3d(sqrt(sin(x)*sin(y)), (x,0,2*pi),(y,0,2*pi))
sage: p.bounding_box()
((0.0, 0.0, -0.0), (6.283185307179586, 6.283185307179586, 0.9991889981715697))
"""
if self.vcount == 0:
return ((0,0,0),(0,0,0))
cdef Py_ssize_t i
cdef point_c low
cdef point_c high
low.x, low.y, low.z = INFINITY, INFINITY, INFINITY
high.x, high.y, high.z = -INFINITY, -INFINITY, -INFINITY
for i in range(self.vcount):
point_c_update_finite_lower_bound(&low, self.vs[i])
point_c_update_finite_upper_bound(&high, self.vs[i])
return ((low.x, low.y, low.z), (high.x, high.y, high.z))
def partition(self, f):
r"""
Partition the faces of ``self``.
The partition is done according to the value of a map
`f: \RR^3 \rightarrow \ZZ` applied to the center of each face.
INPUT:
- `f` -- a function from `\RR^3` to `\ZZ`
EXAMPLES::
sage: from sage.plot.plot3d.shapes import *
sage: S = Box(1,2,3)
sage: len(S.partition(lambda x,y,z : floor(x+y+z)))
6
"""
cdef Py_ssize_t i, j, ix, face_ix
cdef int part
cdef point_c P
cdef face_c *face
cdef face_c *new_face
cdef IndexFaceSet face_set
cdef int *partition = <int *>check_allocarray(self.fcount, sizeof(int))
part_counts = {}
for i from 0 <= i < self.fcount:
face = &self._faces[i]
P = self.vs[face.vertices[0]]
for j from 1 <= j < face.n:
point_c_add(&P, P, self.vs[face.vertices[j]])
point_c_mul(&P, P, 1.0/face.n)
partition[i] = part = f(P.x, P.y, P.z)
try:
count = part_counts[part]
except KeyError:
part_counts[part] = count = [0, 0]
count[0] += 1
count[1] += face.n
all = {}
for part, count in part_counts.iteritems():
face_set = IndexFaceSet([])
face_set.realloc(self.vcount, count[0], count[1])
memcpy(face_set.vs, self.vs, sizeof(point_c) * self.vcount)
face_ix = 0
ix = 0
for i from 0 <= i < self.fcount:
if partition[i] == part:
face = &self._faces[i]
new_face = &face_set._faces[face_ix]
new_face.n = face.n
new_face.vertices = &face_set.face_indices[ix]
for j from 0 <= j < face.n:
new_face.vertices[j] = face.vertices[j]
face_ix += 1
ix += face.n
face_set._clean_point_list()
all[part] = face_set
sig_free(partition)
return all
def add_condition(self, condition, N=40):
"""
Cut the surface according to the given condition.
This allows to take the intersection of the surface
with a domain in 3-space, in such a way that the result
has a smooth boundary.
INPUT:
- ``condition`` -- boolean function on ambient space, that
defines the domain
- ``N`` -- number of steps (default: 40) used on the boundary
to cut the triangles that are not entirely within the domain
For higher quality, meaning smoother boundary, use larger ``N``.
OUTPUT:
an ``IndexFaceSet``
This will contain both triangular and quadrilateral faces.
EXAMPLES::
sage: var('x,y,z')
(x, y, z)
sage: P = implicit_plot3d(z-x*y,(-2,2),(-2,2),(-2,2))
sage: def condi(x,y,z):
....: return bool(x*x+y*y+z*z <= Integer(1))
sage: R = P.add_condition(condi,8);R
Graphics3d Object
.. PLOT::
x,y,z = var('x,y,z')
P = implicit_plot3d(z-x*y,(-2,2),(-2,2),(-2,2))
def condi(x,y,z):
return bool(x*x+y*y+z*z <= Integer(1))
sphinx_plot(P.add_condition(condi,8))
An example with colors::
sage: def condi(x,y,z):
....: return bool(x*x+y*y <= 1.1)
sage: cm = colormaps.hsv
sage: cf = lambda x,y,z: float(x+y) % 1
sage: P = implicit_plot3d(x**2+y**2+z**2-1-x**2*z+y**2*z,(-2,2),(-2,2),(-2,2),color=(cm,cf))
sage: R = P.add_condition(condi,18); R
Graphics3d Object
.. PLOT::
x,y,z = var('x,y,z')
def condi(x,y,z):
return bool(x*x+y*y <= 1.1)
cm = colormaps.hsv
cf = lambda x,y,z: float(x+y) % 1
P = implicit_plot3d(x**2+y**2+z**2-1-x**2*z+y**2*z,(-2,2),(-2,2),(-2,2),color=(cm,cf))
sphinx_plot(P.add_condition(condi,18))
An example with transparency::
sage: P = implicit_plot3d(x**4+y**4+z**2-4,(x,-2,2),(y,-2,2),(z,-2,2),alpha=0.3)
sage: def cut(a,b,c):
....: return a*a+c*c > 2
sage: Q = P.add_condition(cut,40); Q
Graphics3d Object
.. PLOT::
x,y,z = var('x,y,z')
P = implicit_plot3d(x**4+y**4+z**2-4,(x,-2,2),(y,-2,2),(z,-2,2),alpha=0.3)
def cut(a,b,c):
return a*a+c*c > 2
sphinx_plot(P.add_condition(cut,40))
A sombrero with quadrilaterals::
sage: P = plot3d(-sin(2*x*x+2*y*y)*exp(-x*x-y*y),(x,-2,2),(y,-2,2),
....: color='gold')
sage: def cut(x,y,z):
....: return x*x+y*y < 1