OSDN Git Service

refactoring mqo use common structures
[meshio/pymeshio.git] / pymeshio / common.py
index e67580d..b0af81c 100644 (file)
@@ -32,6 +32,10 @@ class Vector2(object):
     def to_tuple(self):\r
         return (self.x, self.y)\r
 \r
+    def cross(self, rhs):\r
+        """cross(outer) product"""\r
+        return self.x*rhs.y-self.y*rhs.x\r
+\r
 \r
 class Vector3(object):\r
     """\r
@@ -59,8 +63,39 @@ class Vector3(object):
     def to_tuple(self):\r
         return (self.x, self.y, self.z)\r
 \r
-    def __add__(l, r):\r
-        return Vector3(l.x+r.x, l.y+r.y, l.z+r.z)\r
+    def __add__(self, r):\r
+        return Vector3(self.x+r.x, self.y+r.y, self.z+r.z)\r
+\r
+    def __sub__(self, rhs):\r
+        return Vector3(self.x-rhs.x, self.y-rhs.y, self.z-rhs.z)\r
+\r
+    def getSqNorm(self):\r
+        return self.x*self.x + self.y*self.y + self.z*self.z\r
+\r
+    def getNorm(self):\r
+        return math.sqrt(self.getSqNorm())\r
+\r
+    def normalize(self):\r
+        factor=1.0/self.getNorm()\r
+        self.x*=factor\r
+        self.y*=factor\r
+        self.z*=factor\r
+        return self\r
+\r
+    def to_a(self):\r
+        return [self.x, self.y, self.z]\r
+\r
+    def dot(self, rhs):\r
+        """dot(inner) product"""\r
+        return self.x*rhs.x + self.y*rhs.y + self.z*rhs.z\r
+\r
+    def cross(self, rhs):\r
+        """cross(outer) product"""\r
+        return Vector3(\r
+                self.y*rhs.z - rhs.y*self.z,\r
+                self.z*rhs.x - rhs.z*self.x,\r
+                self.x*rhs.y - rhs.x*self.y,\r
+                )\r
 \r
 \r
 class Quaternion(object):\r
@@ -225,16 +260,20 @@ class ParseException(Exception):
     pass\r
 \r
 \r
-def readall(path: str) -> bytes:\r
+def readall(path):\r
+    """read all bytes from path\r
+    """\r
     with open(path, "rb") as f:\r
         return f.read()\r
 \r
 \r
 class BinaryLoader(object):\r
+    """general BinaryLoader\r
+    """\r
     def __init__(self, io):\r
         self.io=io\r
 \r
-    def unpack(self, fmt: str, size: int) -> "read value as format":\r
+    def unpack(self, fmt, size):\r
         result=struct.unpack(fmt, self.io.read(size))\r
         return result[0]\r
 \r
@@ -279,4 +318,3 @@ class BinaryLoader(object):
                 self.read_float()\r
                 )\r
 \r
-\r