Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

using python-style loops in plot3d/*.pyx #35922

Merged
merged 2 commits into from
Jul 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 15 additions & 15 deletions src/sage/plot/plot3d/implicit_surface.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -493,8 +493,8 @@ cdef class MarchingCubesTriangles(MarchingCubes):
cdef bint has_nan
cdef point_c gradients[2]
cdef int i
for y from 0 <= y < ny - 1:
for z from 0 <= z < nz:
for y in range(ny - 1):
for z in range(nz):
if marching_has_edge(cur[y,z], cur[y+1,z], self.contour, &frac, &has_nan):
v = mk_VertexInfo(x, y+frac, z, &self.eval_min, &self.eval_scale)
if self.region is not None:
Expand All @@ -507,7 +507,7 @@ cdef class MarchingCubesTriangles(MarchingCubes):
self.apply_point_func(&v.gradient, self.gradient, v)
else:
# Use central differencing.
for i from 0 <= i < 2:
for i in range(2):
self.get_gradient(&gradients[i],
x, y+i, z,
cur[y+i,z],
Expand All @@ -527,8 +527,8 @@ cdef class MarchingCubesTriangles(MarchingCubes):

# OK, that updated the Y vertices. Now we do almost exactly
# the same thing to update Z vertices.
for y from 0 <= y < ny:
for z from 0 <= z < nz - 1:
for y in range(ny):
for z in range(nz - 1):
if marching_has_edge(cur[y,z], cur[y,z+1], self.contour, &frac, &has_nan):
v = mk_VertexInfo(x, y, z+frac, &self.eval_min, &self.eval_scale)
if self.region is not None:
Expand All @@ -541,7 +541,7 @@ cdef class MarchingCubesTriangles(MarchingCubes):
self.apply_point_func(&v.gradient, self.gradient, v)
else:
# Use central differencing.
for i from 0 <= i < 2:
for i in range(2):
self.get_gradient(&gradients[i],
x, y, z+i,
cur[y,z+i],
Expand Down Expand Up @@ -594,8 +594,8 @@ cdef class MarchingCubesTriangles(MarchingCubes):
cdef double frac
cdef bint has_nan
cdef point_c gradients[2]
for y from 0 <= y < ny:
for z from 0 <= z < nz:
for y in range(ny):
for z in range(nz):
if marching_has_edge(left[y,z], right[y,z], self.contour, &frac, &has_nan):
v = mk_VertexInfo(x+frac, y, z, &self.eval_min, &self.eval_scale)
if self.region is not None:
Expand Down Expand Up @@ -744,8 +744,8 @@ cdef class MarchingCubesTriangles(MarchingCubes):

cdef int i

for y from 0 <= y < ny-1:
for z from 0 <= z < nz-1:
for y in range(ny - 1):
for z in range(nz - 1):
# For each vertex (0 to 7), set the corresponding bit
# of insideMask iff the vertex is inside the surface.
insideMask = 0
Expand Down Expand Up @@ -908,13 +908,13 @@ cpdef render_implicit(f, xrange, yrange, zrange, plot_points, cube_marchers):

cdef MarchingCubes marcher

for n from 0 <= n < nx:
for n in range(nx):
x = nx-1-n
eval_x = eval_min.x + eval_scale.x * x
slice = data[n % 4, :, :]
for y from 0 <= y < ny:
for y in range(ny):
eval_y = eval_min.y + eval_scale.y * y
for z from 0 <= z < nz:
for z in range(nz):
eval_z = eval_min.z + eval_scale.z * z
slice[y, z] = f(eval_x, eval_y, eval_z)

Expand Down Expand Up @@ -1217,7 +1217,7 @@ cdef class ImplicitSurface(IndexFaceSet):
int fcount = len(results)

self.realloc(fcount * 3, fcount, fcount * 3)
for i from 0 <= i < fcount:
for i in range(fcount):
dest_face = &self._faces[i]
src_face = results[i]

Expand All @@ -1233,7 +1233,7 @@ cdef class ImplicitSurface(IndexFaceSet):
# ct is the mean of the colors of vertices
dest_face.color.r, dest_face.color.g, dest_face.color.b = ct

for j from 0 <= j < 3:
for j in range(3):
dest_face.vertices[j] = (3 * i) + j
dest_vertex = &self.vs[(3 * i) + j]
dest_vertex.x = src_face[j]['x']
Expand Down
86 changes: 43 additions & 43 deletions src/sage/plot/plot3d/index_face_set.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -178,15 +178,15 @@ cdef inline format_pmesh_face(face_c face, int has_color):
# Naive triangulation
all = []
if has_color == 1:
for i from 1 <= i < face.n - 1:
for i in range(1, 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:
for i in range(1, 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],
Expand Down Expand Up @@ -369,16 +369,16 @@ cdef class IndexFaceSet(PrimitiveObject):

cdef Py_ssize_t i
cdef Py_ssize_t index_len = 0
for i from 0 <= i < len(faces):
for i in range(len(faces)):
index_len += len(faces[i])

self.realloc(len(point_list), len(faces), index_len)

for i from 0 <= i < self.vcount:
for i in range(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:
for i in range(self.fcount):
self._faces[i].n = len(faces[i])
self._faces[i].vertices = &self.face_indices[cur_pt]
if self.global_texture:
Expand Down Expand Up @@ -517,15 +517,15 @@ cdef class IndexFaceSet(PrimitiveObject):
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:
for i in range(self.fcount):
face = &self._faces[i]
total += face.n
for j from 0 <= j < face.n:
for j in range(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:
for i in range(self.vcount):
running_point_counts[i] = running
running += point_counts[i]
if point_counts[i] > max:
Expand All @@ -540,9 +540,9 @@ cdef class IndexFaceSet(PrimitiveObject):
raise
sig_on()
memset(point_counts, 0, sizeof(int) * self.vcount)
for i from 0 <= i < self.fcount:
for i in range(self.fcount):
face = &self._faces[i]
for j from 0 <= j < face.n:
for j in range(face.n):
v = face.vertices[j]
point_faces[running_point_counts[v]+point_counts[v]] = face
point_counts[v] += 1
Expand All @@ -557,7 +557,7 @@ cdef class IndexFaceSet(PrimitiveObject):
while start < self.vcount:
ix = self.vcount
# Find creases
for i from 0 <= i < self.vcount - start:
for i in range(self.vcount - start):
faces = &point_faces[running_point_counts[i]]
any = 0
for j from point_counts[i] > j >= 1:
Expand All @@ -582,17 +582,17 @@ cdef class IndexFaceSet(PrimitiveObject):
raise
ix = self.vcount
running = 0
for i from 0 <= i < self.vcount - start:
for i in range(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:
for j in range(count):
faces[j] = point_faces[running_point_counts[i] + point_counts[i] + j]
face = faces[j]
for k from 0 <= k < face.n:
for k in range(face.n):
if face.vertices[k] == i + start:
face.vertices[k] = ix
point_counts[ix-self.vcount] = count
Expand Down Expand Up @@ -653,8 +653,8 @@ cdef class IndexFaceSet(PrimitiveObject):
"""
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]
for j in range(self._faces[i].n)]
for i in range(self.fcount)]

def has_local_colors(self):
"""
Expand Down Expand Up @@ -719,11 +719,11 @@ cdef class IndexFaceSet(PrimitiveObject):
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],
for j in range(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]
for i in range(self.fcount)]

def faces(self):
"""
Expand Down Expand Up @@ -944,10 +944,10 @@ cdef class IndexFaceSet(PrimitiveObject):
cdef int *partition = <int *>check_allocarray(self.fcount, sizeof(int))

part_counts = {}
for i from 0 <= i < self.fcount:
for i in range(self.fcount):
face = &self._faces[i]
P = self.vs[face.vertices[0]]
for j from 1 <= j < face.n:
for j in range(1, 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)
Expand All @@ -964,13 +964,13 @@ cdef class IndexFaceSet(PrimitiveObject):
memcpy(face_set.vs, self.vs, sizeof(point_c) * self.vcount)
face_ix = 0
ix = 0
for i from 0 <= i < self.fcount:
for i in range(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:
for j in range(face.n):
new_face.vertices[j] = face.vertices[j]
face_ix += 1
ix += face.n
Expand Down Expand Up @@ -1226,7 +1226,7 @@ cdef class IndexFaceSet(PrimitiveObject):
cdef face_c face
cdef Py_ssize_t i, k
sig_on()
for i from 0 <= i < self.fcount:
for i in range(self.fcount):
face = self._faces[i]
if transform is not None:
transform.transform_point_c(&P, self.vs[face.vertices[0]])
Expand All @@ -1242,7 +1242,7 @@ cdef class IndexFaceSet(PrimitiveObject):
else:
PyList_Append(lines, format_tachyon_texture(face.color))
if face.n > 3:
for k from 3 <= k < face.n:
for k in range(3, face.n):
Q = R
if transform is not None:
transform.transform_point_c(&R, self.vs[face.vertices[k]])
Expand Down Expand Up @@ -1290,7 +1290,7 @@ cdef class IndexFaceSet(PrimitiveObject):
for i in range(self.vcount)))
else:
vertices_str = "["
for i from 0 <= i < self.vcount:
for i in range(self.vcount):
transform.transform_point_c(&res, self.vs[i])
if i > 0:
vertices_str += ","
Expand Down Expand Up @@ -1408,7 +1408,7 @@ cdef class IndexFaceSet(PrimitiveObject):
vertices = []
cdef Transformation transform = render_params.transform
cdef point_c res
for i from 0 <= i < self.vcount:
for i in range(self.vcount):
if transform is None:
res = self.vs[i]
else:
Expand All @@ -1418,16 +1418,16 @@ cdef class IndexFaceSet(PrimitiveObject):

faces = []
cdef face_c face
for i from 0 <= i < self.fcount:
for i in range(self.fcount):
face = self._faces[i]
faces.append([int(face.vertices[j]) for j from 0 <= j < face.n])
faces.append([int(face.vertices[j]) for j in range(face.n)])
surface['faces'] = faces

if self.global_texture:
surface['color'] = '#' + str(self.texture.hex_rgb())
else:
face_colors = []
for i from 0 <= i < self.fcount:
for i in range(self.fcount):
face = self._faces[i]
color = Color(face.color.r, face.color.g, face.color.b)
face_colors.append(str(color.html_color()))
Expand Down Expand Up @@ -1472,16 +1472,16 @@ cdef class IndexFaceSet(PrimitiveObject):

sig_on()
if transform is None:
points = [format_obj_vertex(self.vs[i]) for i from 0 <= i < self.vcount]
points = [format_obj_vertex(self.vs[i]) for i in range(self.vcount)]
else:
points = []
for i from 0 <= i < self.vcount:
for i in range(self.vcount):
transform.transform_point_c(&res, self.vs[i])
PyList_Append(points, format_obj_vertex(res))

faces = [format_obj_face(self._faces[i], off) for i from 0 <= i < self.fcount]
faces = [format_obj_face(self._faces[i], off) for i in range(self.fcount)]
if not self.enclosed:
back_faces = [format_obj_face_back(self._faces[i], off) for i from 0 <= i < self.fcount]
back_faces = [format_obj_face_back(self._faces[i], off) for i in range(self.fcount)]
else:
back_faces = []

Expand Down Expand Up @@ -1513,25 +1513,25 @@ cdef class IndexFaceSet(PrimitiveObject):
sig_on()
if transform is None:
points = [format_pmesh_vertex(self.vs[i])
for i from 0 <= i < self.vcount]
for i in range(self.vcount)]
else:
points = []
for i from 0 <= i < self.vcount:
for i in range(self.vcount):
transform.transform_point_c(&res, self.vs[i])
PyList_Append(points, format_pmesh_vertex(res))

# activation of coloring in jmol
if self.global_texture:
faces = [format_pmesh_face(self._faces[i], 1)
for i from 0 <= i < self.fcount]
for i in range(self.fcount)]
else:
faces = [format_pmesh_face(self._faces[i], -1)
for i from 0 <= i < self.fcount]
for i in range(self.fcount)]

# If a face has more than 4 vertices, it gets chopped up in
# format_pmesh_face
cdef Py_ssize_t extra_faces = 0
for i from 0 <= i < self.fcount:
for i in range(self.fcount):
if self._faces[i].n >= 5:
extra_faces += self._faces[i].n-3

Expand Down Expand Up @@ -1641,14 +1641,14 @@ cdef class IndexFaceSet(PrimitiveObject):
dual.realloc(self.fcount, self.vcount, self.icount)

# is using dicts overly-heavy?
dual_faces = [{} for i from 0 <= i < self.vcount]
dual_faces = [{} for i in range(self.vcount)]

for i from 0 <= i < self.fcount:
for i in range(self.fcount):
sig_check()
# Let the vertex be centered on the face according to a simple average
face = &self._faces[i]
dual.vs[i] = self.vs[face.vertices[0]]
for j from 1 <= j < face.n:
for j in range(1, face.n):
point_c_add(&dual.vs[i], dual.vs[i], self.vs[face.vertices[j]])
point_c_mul(&dual.vs[i], dual.vs[i], 1.0/face.n)

Expand Down Expand Up @@ -1676,7 +1676,7 @@ cdef class IndexFaceSet(PrimitiveObject):
face.vertices = &dual.face_indices[ix]
ff, next_ = next(iter(dd.itervalues()))
face.vertices[0] = ff
for j from 1 <= j < face.n:
for j in range(1, face.n):
ff, next_ = dd[next_]
face.vertices[j] = ff
i += 1
Expand Down Expand Up @@ -1764,7 +1764,7 @@ cdef class FaceIter:
raise StopIteration
else:
face = []
for j from 0 <= j < self.set._faces[self.i].n:
for j in range(self.set._faces[self.i].n):
P = self.set.vs[self.set._faces[self.i].vertices[j]]
PyList_Append(face, (P.x, P.y, P.z))
self.i += 1
Expand Down
Loading