|
| 1 | +import pathlib |
| 2 | +import math |
| 3 | +import numpy |
| 4 | +import time |
| 5 | +import moderngl |
| 6 | +import pyrr |
| 7 | +from PyQt5 import QtWidgets |
| 8 | +from PyQt5.QtCore import QTimer |
| 9 | + |
| 10 | +from util_moderngl_qt import DrawerMesh, QGLWidgetViewer3 |
| 11 | +from util_moderngl_qt.drawer_transform_multi import DrawerTransformMulti |
| 12 | +from del_msh import TriMesh, DeformMLS |
| 13 | + |
| 14 | + |
| 15 | +class MainWindow(QtWidgets.QMainWindow): |
| 16 | + |
| 17 | + def __init__(self): |
| 18 | + path_file = pathlib.Path('.') / 'asset' / 'bunny_1k.obj' |
| 19 | + self.tri2vtx, self.vtx2xyz = TriMesh.load_wavefront_obj(str(path_file), is_centerize=True, normalized_size=1.) |
| 20 | + self.vtx2xyz_new = self.vtx2xyz.copy() |
| 21 | + print(self.vtx2xyz_new.strides, self.vtx2xyz_new.dtype) |
| 22 | + self.samples_old = numpy.array([ |
| 23 | + (-0.3, -0.3, -0.3), |
| 24 | + (+0.3, -0.3, -0.3), |
| 25 | + (-0.3, +0.3, -0.3), |
| 26 | + (-0.3, -0.3, +0.3), |
| 27 | + (-0.3, +0.3, +0.3)], dtype=numpy.float32) |
| 28 | + self.weights = DeformMLS.kernel(self.samples_old, self.vtx2xyz) |
| 29 | + self.mls_data = DeformMLS.precomp(self.samples_old, self.vtx2xyz, self.weights) |
| 30 | + self.samples_new = self.samples_old.copy() |
| 31 | + |
| 32 | + edge2vtx = TriMesh.edge2vtx(self.tri2vtx, self.vtx2xyz.shape[0]) |
| 33 | + self.drawer_edge = DrawerMesh.Drawer( |
| 34 | + vtx2xyz=self.vtx2xyz, |
| 35 | + list_elem2vtx=[ |
| 36 | + DrawerMesh.ElementInfo(index=edge2vtx, color=(0, 0, 0), mode=moderngl.LINES), |
| 37 | + DrawerMesh.ElementInfo(index=self.tri2vtx, color=(1, 1, 1), mode=moderngl.TRIANGLES) |
| 38 | + ] |
| 39 | + ) |
| 40 | + |
| 41 | + sphere_tri2vtx, sphere_vtx2xyz = TriMesh.sphere() |
| 42 | + self.drawer_sphere = DrawerMesh.Drawer(vtx2xyz=sphere_vtx2xyz, list_elem2vtx=[ |
| 43 | + DrawerMesh.ElementInfo(index=sphere_tri2vtx, color=(1., 0., 0.), mode=moderngl.TRIANGLES)]) |
| 44 | + self.drawer_sphere = DrawerTransformMulti(self.drawer_sphere) |
| 45 | + for sample in self.samples_old: |
| 46 | + scale = pyrr.Matrix44.from_scale((0.01, 0.01, 0.01)) |
| 47 | + translation = pyrr.Matrix44.from_translation(sample) |
| 48 | + self.drawer_sphere.list_transform.append(translation * scale) |
| 49 | + |
| 50 | + super().__init__() |
| 51 | + self.resize(640, 480) |
| 52 | + self.setWindowTitle('Mesh Viewer') |
| 53 | + self.glwidget = QGLWidgetViewer3.QtGLWidget_Viewer3( |
| 54 | + [self.drawer_edge, self.drawer_sphere]) |
| 55 | + self.setCentralWidget(self.glwidget) |
| 56 | + # |
| 57 | + self.timer = QTimer() |
| 58 | + self.timer.setInterval(30) |
| 59 | + self.timer.timeout.connect(self.step_time) |
| 60 | + self.timer.start() |
| 61 | + |
| 62 | + def step_time(self): |
| 63 | + # self.time += self.timer.interval() * 1.0e-3 |
| 64 | + self.samples_new = self.samples_old.copy() |
| 65 | + self.samples_new[0, 0] += -0.5 + 0.4 * math.sin(time.time() * 2.0) |
| 66 | + self.vtx2xyz_new = self.mls_data.dot(self.samples_new) |
| 67 | + # |
| 68 | + self.drawer_edge.update_position(self.vtx2xyz_new) |
| 69 | + for i_sample in range(self.samples_new.shape[0]): |
| 70 | + scale = pyrr.Matrix44.from_scale((0.03, 0.03, 0.03)) |
| 71 | + translation = pyrr.Matrix44.from_translation(self.samples_new[i_sample]) |
| 72 | + self.drawer_sphere.list_transform[i_sample] = translation * scale |
| 73 | + self.glwidget.update() |
| 74 | + |
| 75 | + |
| 76 | +def main(): |
| 77 | + with QtWidgets.QApplication([]) as app: |
| 78 | + win = MainWindow() |
| 79 | + win.show() |
| 80 | + app.exec() |
| 81 | + |
| 82 | + |
| 83 | +if __name__ == "__main__": |
| 84 | + main() |
0 commit comments