BoxPack2D example in epydocs, small cleanup on Geometry.c

This commit is contained in:
Campbell Barton 2007-03-20 12:07:02 +00:00
parent bafc50df9b
commit 83a9a84498
6 changed files with 52 additions and 46 deletions

@ -38,9 +38,6 @@ from Blender import *
import BPyMesh
# reload(BPyMesh)
# import boxpack2d
# reload(boxpack2d) # for developing.
from math import sqrt
class prettyface(object):

@ -56,9 +56,6 @@ global USER_FILL_HOLES_QUALITY
USER_FILL_HOLES = None
USER_FILL_HOLES_QUALITY = None
# import boxpack2d
# reload(boxpack2d) # for developing.
dict_matrix = {}
def pointInTri2D(v, v1, v2, v3):

@ -44,8 +44,7 @@
/* needed for EXPP_ReturnPyObjError and EXPP_check_sequence_consistency */
#include "gen_utils.h"
//#include "util.h" /* MIN2 and MAX2 */
#include "BKE_utildefines.h"
#define SWAP_FLOAT(a,b,tmp) tmp=a; a=b; b=tmp
@ -305,7 +304,7 @@ static PyObject *M_Geometry_LineIntersect2D( PyObject * self, PyObject * args )
#define SET_BOXTOP(b, f) b->v[BL]->y = f - b->h; b->v[TR]->y = f; UPDATE_V34Y(b)
#define BOXINTERSECT(b1, b2) (!(BOXLEFT(b1)+EUL>=BOXRIGHT(b2) || BOXBOTTOM(b1)+EUL>=BOXTOP(b2) || BOXRIGHT(b1)-EUL<=BOXLEFT(b2) || BOXTOP(b1)-EUL<=BOXBOTTOM(b2) ))
#define BOXDEBUG(b) printf("\tBox Debug i %i, w:%.3f h:%.3f x:%.3f y:%.3f\n", b->index, b->w, b->h, b->x, b->y)
/* #define BOXDEBUG(b) printf("\tBox Debug i %i, w:%.3f h:%.3f x:%.3f y:%.3f\n", b->index, b->w, b->h, b->x, b->y) */
static int box_areasort(const void *p1, const void *p2)
@ -315,7 +314,6 @@ static int box_areasort(const void *p1, const void *p2)
a1 = BOXAREA(b1);
a2 = BOXAREA(b2);
/*printf("a1 a2 %f %f\n", a1, a2);*/
/* sort largest to smallest */
if ( a1 < a2 ) return 1;
@ -336,20 +334,15 @@ static int vertex_sort(const void *p1, const void *p2)
v1 = vertarray + ((int *) p1)[0];
v2 = vertarray + ((int *) p2)[0];
// self.verts.sort(key = lambda b: max(b.x+w, b.y+h) ) # Reverse area sort
a1 = MAX2(v1->x+box_width, v1->y+box_height);
a2 = MAX2(v2->x+box_width, v2->y+box_height);
/*printf("a1 a2 %f %f\n", a1, a2);*/
/* sort largest to smallest */
if ( a1 > a2 ) return 1;
else if ( a1 < a2 ) return -1;
return 0;
}
static void boxPackAll(boxPack *boxarray, int len, float *tot_width, float *tot_height)
{
boxVert *vert;
@ -544,14 +537,12 @@ static void boxPackAll(boxPack *boxarray, int len, float *tot_width, float *tot_
box->v[BL]->free &= ~(BRF|BLF);
box->v[BR]->free &= ~(BRF|BLF);
}
/* The following block of code does a logical
* check with 2 adjacent boxes, its possible to
* flag verts on one or both of the boxes
* as being used by checking the width or
* height of both boxes */
if (vert->tlb && vert->trb && (box == vert->tlb || box == vert->trb)) {
if (vert->tlb->h > vert->trb->h) {
vert->trb->v[TL]->free &= ~(TLF|BLF);

@ -29,7 +29,7 @@
*
* ***** END GPL/BL DUAL LICENSE BLOCK *****
*/
//Include this file for access to vector, quat, matrix, euler, etc...
/*Include this file for access to vector, quat, matrix, euler, etc...*/
#ifndef EXPP_Geometry_H
#define EXPP_Geometry_H

@ -10,36 +10,36 @@ This new module provides access to a geometry function.
"""
def PolyFill(polylines):
"""
Takes a list of polylines and calculates triangles that would fill in the polylines.
Multiple lines can be used to make holes inside a polyline, or fill in 2 seperate lines at once.
@type polylines: List of lists containing vectors, each representing a closed polyline.
@rtype: list
@return: a list if tuples each a tuple of 3 ints representing a triangle indexing the points given.
@note: 2D Vectors will have an assumed Z axis of zero, 4D Vectors W axis is ignored.
@note: The order of points in a polyline effect the direction returned triangles face, reverse the order of a polyline to flip the normal of returned faces.
"""
Takes a list of polylines and calculates triangles that would fill in the polylines.
Multiple lines can be used to make holes inside a polyline, or fill in 2 seperate lines at once.
@type polylines: List of lists containing vectors, each representing a closed polyline.
@rtype: list
@return: a list if tuples each a tuple of 3 ints representing a triangle indexing the points given.
@note: 2D Vectors will have an assumed Z axis of zero, 4D Vectors W axis is ignored.
@note: The order of points in a polyline effect the direction returned triangles face, reverse the order of a polyline to flip the normal of returned faces.
I{B{Example:}}
I{B{Example:}}
The example below creates 2 polylines and fills them in with faces, then makes a mesh in the current scene::
import Blender
Vector= Blender.Mathutils.Vector
The example below creates 2 polylines and fills them in with faces, then makes a mesh in the current scene::
import Blender
Vector= Blender.Mathutils.Vector
# Outline of 5 points
polyline1= [Vector(-2.0, 1.0, 1.0), Vector(-1.0, 2.0, 1.0), Vector(1.0, 2.0, 1.0), Vector(1.0, -1.0, 1.0), Vector(-1.0, -1.0, 1.0)]
polyline2= [Vector(-1, 1, 1.0), Vector(0, 1, 1.0), Vector(0, 0, 1.0), Vector(-1.0, 0.0, 1.0)]
fill= Blender.Geometry.PolyFill([polyline1, polyline2])
# Outline of 5 points
polyline1= [Vector(-2.0, 1.0, 1.0), Vector(-1.0, 2.0, 1.0), Vector(1.0, 2.0, 1.0), Vector(1.0, -1.0, 1.0), Vector(-1.0, -1.0, 1.0)]
polyline2= [Vector(-1, 1, 1.0), Vector(0, 1, 1.0), Vector(0, 0, 1.0), Vector(-1.0, 0.0, 1.0)]
fill= Blender.Geometry.PolyFill([polyline1, polyline2])
# Make a new mesh and add the truangles into it
me= Blender.Mesh.New()
me.verts.extend(polyline1)
me.verts.extend(polyline2)
me.faces.extend(fill) # Add the faces, they reference the verts in polyline 1 and 2
# Make a new mesh and add the truangles into it
me= Blender.Mesh.New()
me.verts.extend(polyline1)
me.verts.extend(polyline2)
me.faces.extend(fill) # Add the faces, they reference the verts in polyline 1 and 2
scn = Blender.Scene.GetCurrent()
ob = scn.objects.new(me)
Blender.Redraw()
"""
scn = Blender.Scene.GetCurrent()
ob = scn.objects.new(me)
Blender.Redraw()
"""
def LineIntersect2D(vec1, vec2, vec3, vec4):
"""
@ -51,10 +51,31 @@ def LineIntersect2D(vec1, vec2, vec3, vec4):
def BoxPack2D(boxlist):
"""
Takes a list of 2D boxes and packs them into a square.
Each box in boxlist must be a list of at least 4 items - [x,y,w,h], after running this script,
the X and Y values in each box will be moved to packed, non overlapping locations.
Example::
# Make 500 random boxes, pack them and make a mesh from it
from Blender import Geometry, Scene, Mesh
import random
boxes = []
for i in xrange(500):
boxes.append( [0,0, random.random()+0.1, random.random()+0.1] )
boxsize = Geometry.BoxPack2D(boxes)
print 'BoxSize', boxsize
me = Mesh.New()
for x in boxes:
me.verts.extend([(x[0],x[1], 0), (x[0],x[1]+x[3], 0), (x[0]+x[2],x[1]+x[3], 0), (x[0]+x[2],x[1], 0) ])
v1= me.verts[-1]
v2= me.verts[-2]
v3= me.verts[-3]
v4= me.verts[-4]
me.faces.extend([(v1,v2,v3,v4)])
scn = Scene.GetCurrent()
scn.objects.new(me)
@note: Each boxlist item can be longer then 4, the extra items are ignored and stay untouched.
@rtype: tuple
@return: a tuple pair - (width, height) of all the packed boxes.
"""

@ -46,7 +46,7 @@ typedef struct {
short wrapped; /* is wrapped data? */
} VectorObject;
//prototypes
/*prototypes*/
PyObject *Vector_Zero( VectorObject * self );
PyObject *Vector_Normalize( VectorObject * self );
PyObject *Vector_Negate( VectorObject * self );