0

我正在尝试使用 Python 的 Pyglet 库绘制 3D 网络。

我有代表蛋白质的球体,它们被绘制在适当的位置(给定 x、y、z 坐标),而不是在 0,0,0 处绘制它们并将它们转换为位置。我现在正在寻找将它们与气缸连接。我的目标是通过计算以每个球体坐标为中心的顶点圆,然后将它们与四边形连接。

但是,我在尝试计算顶点以使两个圆彼此面对时遇到了一些障碍。

到目前为止,这是我的代码:

class Cylinder(object):
    def __init__(self, start, end, radius, slices, batch, group=None):
        """
        :param start: The (x,y,z) coordinates of the cylinder's start point
        :type start: (float, float, float)
        :param end: The (x,y,z) coordinates of the cylinder's end point
        :type end: (float, float, float)
        :param radius: The radius of the cylinder
        :type radius: float
        :param slices: Number of sections to divide the cylinder into
        :type slices: int
        :param batch: The pyglet batch to add the cylinder to
        :type batch: pyglet.graphics.Batch
        :param group: The pyglet group to add the cylinder to
        :type group: int
        """
        self.start_x, self.start_y, self.start_z = start
        self.end_x, self.end_y, self.end_z = end

        self.num_vertices = 6 * slices
        self.num_faces = slices
        self.num_indexes = 4 * self.num_faces

        self.vertices = numpy.zeros(self.num_vertices, dtype=numpy.float)
        self.normals = numpy.zeros(self.num_vertices, dtype=numpy.float)
        self.indexes = numpy.zeros(self.num_indexes, dtype=numpy.uint32)

        step = 2 * pi / slices
        lat = 0
        lon = 0
        for i in xrange(slices):
            sin_lat = sin(lat)
            cos_lat = cos(lat)
            sin_lon = sin(lon)
            cos_lon = cos(lon)

            # Calculate vertex positions
            sx = self.start_x + radius * cos_lon * sin_lat
            sy = self.start_y + radius * sin_lon * sin_lat
            sz = self.start_z + radius * cos_lat
            ex = self.end_x + radius * cos_lon * sin_lat
            ey = self.end_y + radius * sin_lon * sin_lat
            ez = self.end_z + radius * cos_lat

            # Calculate vertex normals
            snx = cos_lon * sin_lat
            sny = sin_lon * sin_lat
            snz = cos_lat
            enx = cos_lon * sin_lat
            eny = sin_lon * sin_lat
            enz = cos_lat

            # Add vertex positions and normals to
            index = 6 * i
            self.vertices[index:index + 6] = (sx, sy, sz, ex, ey, ez)
            self.normals[index:index + 6] = (snx, sny, snz, enx, eny, enz)

            # Go to next vertex pair
            lat += step

        for i in xrange(slices):
            self.indexes[4 * i:4 * i + 4] = ((2 * i),
                                             (2 * i + 2) % (self.num_vertices // 3),
                                             (2 * i + 3) % (self.num_vertices // 3),
                                             (2 * i + 1))

        if batch:
            self.vertex_list = batch.add_indexed(len(self.vertices) // 3,
                                                 gl.GL_QUADS,
                                                 group,
                                                 self.indexes,
                                                 ('v3f/static', self.vertices),
                                                 ('n3f/static', self.normals))

    def delete(self):
        self.vertex_list.delete()

任何帮助将不胜感激!

4

1 回答 1

0

为什么你需要面对面的圆圈?

要构建管,您需要以下算法:

  1. 计算从 A 到 B 的向量 AB 并且它是垂直的(Norm);
  2. 按管半径缩放标准
  3. 开始添加顶点以构造管,段数为 Seg:

    I := 0;
    for K := 0 to Seg-1 do
    begin
      TubeCos := Cos(K/Seg*2*pi);
      TubeSin := Sin(K/Seg*2*pi);
      Vert[I].Normal := (-Norm.X*TubeSin, TubeCos, -Norm.Y*TubeSin);
      Vert[I].Vert := (A.X-Norm.X*TubeRad*TubeSin, TubeRad*TubeCos, A.Y-Norm.Y*TubeRad*TubeSin);
      I++;
      Vert[I].Normal := (-Norm.X*TubeSin, TubeCos, -Norm.Y*TubeSin);
      Vert[I].VertB := (B.X-Norm.X*TubeRad*TubeSin, TubeRad*TubeCos, B.Y-Norm.Y*TubeRad*TubeSin);
      I++;
    end;
    
  4. 将顶点连接成三角形(顺序很重要) //在这个例子中 0-1-2, 2-3-1, 2-3-4, 4-5-3, ...

于 2013-03-22T06:00:00.240 回答