X-Git-Url: http://git.osdn.net/view?a=blobdiff_plain;f=pymeshio%2Fcommon.py;h=9083fb63bf964c825daa3d48f8481e2b933d7a92;hb=b0a265cd2215c87df94c2782caad6ee0b6d26eb9;hp=c8c2d75386a92d8bfc02c5e3d66438efc137e03b;hpb=094992419e74b3946ad4362e4fc9ab6aee0ff256;p=meshio%2Fpymeshio.git diff --git a/pymeshio/common.py b/pymeshio/common.py index c8c2d75..9083fb6 100644 --- a/pymeshio/common.py +++ b/pymeshio/common.py @@ -4,8 +4,22 @@ common utilities. """ import math import struct +import sys +import io +def unicode(src): + """ + literal to unicode for python2 and python3 compatiblity. + + in python2 str to unicode. + in python3 str(as unicode) to str. + """ + if sys.version_info[0]<3: + return src.decode('utf-8') + else: + return src + """ common structures. """ @@ -24,6 +38,9 @@ class Vector2(object): def __eq__(self, rhs): return self.x==rhs.x and self.y==rhs.y + def __ne__(self, rhs): + return not self.__eq__(rhs) + def __getitem__(self, key): if key==0: return self.x @@ -51,11 +68,14 @@ class Vector3(object): self.z=z def __str__(self): - return "<%f %f %f>" % (self.x, self.y, self.z) + return "<%f %.32f %f>" % (self.x, self.y, self.z) def __eq__(self, rhs): return self.x==rhs.x and self.y==rhs.y and self.z==rhs.z + def __ne__(self, rhs): + return not self.__eq__(rhs) + def __getitem__(self, key): if key==0: return self.x @@ -221,6 +241,9 @@ class RGB(object): def __eq__(self, rhs): return self.r==rhs.r and self.g==rhs.g and self.b==rhs.b + def __ne__(self, rhs): + return not self.__eq__(rhs) + def __getitem__(self, key): if key==0: return self.r @@ -243,6 +266,12 @@ class RGBA(object): self.b=b self.a=a + def __eq__(self, rhs): + return self.r==rhs.r and self.g==rhs.g and self.b==rhs.b and self.a==rhs.a + + def __ne__(self, rhs): + return not self.__eq__(rhs) + def __getitem__(self, key): if key==0: return self.r @@ -266,6 +295,9 @@ def radian_to_degree(x): class ParseException(Exception): + """ + Exception in reader + """ pass @@ -276,19 +308,35 @@ def readall(path): return f.read() -class BinaryLoader(object): - """general BinaryLoader +class BinaryReader(object): + """general BinaryReader """ def __init__(self, ios): + current=ios.tell() + ios.seek(0, io.SEEK_END) + self.end=ios.tell() + ios.seek(current) self.ios=ios def is_end(self): - return not self.ios.readable() + #print(self.ios.tell(), self.end) + return self.ios.tell()>=self.end + #return not self.ios.readable() def unpack(self, fmt, size): result=struct.unpack(fmt, self.ios.read(size)) return result[0] + def read_int(self, size): + if size==1: + return self.unpack("b", size) + if size==2: + return self.unpack("h", size) + if size==4: + return self.unpack("i", size) + print("not reach here") + raise ParseException("invalid int size: "+size) + def read_uint(self, size): if size==1: return self.unpack("B", size) @@ -331,11 +379,18 @@ class BinaryLoader(object): ) +class WriteException(Exception): + """ + Exception in writer + """ + pass + + class BinaryWriter(object): def __init__(self, ios): self.ios=ios - def write_text(self, v, size=None): + def write_bytes(self, v, size=None): if size: self.ios.write(struct.pack("={0}s".format(size), v)) else: @@ -344,6 +399,16 @@ class BinaryWriter(object): def write_float(self, v): self.ios.write(struct.pack("f", v)) + def write_int(self, v, size): + if size==1: + self.ios.write(struct.pack("b", v)) + elif size==2: + self.ios.write(struct.pack("h", v)) + elif size==4: + self.ios.write(struct.pack("i", v)) + else: + raise WriteError("invalid int uint size") + def write_uint(self, v, size): if size==1: self.ios.write(struct.pack("B", v)) @@ -363,4 +428,7 @@ class BinaryWriter(object): def write_rgb(self, v): self.ios.write(struct.pack("=3f", v.r, v.g, v.b)) + def write_rgba(self, v): + self.ios.write(struct.pack("=4f", v.r, v.g, v.b, v.a)) +