import struct ### Vertex of a 3D Model ### class vert: def __init__(self, index, x, y, z): self.index = index self.x = x self.y = y self.z = z def __str__(self): return "({}, {}, {})".format(self.x,self.y,self.z) def as_bytes(self): return struct.pack("3f", self.x, self.y, self.z) ### Triangle linking 3 vertices of a 3D Model ### class triangle: def __init__(self, v1, v2, v3): self.v1 = v1 self.v2 = v2 self.v3 = v3 def __str__(self): return "({}, {}, {})".format(self.v1.index, self.v2.index, self.v3.index) def as_bytes(self): return struct.pack("3I", self.v1.index, self.v2.index, self.v3.index) ### 3D Model made of vertices and triangles (linked vertices by index) ### class model: # Init a new model, empty or from a model buffer def __init__(self, model_buffer = None): self.vertices = [] self.triangles = [] # If a buffer is provided, import vertices and triangles from it if model_buffer is not None: # Bytes [0,4): One unsigned integers <vertex count> vcount = struct.unpack("1I", model_buffer[0:4])[0] # Bytes [4,8): One unsigned integers <triangle count> tcount = struct.unpack("1I", model_buffer[4:8])[0] # Bytes [8, vpos_end_byte): Three times vcount floating points <vertices position data> vpos_end_byte = 8 + vcount * 3 * 4 vpos_floats = struct.unpack("{}f".format(vcount * 3), model_buffer[8: vpos_end_byte]) # Bytes [vpos_end_byte, tind_end_byte) : Three times tcount unsigned integers <triangles indices data> tind_end_byte = vpos_end_byte + tcount * 3 * 4 tind_uints = struct.unpack("{}I".format(tcount * 3), model_buffer[vpos_end_byte: tind_end_byte]) # Add vcount vertices with positions extracted from vpos_floats for i in range(0, vcount): self.add_vertex(vpos_floats[i*3], vpos_floats[i*3 + 1], vpos_floats[i*3 + 2]) # Add tcount triangles with indices extracted from tind_uints for i in range(0, tcount): self.add_triangle(self.vertices[tind_uints[i*3]], self.vertices[tind_uints[i*3 + 1]], self.vertices[tind_uints[i*3 + 2]]) # Add new vertex def add_vertex(self, x, y, z): self.vertices.append(vert(len(self.vertices), x, y, z)) # Add new triangle from existing vertices def add_triangle(self, v1, v2, v3): self.triangles.append(triangle(v1,v2,v3)) # Get the model as string def __str__(self): res_str = "Vertices: {}\n".format(len(self.vertices)) for v in self.vertices: res_str += str(v) + "\n" res_str += "\nTriangles: {}\n".format(len(self.triangles)) for t in self.triangles: res_str += str(t) + "\n" return res_str # Get the model as bytes def as_bytes(self): packed_bytes = struct.pack("2I", len(self.vertices), len(self.triangles)) for v in self.vertices: packed_bytes += v.as_bytes() for t in self.triangles: packed_bytes += t.as_bytes() return packed_bytes ### 3D Model of a square ### class square_model(model): def __init__(self): model.__init__(self) # Le agrego vértices para formar cuadrado self.add_vertex( -1.0, -1.0, 0.0) self.add_vertex( 1.0, -1.0, 0.0) self.add_vertex( 1.0, 1.0, 0.0) self.add_vertex( -1.0, 1.0, 0.0) # Triángulos del cuadrado v = self.vertices self.add_triangle(v[0],v[1],v[2]) self.add_triangle(v[2],v[3],v[0]) ### Tests for the 3D model class ### class model_tests: def __init__(self, tested_model): ### Test exporting model as string ### # Print model as string print(str(tested_model)) ### Test exporting model as bytes ### exported_bytes = tested_model.as_bytes() # Print exported bytes print(exported_bytes) ### Test importing model from bytes ### # Create a model from the exported bytes imported_model = model(model_buffer = exported_bytes) # Print imported model as string print(str(imported_model)) tests = model_tests(square_model())
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question