diff options
author | Maxime Curioni <maxime.curioni@gmail.com> | 2008-07-29 09:45:16 +0400 |
---|---|---|
committer | Maxime Curioni <maxime.curioni@gmail.com> | 2008-07-29 09:45:16 +0400 |
commit | db6388e0f2b1e03ff280615fb96b93c565c9b22a (patch) | |
tree | 3a538b4464fb6b9d6bef5df129027970f37f08c8 /source/blender/freestyle/style_modules_blender/vector.py | |
parent | e4677c409dcad94e96b2ae765422f91e0b0dd9fd (diff) |
soc-2008-mxcurioni: finished porting the Freestyle API. All of the original classes, except EdgeModifier and TimestampModifier (which aren't even ported via SWIG), are available under the Blender.Freestyle module. Testing of the porting will now begin to make sure the SWIG-less system works as the original.
Quite a few modifications were made to finish the API:
- Freestyle's SConscript was modified to catch all files within the intern/python directory, allowing integration of future shaders implemented in C++.
- the Operators class was ported, with a special care of making its methods static (using the METH_STATIC flag in the tp_methods method definitions)
- all of the type-checking functions [ BPy_[class name]_Check(obj) ] were changed to allow subclasses to be seen as that type too: instead on looking at the ob_type value, the PyObject_IsInstance function is used.
- all of the iterators can now retrieve the object pointed to by the operator, using the getObject() method. A directedViewEdge pair is returned as a list of the two elements in the pair.
- all of the style modules were copied to a style_modules_blender/ folder and were modified to use Freestyle as a Blender's submodule. IntegrationType and MediumType was also integrated (for example, changing MEAN to IntegrationType.MEAN).
Testing now begins. If everything works correctly, I'll move on to lib3ds removal right away.
Diffstat (limited to 'source/blender/freestyle/style_modules_blender/vector.py')
-rwxr-xr-x | source/blender/freestyle/style_modules_blender/vector.py | 241 |
1 files changed, 241 insertions, 0 deletions
diff --git a/source/blender/freestyle/style_modules_blender/vector.py b/source/blender/freestyle/style_modules_blender/vector.py new file mode 100755 index 00000000000..039f262546b --- /dev/null +++ b/source/blender/freestyle/style_modules_blender/vector.py @@ -0,0 +1,241 @@ +# This module defines 3d geometrical vectors with the standard +# operations on them. +# +# Written by: Konrad Hinsen +# Last revision: 1996-1-26 +# + +"""This module defines three-dimensional geometrical vectors. Vectors support +the usual mathematical operations (v1, v2: vectors, s: scalar): + v1+v2 addition + v1-v2 subtraction + v1*v2 scalar product + s*v1 multiplication with a scalar + v1/s division by a scalar + v1.cross(v2) cross product + v1.length() length + v1.normal() normal vector in direction of v1 + v1.angle(v2) angle between two vectors + v1.x(), v1[0] first element + v1.y(), v1[1] second element + v1.z(), v1[2] third element + +The module offers the following items for export: + Vec3D(x,y,z) the constructor for vectors + isVector(x) a type check function + ex, ey, ez unit vectors along the x-, y-, and z-axes (predefined constants) + +Note: vector elements can be any kind of numbers on which the operations +addition, subtraction, multiplication, division, comparison, sqrt, and acos +are defined. Integer elements are treated as floating point elements. +""" + +import math, types + +class Vec3: + + isVec3 = 1 + + def __init__(self, x=0., y=0., z=0.): + self.data = [x,y,z] + + def __repr__(self): + return 'Vec3(%s,%s,%s)' % (`self.data[0]`,\ + `self.data[1]`,`self.data[2]`) + + def __str__(self): + return `self.data` + + def __add__(self, other): + return Vec3(self.data[0]+other.data[0],\ + self.data[1]+other.data[1],self.data[2]+other.data[2]) + __radd__ = __add__ + + def __neg__(self): + return Vec3(-self.data[0], -self.data[1], -self.data[2]) + + def __sub__(self, other): + return Vec3(self.data[0]-other.data[0],\ + self.data[1]-other.data[1],self.data[2]-other.data[2]) + + def __rsub__(self, other): + return Vec3(other.data[0]-self.data[0],\ + other.data[1]-self.data[1],other.data[2]-self.data[2]) + + def __mul__(self, other): + if isVec3(other): + return reduce(lambda a,b: a+b, + map(lambda a,b: a*b, self.data, other.data)) + else: + return Vec3(self.data[0]*other, self.data[1]*other, + self.data[2]*other) + + def __rmul__(self, other): + if isVec3(other): + return reduce(lambda a,b: a+b, + map(lambda a,b: a*b, self.data, other.data)) + else: + return Vec3(other*self.data[0], other*self.data[1], + other*self.data[2]) + + def __div__(self, other): + if isVec3(other): + raise TypeError, "Can't divide by a vector" + else: + return Vec3(_div(self.data[0],other), _div(self.data[1],other), + _div(self.data[2],other)) + + def __rdiv__(self, other): + raise TypeError, "Can't divide by a vector" + + def __cmp__(self, other): + return cmp(self.data[0],other.data[0]) \ + or cmp(self.data[1],other.data[1]) \ + or cmp(self.data[2],other.data[2]) + + def __getitem__(self, index): + return self.data[index] + + def x(self): + return self.data[0] + def y(self): + return self.data[1] + def z(self): + return self.data[2] + + def length(self): + return math.sqrt(self*self) + + def normal(self): + len = self.length() + if len == 0: + raise ZeroDivisionError, "Can't normalize a zero-length vector" + return self/len + + def cross(self, other): + if not isVec3(other): + raise TypeError, "Cross product with non-vector" + return Vec3(self.data[1]*other.data[2]-self.data[2]*other.data[1], + self.data[2]*other.data[0]-self.data[0]*other.data[2], + self.data[0]*other.data[1]-self.data[1]*other.data[0]) + + def angle(self, other): + if not isVec3(other): + raise TypeError, "Angle between vector and non-vector" + cosa = (self*other)/(self.length()*other.length()) + cosa = max(-1.,min(1.,cosa)) + return math.acos(cosa) + + +class Vec2: + + isVec2 = 1 + + def __init__(self, x=0., y=0.): + self.data = [x,y] + + def __repr__(self): + return 'Vec2(%s,%s,%s)' % (`self.data[0]`,\ + `self.data[1]`) + + def __str__(self): + return `self.data` + + def __add__(self, other): + return Vec2(self.data[0]+other.data[0],\ + self.data[1]+other.data[1]) + __radd__ = __add__ + + def __neg__(self): + return Vec2(-self.data[0], -self.data[1]) + + def __sub__(self, other): + return Vec2(self.data[0]-other.data[0],\ + self.data[1]-other.data[1]) + + def __rsub__(self, other): + return Vec2(other.data[0]-self.data[0],\ + other.data[1]-self.data[1]) + + def __mul__(self, other): + if isVec2(other): + return reduce(lambda a,b: a+b, + map(lambda a,b: a*b, self.data, other.data)) + else: + return Vec2(self.data[0]*other, self.data[1]*other) + + def __rmul__(self, other): + if isVec2(other): + return reduce(lambda a,b: a+b, + map(lambda a,b: a*b, self.data, other.data)) + else: + return Vec2(other*self.data[0], other*self.data[1]) + + def __div__(self, other): + if isVec2(other): + raise TypeError, "Can't divide by a vector" + else: + return Vec2(_div(self.data[0],other), _div(self.data[1],other)) + + def __rdiv__(self, other): + raise TypeError, "Can't divide by a vector" + + def __cmp__(self, other): + return cmp(self.data[0],other.data[0]) \ + or cmp(self.data[1],other.data[1]) + + def __getitem__(self, index): + return self.data[index] + + def x(self): + return self.data[0] + def y(self): + return self.data[1] + + def length(self): + return math.sqrt(self*self) + + def normal(self): + len = self.length() + if len == 0: + raise ZeroDivisionError, "Can't normalize a zero-length vector" + return self/len + + #def cross(self, other): +# if not isVec2(other): +# raise TypeError, "Cross product with non-vector" +# return Vec2(self.data[1]*other.data[2]-self.data[2]*other.data[1], +# self.data[2]*other.data[0]-self.data[0]*other.data[2], +# self.data[0]*other.data[1]-self.data[1]*other.data[0]) + + def angle(self, other): + if not isVec2(other): + raise TypeError, "Angle between vector and non-vector" + cosa = (self*other)/(self.length()*other.length()) + cosa = max(-1.,min(1.,cosa)) + return math.acos(cosa) + + + +# Type check + +def isVec3(x): + return hasattr(x,'isVec3') + +def isVec2(x): + return hasattr(x,'isVec2') + +# "Correct" division for arbitrary number types + +def _div(a,b): + if type(a) == types.IntType and type(b) == types.IntType: + return float(a)/float(b) + else: + return a/b + + +# Some useful constants + +ex = Vec3(1.,0.,0.) +ey = Vec3(0.,1.,0.) +ez = Vec3(0.,0.,1.) |