def transform(self, m): v = [ tr.translate(mat([i + [1]]), self.init_p).tolist()[0][:3] for i in self.v ] v = [(mat([i + [1]]) * m).tolist()[0][:3] for i in v] self.__make_polygons(v)
def rotate_ox(p, b, a): cosa, sina = cos(a), sin(a) p = translate(p, [-b[0], -b[1], -b[2]]) p = (mat([p + [1]]) * mat([[1, 0, 0, 0], [0, cosa, sina, 0], [0, -sina, cosa, 0], [0, 0, 0, 1]])).tolist()[0][:3] return translate(p, b)
def rotate_ox(m, b, a): cosa, sina = cos(rad(a)), sin(rad(a)) rotm = translate(mat(1), [-b[0], -b[1], -b[2]]) rotm = rotm * mat([[1, 0, 0, 0], [0, cosa, sina, 0], [0, -sina, cosa, 0], [0, 0, 0, 1]]) rotm = rotm * translate(mat(1), b) return m * rotm
def project(p, type = "ortho"): if type == "ortho": return (mat([p + [1]]) * mat([[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 0, 0], [0, 0, 0, 1]])).tolist()[0][:2] elif type == "oblique": return (mat([p + [1]]) * mat([[ 1, 0, 0, 0], [ 0, 1, 0, 0], [pi/4, pi/4, 0, 0], [ 0, 0, 0, 1]])).tolist()[0][:2]
def setparams(self, params): for key in params: if key in ("name", "init_p"): exec "self.%s = params[key]" % (key,) continue if key == "mat": self.mat = mat(params[key]) continue exec """for i in range(len(self.%s)): if params[key][i] == None: continue if params[key][i] < 0: raise NegativeException("Parameters can't be negative") self.%s[i] = params[key][i]""" % ( key, key, ) if self.cylM[1] > self.cylN[0]: raise BadRadiusException("Radius cyl M must be less then radius cyl N") if self.cylD[0] > self.cylF[0]: raise BadRadiusException("Radius cyl D must be less then radius cyl F") if self.cylF[0] > self.cylH[0]: raise BadRadiusException("Radius cyl F must be less then radius cyl H") if self.mat == mat(1): self.mat = tr.translate(self.mat, self.init_p) self.namesofshapes = ( "cylA", "coneB", "coneC", "cylD", "cylE", "cylF", "cylG", "cylH", "coneI", "cylJ", "coneK", "cylL", "cylM", "cylN", ) self.c = self.init_p[:] # calculating base point (for rotation, scaling, etc.) for j in [getattr(self, i)[2] for i in self.namesofshapes]: self.c[2] += j / 2.0 tmp_init_p = [0, 0, 0] for name in self.namesofshapes: sh = getattr(self, name)[:] maxr = sqrt((tmp_init_p[2] - self.c[2]) ** 2 + (sh[0] > sh[1] and sh[0] or sh[1]) ** 2) # may cause bug self.max_radius = maxr > self.max_radius and maxr or self.max_radius # may cause bug self.shapes[name] = Cone(tmp_init_p[:], getattr(self, name)) tmp_init_p[2] += getattr(self, name)[2]
def main(m, n): from matrix import mat, show M = mat(m, n) partial(M, m, n) print(show(M), end="")
def grid ( m , n ) : e = mat( m + 1 , n + 1 , None ) e[0][0] = 1 return e
def main ( m , n ) : from matrix import mat , show M = mat( m , n ) info( M , m , n ) print( show( M ) , end = "" )
def camat((ox, oy, oz)): cosa, sina = cos(rad(-oz)), sin(rad(-oz)) cosb, sinb = cos(rad(-oy)), sin(rad(-oy)) cosc, sinc = cos(rad(-ox)), sin(rad(-ox)) mat_oz = mat([[ cosa, sina, 0, 0], [-sina, cosa, 0, 0], [ 0, 0, 1, 0], [ 0, 0, 0, 1]]) mat_oy = mat([[cosb, 0, -sinb, 0], [ 0, 1, 0, 0], [sinb, 0, cosb, 0], [ 0, 0, 0, 1]]) mat_ox = mat([[1, 0, 0, 0], [0, cosc, sinc, 0], [0, -sinc, cosc, 0], [0, 0, 0, 1]]) return (mat_oz, mat_oy, mat_ox)
def setparams(self, params): for key in params: if key in ('name', 'init_p'): exec 'self.%s = params[key]' % (key, ) continue if key == 'mat': self.mat = mat(params[key]) continue exec """for i in range(len(self.%s)): if params[key][i] == None: continue if params[key][i] < 0: raise NegativeException("Parameters can't be negative") self.%s[i] = params[key][i]""" % (key, key) if self.cylM[1] > self.cylN[0]: raise BadRadiusException( 'Radius cyl M must be less then radius cyl N') if self.cylD[0] > self.cylF[0]: raise BadRadiusException( 'Radius cyl D must be less then radius cyl F') if self.cylF[0] > self.cylH[0]: raise BadRadiusException( 'Radius cyl F must be less then radius cyl H') if self.mat == mat(1): self.mat = tr.translate(self.mat, self.init_p) self.namesofshapes = ('cylA', 'coneB', 'coneC', 'cylD', 'cylE', 'cylF', 'cylG', 'cylH', 'coneI', 'cylJ', 'coneK', 'cylL', 'cylM', 'cylN') self.c = self.init_p[:] #calculating base point (for rotation, scaling, etc.) for j in [getattr(self, i)[2] for i in self.namesofshapes]: self.c[2] += j / 2.0 tmp_init_p = [0, 0, 0] for name in self.namesofshapes: sh = getattr(self, name)[:] maxr = sqrt((tmp_init_p[2] - self.c[2])**2 + (sh[0] > sh[1] and sh[0] or sh[1])**2) #may cause bug self.max_radius = maxr > self.max_radius and maxr or self.max_radius #may cause bug self.shapes[name] = Cone(tmp_init_p[:], getattr(self, name)) tmp_init_p[2] += getattr(self, name)[2]
def grid(m, n): e = mat(m + 1, n + 1, []) e[0][0] = [[]] for i in range(1, m + 1): e[i][0] = [["a[%d]" % k for k in range(1, i + 1)]] for j in range(1, n + 1): e[0][j] = [["b[%d]" % k for k in range(1, j + 1)]] return e
def compute ( compare , m , n ) : Mo = mat( m , m ) mohanty( compare , Mo , m , n ) print( "mohanty matrix" ) print( show( Mo ) , end = "" ) d = det( Mo , m ) print( "after gaussian elimination" ) print( show( Mo ) , end = "" ) return d
def compute(compare, m, n): Mo = mat(m, m) mohanty(compare, Mo, m, n) print("mohanty matrix") print(show(Mo), end="") d = det(Mo, m) print("after gaussian elimination") print(show(Mo), end="") return d
def grid ( m , n ) : e = mat( m + 1 , n + 1 , [ ] ) e[0][0] = [ [ ] ] for i in range ( 1 , m + 1 ) : e[i][0] = [ [ "a[%d]" % k for k in range( 1 , i + 1 ) ] ] for j in range ( 1 , n + 1 ) : e[0][j] = [ [ "b[%d]" % k for k in range( 1 , j + 1 ) ] ] return e
def __init__(self, **kwargs): self.name = 'obj' self.init_p = [0, 0, 0] self.cylA = [10, 10, 5] self.coneB = [10, 30, 40] self.coneC = [30, 10, 40] self.cylD = [15, 15, 5] self.cylE = [10, 10, 15] self.cylF = [30, 30, 10] self.cylG = [10, 10, 5] self.cylH = [50, 50, 10] self.coneI = [10, 30, 100] self.cylJ = [40, 40, 20] self.coneK = [40, 70, 40] self.cylL = [70, 70, 5] self.cylM = [65, 65, 5] self.cylN = [70, 70, 10] self.mat = mat(1) self.state = 'visible' self.c = [] self.shapes = {} self.max_radius = 0 self.setparams(kwargs)
def __init__(self, **kwargs): self.pos = kwargs['pos'][:] self.dir_ = kwargs['dir_'][:] self.near_clip_z = kwargs['near_clip_z'] self.far_clip_z = kwargs['far_clip_z'] self.viewport_width = kwargs['viewport_width'] self.viewport_height = kwargs['viewport_height'] self.viewport_center_x = (self.viewport_width - 1) / 2 self.viewport_center_y = (self.viewport_height - 1) / 2 self.aspect_ratio = float(self.viewport_width) / float( self.viewport_height) if kwargs.has_key('mcam'): self.mcam = kwargs['mcam'] else: self.mcam = mat(1) self.fov = kwargs['fov'] self.viewplane_width = 2.0 self.viewplane_height = 2.0 / self.aspect_ratio self.view_dist = 0.5 * self.viewplane_width * tan(rad(self.fov / 2.0)) #init mcam: self.mcam = tr.translate(self.mcam, (-self.pos[0], -self.pos[1], -self.pos[2])) (mat_oz, mat_oy, mat_ox) = tr.camat(self.dir_) self.mcam = self.mcam * mat_oz self.mcam = self.mcam * mat_oy self.mcam = self.mcam * mat_ox
def __init__(self, **kwargs): self.name = "obj" self.init_p = [0, 0, 0] self.cylA = [10, 10, 5] self.coneB = [10, 30, 40] self.coneC = [30, 10, 40] self.cylD = [15, 15, 5] self.cylE = [10, 10, 15] self.cylF = [30, 30, 10] self.cylG = [10, 10, 5] self.cylH = [50, 50, 10] self.coneI = [10, 30, 100] self.cylJ = [40, 40, 20] self.coneK = [40, 70, 40] self.cylL = [70, 70, 5] self.cylM = [65, 65, 5] self.cylN = [70, 70, 10] self.mat = mat(1) self.state = "visible" self.c = [] self.shapes = {} self.max_radius = 0 self.setparams(kwargs)
def __init__(self, **kwargs): self.pos = kwargs["pos"][:] self.dir_ = kwargs["dir_"][:] self.near_clip_z = kwargs["near_clip_z"] self.far_clip_z = kwargs["far_clip_z"] self.viewport_width = kwargs["viewport_width"] self.viewport_height = kwargs["viewport_height"] self.viewport_center_x = (self.viewport_width - 1) / 2 self.viewport_center_y = (self.viewport_height - 1) / 2 self.aspect_ratio = float(self.viewport_width) / float(self.viewport_height) if kwargs.has_key("mcam"): self.mcam = kwargs["mcam"] else: self.mcam = mat(1) self.fov = kwargs["fov"] self.viewplane_width = 2.0 self.viewplane_height = 2.0 / self.aspect_ratio self.view_dist = 0.5 * self.viewplane_width * tan(rad(self.fov / 2.0)) # init mcam: self.mcam = tr.translate(self.mcam, (-self.pos[0], -self.pos[1], -self.pos[2])) (mat_oz, mat_oy, mat_ox) = tr.camat(self.dir_) self.mcam = self.mcam * mat_oz self.mcam = self.mcam * mat_oy self.mcam = self.mcam * mat_ox
def world2cam(self, v): return (mat([v + [1]]) * self.mcam).tolist()[0][:3]
import time import numpy as np import cv2 from collections import defaultdict import destination import path #import direction import matrix x1 = int(input("Enter starting point: ")) y1 = int(input("Enter the y coordinate: ")) #dir=input("Enter direction: ") flag = 1 sh = input("Enter shape: ") co = input("Enter color: ") s = (x1, y1) a = (matrix.mat()) print(a) if (x1 == 4 and y1 == 0): dir = 'u' a[3][4][0:2] = a[4][5][0:2] = a[5][4][0:2] = [0, 0] elif (x1 == 0 and y1 == 4): dir = 'r' a[4][3][0:2] = a[4][5][0:2] = a[5][4][0:2] = [0, 0] elif (x1 == 4 and y1 == 8): dir = 'd' a[3][4][0:2] = a[4][3][0:2] = a[5][4][0:2] = [0, 0] elif (x1 == 8 and y1 == 4): dir = 'l' a[3][4][0:2] = a[4][5][0:2] = a[4][3][0:2] = [0, 0] while (True): points = []
def scale(p, b, sx, sy, sz): return translate((mat([translate(p, [-b[0], -b[1], -b[2]]) + [1]]) * mat([[sx, 0, 0, 0], [ 0, sy, 0, 0], [ 0, 0, sz, 0], [ 0, 0, 0, 1]])).tolist()[0][:3], b)
def transform(self, m): v = [tr.translate(mat([i + [1]]), self.init_p).tolist()[0][:3] for i in self.v] v = [(mat([i + [1]]) * m).tolist()[0][:3] for i in v] self.__make_polygons(v)
def get_c(self): return (mat([self.c[:] + [1]]) * self.mat).tolist()[0][:3]
def main(m, n): from matrix import mat, show M = mat(m, n, 0) print(show(M), end="")
def translate(m, x_y_z): x, y, z = x_y_z return m * mat([[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [x, y, z, 1]])
from matrix import matrix as mat def project(p, type = "ortho"): if type == "ortho": return (mat([p + [1]]) * mat([[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 0, 0], [0, 0, 0, 1]])).tolist()[0][:2] elif type == "oblique": return (mat([p + [1]]) * mat([[ 1, 0, 0, 0], [ 0, 1, 0, 0], [pi/4, pi/4, 0, 0], [ 0, 0, 0, 1]])).tolist()[0][:2] def translate(p, (m, n, l)): return (mat([p + [1]]) * mat([[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [m, n, l, 1]])).tolist()[0][:3] def rotate_oz(p, b, a): cosa, sina = cos(a), sin(a) p = translate(p, [-b[0], -b[1], -b[2]]) p = (mat([p + [1]]) * mat([[ cosa, sina, 0, 0], [-sina, cosa, 0, 0], [ 0, 0, 1, 0], [ 0, 0, 0, 1]])).tolist()[0][:3] return translate(p, b) def rotate_oy(p, b, a): cosa, sina = cos(a), sin(a)
[ 0, 0, 0, 1]]) mat_ox = mat([[1, 0, 0, 0], [0, cosc, sinc, 0], [0, -sinc, cosc, 0], [0, 0, 0, 1]]) return (mat_oz, mat_oy, mat_ox) def project(p, prtype, c = 1000.0): if prtype == "parallel": return [p[0], p[1]] elif prtype == "central": return [p[0]/(1 - p[2] / c), p[1]/(1 - p[2] / c)] def translate(m, (x, y, z)): return m * mat([[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [x, y, z, 1]]) def rotate(m, b, (ox, oy, oz)): return rotate_oz(rotate_oy(rotate_ox(m, b, ox), b, oy), b, oz) def rotate_oz(m, b, a): cosa, sina = cos(rad(a)), sin(rad(a)) rotm = translate(mat(1), [-b[0], -b[1], -b[2]]) rotm = rotm * mat([[ cosa, sina, 0, 0], [-sina, cosa, 0, 0], [ 0, 0, 1, 0], [ 0, 0, 0, 1]]) rotm = rotm * translate(mat(1), b) return m * rotm