def extrude(self, x1, y1, x2, y2): n = QVector3D.normal(QVector3D(0, 0, -0.1), QVector3D(x2 - x1, y2 - y1, 0)) self.add(QVector3D(x1, y1, 0.05), n) self.add(QVector3D(x1, y1, -0.05), n) self.add(QVector3D(x2, y2, 0.05), n) self.add(QVector3D(x2, y2, -0.05), n) self.add(QVector3D(x2, y2, 0.05), n) self.add(QVector3D(x1, y1, -0.05), n)
def extrude(self, x1, y1, x2, y2): n = QVector3D.normal(QVector3D(0, 0, -0.1), QVector3D(x2 - x1, y2 - y1, 0)) self.add(QVector3D(x1, y1, 0.05), n) self.add(QVector3D(x1, y1, -0.05), n) self.add(QVector3D(x2, y2, 0.05), n) self.add(QVector3D(x2, y2, -0.05), n) self.add(QVector3D(x2, y2, 0.05), n) self.add(QVector3D(x1, y1, -0.05), n)
def quad(self, x1, y1, x2, y2, x3, y3, x4, y4): n = QVector3D.normal(QVector3D(x4 - x1, y4 - y1, 0), QVector3D(x2 - x1, y2 - y1, 0)) self.add(QVector3D(x1, y1, -0.05), n) self.add(QVector3D(x4, y4, -0.05), n) self.add(QVector3D(x2, y2, -0.05), n) self.add(QVector3D(x3, y3, -0.05), n) self.add(QVector3D(x2, y2, -0.05), n) self.add(QVector3D(x4, y4, -0.05), n) n = QVector3D.normal(QVector3D(x1 - x4, y1 - y4, 0), QVector3D(x2 - x4, y2 - y4, 0)) self.add(QVector3D(x4, y4, 0.05), n) self.add(QVector3D(x1, y1, 0.05), n) self.add(QVector3D(x2, y2, 0.05), n) self.add(QVector3D(x2, y2, 0.05), n) self.add(QVector3D(x3, y3, 0.05), n) self.add(QVector3D(x4, y4, 0.05), n)
def quad(self, x1, y1, x2, y2, x3, y3, x4, y4): n = QVector3D.normal(QVector3D(x4 - x1, y4 - y1, 0), QVector3D(x2 - x1, y2 - y1, 0)) self.add(QVector3D(x1, y1, -0.05), n) self.add(QVector3D(x4, y4, -0.05), n) self.add(QVector3D(x2, y2, -0.05), n) self.add(QVector3D(x3, y3, -0.05), n) self.add(QVector3D(x2, y2, -0.05), n) self.add(QVector3D(x4, y4, -0.05), n) n = QVector3D.normal(QVector3D(x1 - x4, y1 - y4, 0), QVector3D(x2 - x4, y2 - y4, 0)) self.add(QVector3D(x4, y4, 0.05), n) self.add(QVector3D(x1, y1, 0.05), n) self.add(QVector3D(x2, y2, 0.05), n) self.add(QVector3D(x2, y2, 0.05), n) self.add(QVector3D(x3, y3, 0.05), n) self.add(QVector3D(x4, y4, 0.05), n)
def generateVertex(self, img_scr): self.i = 0 self.m_count = 0 self.m_data = np.empty(3000000 * 6, dtype=ctypes.c_float) image = Image.open(img_scr) basewidth = 300 wpercent = (basewidth / float(image.size[0])) hsize = int((float(image.size[1]) * float(wpercent))) image = image.resize((basewidth, hsize), Image.ANTIALIAS) ary = np.array(image) r, g, b = np.split(ary, 3, axis=2) r = r.reshape(-1) g = r.reshape(-1) b = r.reshape(-1) self.width = ary.shape[0] self.height = ary.shape[1] bitmap = list( map(lambda x: 255 - (0.299 * x[0] + 0.587 * x[1] + 0.114 * x[2]), zip(r, g, b))) max_grey = np.amax(bitmap) min_grey = np.amin(bitmap) bitmap_mapped = list( map( lambda x: (self.max_thickness - self.min_thickness) / (max_grey - min_grey) * x + self.min_thickness, bitmap)) bitmap1 = np.array(bitmap_mapped).reshape([self.width, self.height]) for x in range(0, self.width - 1): for y in range(0, self.height - 1): x1 = x * self.step_size y1 = y * self.step_size z1 = bitmap1[x, y] x2 = (x + 1) * self.step_size y2 = y * self.step_size z2 = bitmap1[x + 1, y] x3 = (x + 1) * self.step_size y3 = (y + 1) * self.step_size z3 = bitmap1[x + 1, y + 1] x4 = x * self.step_size y4 = (y + 1) * self.step_size z4 = bitmap1[x, y + 1] n = QVector3D( QVector3D.normal(QVector3D(x1, y1, z1), QVector3D(x2, y2, z2), QVector3D(x3, y3, z3))) self.add(QVector3D(x1, y1, z1), n) self.add(QVector3D(x2, y2, z2), n) self.add(QVector3D(x3, y3, z3), n) self.add(QVector3D(x4, y4, z4), n) self.generateBackFace() self.generateBorders() print("Vertices were added to vbo")
def create_normal_buffer(vertices, triangles): """ Creates normal vectors for each vertex on the mesh. Qt requires each vertex to have it's own normal. :param vertices: The vertices for the mesh :param triangles: A list of the triangles that make up each face in the mesh :return: A list of the normal points for the faces """ normal_buffer_values = [] for triangle in triangles: # Get the vertices of each triangle points = [vertices[p] for p in triangle] # Convert our vector objects into Qt Vectors # Calculate the normal, leveraging Qt normal = QVector3D.normal(*points) # Need to have a normal for each vector normal_buffer_values.extend(normal.toTuple() * 3) return normal_buffer_values