Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: data/contentcreation/orx_artists/SilvanNellen/moonsurface/BPyMathutils.py @ 5413

Last change on this file since 5413 was 4790, checked in by snellen, 18 years ago

new folder for SilvanNellen, moonsurface

File size: 6.3 KB
Line 
1# $Id: BPyMathutils.py,v 1.6 2006/12/14 14:53:32 campbellbarton Exp $
2#
3# --------------------------------------------------------------------------
4# helper functions to be used by other scripts
5# --------------------------------------------------------------------------
6# ***** BEGIN GPL LICENSE BLOCK *****
7#
8# This program is free software; you can redistribute it and/or
9# modify it under the terms of the GNU General Public License
10# as published by the Free Software Foundation; either version 2
11# of the License, or (at your option) any later version.
12#
13# This program is distributed in the hope that it will be useful,
14# but WITHOUT ANY WARRANTY; without even the implied warranty of
15# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16# GNU General Public License for more details.
17#
18# You should have received a copy of the GNU General Public License
19# along with this program; if not, write to the Free Software Foundation,
20# Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
21#
22# ***** END GPL LICENCE BLOCK *****
23# --------------------------------------------------------------------------
24
25import Blender
26from Blender.Mathutils import *
27
28# ------ Mersenne Twister - start
29
30# Copyright (C) 1997 Makoto Matsumoto and Takuji Nishimura.
31# Any feedback is very welcome. For any question, comments,
32# see http://www.math.keio.ac.jp/matumoto/emt.html or email
33# matumoto@math.keio.ac.jp
34
35# The link above is dead, this is the new one:
36# http://www.math.sci.hiroshima-u.ac.jp/m-mat/MT/emt.html
37# And here the license info, from Mr. Matsumoto's site:
38# Until 2001/4/6, MT had been distributed under GNU Public License,
39# but after 2001/4/6, we decided to let MT be used for any purpose, including
40# commercial use. 2002-versions mt19937ar.c, mt19937ar-cok.c are considered
41# to be usable freely.
42#
43# So from the year above (1997), this code is under GPL.
44
45# Period parameters
46N = 624
47M = 397
48MATRIX_A = 0x9908b0dfL   # constant vector a
49UPPER_MASK = 0x80000000L # most significant w-r bits
50LOWER_MASK = 0x7fffffffL # least significant r bits
51
52# Tempering parameters
53TEMPERING_MASK_B = 0x9d2c5680L
54TEMPERING_MASK_C = 0xefc60000L
55
56def TEMPERING_SHIFT_U(y):
57    return (y >> 11)
58
59def TEMPERING_SHIFT_S(y):
60    return (y << 7)
61
62def TEMPERING_SHIFT_T(y):
63    return (y << 15)
64
65def TEMPERING_SHIFT_L(y):
66    return (y >> 18)
67
68mt = []   # the array for the state vector
69mti = N+1 # mti==N+1 means mt[N] is not initialized
70
71# initializing the array with a NONZERO seed
72def sgenrand(seed):
73  # setting initial seeds to mt[N] using
74  # the generator Line 25 of Table 1 in
75  # [KNUTH 1981, The Art of Computer Programming
76  #    Vol. 2 (2nd Ed.), pp102]
77
78  global mt, mti
79
80  mt = []
81 
82  mt.append(seed & 0xffffffffL)
83  for i in xrange(1, N + 1):
84    mt.append((69069 * mt[i-1]) & 0xffffffffL)
85
86  mti = i
87# end sgenrand
88
89
90def genrand():
91  global mt, mti
92 
93  mag01 = [0x0L, MATRIX_A]
94  # mag01[x] = x * MATRIX_A  for x=0,1
95  y = 0
96 
97  if mti >= N: # generate N words at one time
98    if mti == N+1:   # if sgenrand() has not been called,
99      sgenrand(4357) # a default initial seed is used
100
101    for kk in xrange((N-M) + 1):
102      y = (mt[kk]&UPPER_MASK)|(mt[kk+1]&LOWER_MASK)
103      mt[kk] = mt[kk+M] ^ (y >> 1) ^ mag01[y & 0x1]
104
105    for kk in xrange(kk, N):
106      y = (mt[kk]&UPPER_MASK)|(mt[kk+1]&LOWER_MASK)
107      mt[kk] = mt[kk+(M-N)] ^ (y >> 1) ^ mag01[y & 0x1]
108
109    y = (mt[N-1]&UPPER_MASK)|(mt[0]&LOWER_MASK)
110    mt[N-1] = mt[M-1] ^ (y >> 1) ^ mag01[y & 0x1]
111
112    mti = 0
113
114  y = mt[mti]
115  mti += 1
116  y ^= TEMPERING_SHIFT_U(y)
117  y ^= TEMPERING_SHIFT_S(y) & TEMPERING_MASK_B
118  y ^= TEMPERING_SHIFT_T(y) & TEMPERING_MASK_C
119  y ^= TEMPERING_SHIFT_L(y)
120
121  return ( float(y) / 0xffffffffL ) # reals
122
123#------ Mersenne Twister -- end
124
125
126
127
128""" 2d convexhull
129Based from Dinu C. Gherman's work,
130modified for Blender/Mathutils by Campell Barton
131"""
132######################################################################
133# Public interface
134######################################################################
135from Blender.Mathutils import DotVecs
136def convexHull(point_list_2d):
137        """Calculate the convex hull of a set of vectors
138        The vectors can be 3 or 4d but only the Xand Y are used.
139        returns a list of convex hull indicies to the given point list
140        """
141
142        ######################################################################
143        # Helpers
144        ######################################################################
145
146        def _myDet(p, q, r):
147                """Calc. determinant of a special matrix with three 2D points.
148
149                The sign, "-" or "+", determines the side, right or left,
150                respectivly, on which the point r lies, when measured against
151                a directed vector from p to q.
152                """
153                return (q.x*r.y + p.x*q.y + r.x*p.y)  -  (q.x*p.y + r.x*q.y + p.x*r.y)
154       
155        def _isRightTurn((p, q, r)):
156                "Do the vectors pq:qr form a right turn, or not?"
157                #assert p[0] != q[0] and q[0] != r[0] and p[0] != r[0]
158                if _myDet(p[0], q[0], r[0]) < 0:
159                        return 1
160                else:
161                        return 0
162
163        # Get a local list copy of the points and sort them lexically.
164        points = [(p, i) for i, p in enumerate(point_list_2d)]
165       
166        try:    points.sort(key = lambda a: (a[0].x, a[0].y))
167        except: points.sort(lambda a,b: cmp((a[0].x, a[0].y), (b[0].x, b[0].y)))
168
169        # Build upper half of the hull.
170        upper = [points[0], points[1]] # cant remove these.
171        for i in xrange(len(points)-2):
172                upper.append(points[i+2])
173                while len(upper) > 2 and not _isRightTurn(upper[-3:]):
174                        del upper[-2]
175
176        # Build lower half of the hull.
177        points.reverse()
178        lower = [points.pop(0), points.pop(1)]
179        for p in points:
180                lower.append(p)
181                while len(lower) > 2 and not _isRightTurn(lower[-3:]):
182                        del lower[-2]
183       
184        # Concatenate both halfs and return.
185        return [p[1] for ls in (upper, lower) for p in ls]
186
187
188def plane2mat(plane, normalize= False):
189        '''
190        Takes a plane and converts to a matrix
191        points between 0 and 1 are up
192        1 and 2 are right
193        assumes the plane has 90d corners
194        '''
195        cent= (plane[0]+plane[1]+plane[2]+plane[3] ) /4.0
196
197       
198        up= cent - ((plane[0]+plane[1])/2.0)
199        right= cent - ((plane[1]+plane[2])/2.0)
200        z= CrossVecs(up, right)
201       
202        if normalize:
203                up.normalize()
204                right.normalize()
205                z.normalize()
206       
207        mat= Matrix(up, right, z)
208       
209        # translate
210        mat.resize4x4()
211        tmat= Blender.Mathutils.TranslationMatrix(cent)
212        return mat * tmat
Note: See TracBrowser for help on using the repository browser.