removed unusued vars from Geometry.c

uv_archimap still had python based line intersect

added plane2matrix function to BPyMathutils
added an optional arg to imageFromObjectsOrtho - camera_matrix
camera_matrix can be used to define a plane in 3d space where X and Y scale is used to set the width and height of the area to render.
This commit is contained in:
Campbell Barton 2006-10-08 10:51:38 +00:00
parent b3a48fe82e
commit 29daa64095
4 changed files with 106 additions and 81 deletions

@ -182,3 +182,29 @@ def convexHull(point_list_2d):
# Concatenate both halfs and return. # Concatenate both halfs and return.
return [p[1] for ls in (upper, lower) for p in ls] return [p[1] for ls in (upper, lower) for p in ls]
def plane2mat(plane, normalize= False):
'''
Takes a plane and converts to a matrix
points between 0 and 1 are up
1 and 2 are right
assumes the plane has 90d corners
'''
cent= (plane[0]+plane[1]+plane[2]+plane[3] ) /4.0
up= cent - ((plane[0]+plane[1])/2.0)
right= cent - ((plane[1]+plane[2])/2.0)
z= CrossVecs(up, right)
if normalize:
up.normalize()
right.normalize()
z.normalize()
mat= Matrix(up, right, z)
# translate
mat.resize4x4()
tmat= Blender.Mathutils.TranslationMatrix(cent)
return mat * tmat

@ -3,12 +3,16 @@ from Blender import Scene, sys, Camera, Object, Image
from Blender.Scene import Render from Blender.Scene import Render
Vector= Blender.Mathutils.Vector Vector= Blender.Mathutils.Vector
def imageFromObjectsOrtho(objects, path, width, height, smooth, alpha= True): def imageFromObjectsOrtho(objects, path, width, height, smooth, alpha= True, camera_matrix= None):
''' '''
Takes any number of objects and renders them on the z axis, between x:y-0 and x:y-1 Takes any number of objects and renders them on the z axis, between x:y-0 and x:y-1
Usefull for making images from a mesh without per pixel operations Usefull for making images from a mesh without per pixel operations
- objects must be alredy placed - objects must be alredy placed
- smooth, anti alias True/False
- path renders to a PNG image - path renders to a PNG image
- alpha weather to render background as alpha
returns the blender image
''' '''
# remove an extension if its alredy there # remove an extension if its alredy there
@ -69,13 +73,83 @@ def imageFromObjectsOrtho(objects, path, width, height, smooth, alpha= True):
render_scn.setCurrentCamera(render_cam_ob) render_scn.setCurrentCamera(render_cam_ob)
render_cam_data.type= 1 # ortho render_cam_data.type= 1 # ortho
render_cam_data.scale= 1.0
# Position the camera # Position the camera
render_cam_ob.LocZ= 1.0 if camera_matrix:
render_cam_ob.LocX= 0.5 render_cam_ob.setMatrix(camera_matrix)
render_cam_ob.LocY= 0.5 # We need to take into account the matrix scaling when setting the size
# so we get the image bounds defined by the matrix
# first get the x and y factors from the matrix.
# To render the correct dimensions we must use the aspy and aspy to force the matrix scale to
# override the aspect enforced by the width and weight.
cent= Vector() * camera_matrix
xvec= Vector(1,0,0) * camera_matrix
yvec= Vector(0,1,0) * camera_matrix
# zvec= Vector(0,0,1) * camera_matrix
xlen = (cent-xvec).length # half height of the image
ylen = (cent-yvec).length # half width of the image
# zlen = (cent-zvec).length # dist to place the camera? - just use the loc for now.
# less then 1.0 portrate, 1.0 or more is portrate
asp_cam_mat= xlen/ylen # divide by zero? - possible but scripters fault.
asp_image_res= float(width)/height
#print 'asp quad', asp_cam_mat, 'asp_image', asp_image_res
#print 'xylen', xlen, ylen, 'w/h', width, height
# Setup the aspect
if asp_cam_mat > asp_image_res:
# camera is wider then image res.
# to make the image wider, reduce the aspy
asp_diff= asp_image_res/asp_cam_mat
min_asp= int(round(asp_diff * 200))
#print 'X', min_asp
elif asp_cam_mat < asp_image_res: # asp_cam_mat < asp_image_res
# camera is narrower then image res
# to make the image narrower, reduce the aspx
asp_diff= asp_cam_mat/asp_image_res
min_asp= int(round(asp_diff * 200))
#print 'Y', min_asp
else:
min_asp= 200
# set the camera size
if xlen > ylen:
if asp_cam_mat > asp_image_res:
render_context.aspectX= 200 # get the greatest range possible
render_context.aspectY= min_asp # get the greatest range possible
else:
render_context.aspectY= 200 # get the greatest range possible
render_context.aspectX= min_asp # get the greatest range possible
#print "xlen bigger"
render_cam_data.scale= xlen * 2
elif xlen < ylen:# ylen is bigger
if asp_cam_mat > asp_image_res:
render_context.aspectX= 200 # get the greatest range possible
render_context.aspectY= min_asp # get the greatest range possible
else:
render_context.aspectY= 200 # get the greatest range possible
render_context.aspectX= min_asp # get the greatest range possible
#print "ylen bigger"
render_cam_data.scale= ylen *2
else:
# asppect 1:1
#print 'NOLEN Bigger'
render_cam_data.scale= xlen * 2
#print xlen, ylen, 'xlen, ylen'
else:
render_cam_data.scale= 1.0
render_cam_ob.LocZ= 1.0
render_cam_ob.LocX= 0.5
render_cam_ob.LocY= 0.5
render_context.threads= True # good for dual core cpu's render_context.threads= True # good for dual core cpu's
render_context.render() render_context.render()
render_context.saveRenderedImage(path) render_context.saveRenderedImage(path)
@ -97,9 +171,6 @@ def imageFromObjectsOrtho(objects, path, width, height, smooth, alpha= True):
#-----------------------------------------------------------------------------# #-----------------------------------------------------------------------------#
# UV Baking functions, make a picture from mesh(es) uvs # # UV Baking functions, make a picture from mesh(es) uvs #
#-----------------------------------------------------------------------------# #-----------------------------------------------------------------------------#

@ -59,78 +59,6 @@ USER_FILL_HOLES_QUALITY = None
import boxpack2d import boxpack2d
reload(boxpack2d) # for developing. reload(boxpack2d) # for developing.
# Do 2 lines intersect?
def lineIntersection2D(x1,y1, x2,y2, _x1,_y1, _x2,_y2):
# Bounding box intersection first.
if min(x1, x2) > max(_x1, _x2) or \
max(x1, x2) < min(_x1, _x2) or \
min(y1, y2) > max(_y1, _y2) or \
max(y1, y2) < min(_y1, _y2):
return None, None # BAsic Bounds intersection TEST returns false.
# are either of the segments points? Check Seg1
if abs(x1 - x2) + abs(y1 - y2) <= SMALL_NUM:
return None, None
# are either of the segments points? Check Seg2
if abs(_x1 - _x2) + abs(_y1 - _y2) <= SMALL_NUM:
return None, None
# Make sure the HOZ/Vert Line Comes first.
if abs(_x1 - _x2) < SMALL_NUM or abs(_y1 - _y2) < SMALL_NUM:
x1, x2, y1, y2, _x1, _x2, _y1, _y2 = _x1, _x2, _y1, _y2, x1, x2, y1, y2
if abs(x2-x1) < SMALL_NUM: # VERTICLE LINE
if abs(_x2-_x1) < SMALL_NUM: # VERTICLE LINE SEG2
return None, None # 2 verticle lines dont intersect.
elif abs(_y2-_y1) < SMALL_NUM:
return x1, _y1 # X of vert, Y of hoz. no calculation.
yi = ((_y1 / abs(_x1 - _x2)) * abs(_x2 - x1)) + ((_y2 / abs(_x1 - _x2)) * abs(_x1 - x1))
if yi > max(y1, y2): # New point above seg1's vert line
return None, None
elif yi < min(y1, y2): # New point below seg1's vert line
return None, None
return x1, yi # Intersecting.
if abs(y2-y1) < SMALL_NUM: # HOZ LINE
if abs(_y2-_y1) < SMALL_NUM: # HOZ LINE SEG2
return None, None # 2 hoz lines dont intersect.
# Can skip vert line check for seg 2 since its covered above.
xi = ((_x1 / abs(_y1 - _y2)) * abs(_y2 - y1)) + ((_x2 / abs(_y1 - _y2)) * abs(_y1 - y1))
if xi > max(x1, x2): # New point right of seg1's hoz line
return None, None
elif xi < min(x1, x2): # New point left of seg1's hoz line
return None, None
return xi, y1 # Intersecting.
# ACCOUNTED FOR HOZ/VERT LINES. GO ON WITH BOTH ANGLULAR.
b1 = (y2-y1)/(x2-x1)
b2 = (_y2-_y1)/(_x2-_x1)
a1 = y1-b1*x1
a2 = _y1-b2*_x1
if b1 - b2 == 0.0:
return None, None
xi = - (a1-a2)/(b1-b2)
yi = a1+b1*xi
if (x1-xi)*(xi-x2) >= 0 and (_x1-xi)*(xi-_x2) >= 0 and (y1-yi)*(yi-y2) >= 0 and (_y1-yi)*(yi-_y2)>=0:
return xi, yi
else:
return None, None
dict_matrix = {} dict_matrix = {}
def pointInTri2D(v, v1, v2, v3): def pointInTri2D(v, v1, v2, v3):

@ -175,7 +175,7 @@ static PyObject *M_Geometry_PolyFill( PyObject * self, PyObject * args )
static PyObject *M_Geometry_LineIntersect2D( PyObject * self, PyObject * args ) static PyObject *M_Geometry_LineIntersect2D( PyObject * self, PyObject * args )
{ {
VectorObject *line_a1, *line_a2, *line_b1, *line_b2; VectorObject *line_a1, *line_a2, *line_b1, *line_b2;
float a1x, a1y, a2x, a2y, b1x, b1y, b2x, b2y, xi, yi, a1,a2,b1,b2, c1,c2, det_inv, m1, m2, newvec[2]; float a1x, a1y, a2x, a2y, b1x, b1y, b2x, b2y, xi, yi, a1,a2,b1,b2, newvec[2];
if( !PyArg_ParseTuple ( args, "O!O!O!O!", if( !PyArg_ParseTuple ( args, "O!O!O!O!",
&vector_Type, &line_a1, &vector_Type, &line_a1,
&vector_Type, &line_a2, &vector_Type, &line_a2,