Skip to content
Snippets Groups Projects
Blocks.py 64.6 KiB
Newer Older
  • Learn to ignore specific revisions
  • # ***** BEGIN GPL LICENSE BLOCK *****
    #
    # This program is free software; you may redistribute it, and/or
    # modify it, under the terms of the GNU General Public License
    # as published by the Free Software Foundation - either version 2
    # of the License, or (at your option) any later version.
    #
    # This program is distributed in the hope that it will be useful,
    # but WITHOUT ANY WARRANTY; without even the implied warranty of
    # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    # GNU General Public License for more details.
    #
    # You should have received a copy of the GNU General Public License
    # along with this program. If not, write to:
    #
    
    Campbell Barton's avatar
    Campbell Barton committed
    #   the Free Software Foundation Inc.
    #   51 Franklin Street, Fifth Floor
    #   Boston, MA 02110-1301, USA
    
    #
    # or go online at: http://www.gnu.org/licenses/ to view license options.
    #
    # ***** END GPL LICENCE BLOCK *****
    
    
    ##
    #
    # Module notes:
    #
    # Grout needs to be implemented.
    # consider removing wedge crit for small "c" and "cl" values
    # wrap around for openings on radial stonework?
    # auto-clip wall edge to SMALL for radial and domes.
    # unregister doesn't release all references.
    # repeat for opening doesn't distribute evenly when radialized - see wrap around
    #   note above.
    # if opening width == indent*2 the edge blocks fail (row of blocks cross opening).
    # if openings overlap fills inverse with blocks - see h/v slots.
    # Negative grout width creates a pair of phantom blocks, seperated by grout
    #   width, inside the edges.
    # if block width variance is 0, and edging is on, right edge blocks create a "vertical seam".
    #
    ##
    
    # <pep8-80 compliant>
    
    import bpy, time, math
    from random import random
    from math import fmod, sqrt, sin, cos, atan
    
    #A few constants
    SMALL = 0.000000000001
    
    Campbell Barton's avatar
    Campbell Barton committed
    NOTZERO = 0.01 # for values that must be != 0; see UI options/variables -
    
    # sort of a bug to be fixed.
    PI = math.pi
    
    #global variables
    
    #General masonry Settings
    
    Campbell Barton's avatar
    Campbell Barton committed
    settings = {'w': 1.2, 'wv': 0.3, 'h': .6, 'hv': 0.3, 'd': 0.3, 'dv': 0.1,
                'g': 0.1, 'gv': 0.07, 'gd': 0.01, 'gdv': 0.0, 'b': 0, 'bv': 0,
                'f': 0.0, 'fv': 0.0, 't': 0.0, 'sdv': 0.1, 'hwt': 0.5, 'aln':0,
                'wm': 0.8, 'hm': 0.3, 'dm':0.1,
                'woff':0.0, 'woffv':0.0, 'eoff':0.3, 'eoffv':0.0, 'rwhl':1,
    
                'hb':0, 'ht':0, 'ge':0, 'physics':0}
    
    Campbell Barton's avatar
    Campbell Barton committed
    # 'w':width 'wv':widthVariation
    # 'h':height 'hv':heightVariation
    
    # 'd':depth 'dv':depthVariation
    
    Campbell Barton's avatar
    Campbell Barton committed
    # 'g':grout 'gv':groutVariation 'gd':groutDepth 'gdv':groutDepthVariation
    
    # 'b':bevel 'bv':bevelVariation
    
    Campbell Barton's avatar
    Campbell Barton committed
    # 'f':flawSize 'fv':flawSizeVariation 'ff':flawFraction
    # 't':taper
    
    # 'sdv':subdivision(distance or angle)
    
    Campbell Barton's avatar
    Campbell Barton committed
    # 'hwt':row height effect on block widths in the row (0=no effect,
    
    #     1=1:1 relationship, negative values allowed, 0.5 works well)
    # 'aln':alignment(0=none, 1=rows w/features, 2=features w/rows)
    #     (currently un-used)
    # 'wm':width minimum 'hm':height minimum 'dm':depth minimum
    
    Campbell Barton's avatar
    Campbell Barton committed
    # 'woff':row start offset(fraction of width)
    
    # 'woffv':width offset variation(fraction of width)
    
    Campbell Barton's avatar
    Campbell Barton committed
    # 'eoff':edge offset 'eoffv':edge offset variation
    
    # 'rwhl':row height lock(1 is all blocks in row have same height)
    # 'hb':bottom row height 'ht': top row height 'ge': grout the edges
    # 'physics': set up for physics
    
    # dims = area of wall (face)
    dims = {'s':0, 'e':PI*3/2, 'b':0.1, 't':12.3} # radial
    # 's':start x or theta 'e':end x or theta 'b':bottom z or r 't':top z or r
    # 'w' = e-s and h = t-b; calculated to optimize for various operations/usages
    #dims = {'s':-12, 'e':15, 'w':27, 'b':-15., 't':15., 'h':30}
    #dims = {'s':-bayDim/2, 'e':bayDim/2, 'b':-5., 't':10.} # bay settings?
    
    radialized = 0 # Radiating from one point - round/disc; instead of square
    slope = 0 # Warp/slope; curved over like a vaulted tunnel
    
    Campbell Barton's avatar
    Campbell Barton committed
    # 'bigblock': merge adjacent blocks into single large blocks
    
    bigBlock = 0 # Merge blocks
    
    # Gaps in blocks for various apertures.
    #openingSpecs = []
    
    Campbell Barton's avatar
    Campbell Barton committed
    openingSpecs = [{'w':0.5, 'h':0.5, 'x':0.8, 'z':2.7, 'rp':1, 'b':0.0,
    
                     'v':0, 'vl':0, 't':0, 'tl':0}]
    
    Campbell Barton's avatar
    Campbell Barton committed
    # 'w': opening width, 'h': opening height,
    # 'x': horizontal position, 'z': vertical position,
    # 'rp': make multiple openings, with a spacing of x,
    
    # 'b': bevel the opening, inside only, like an arrow slit.
    # 'v': height of the top arch, 'vl':height of the bottom arch,
    # 't': thickness of the top arch, 'tl': thickness of the bottom arch
    
    # Add blocks to make platforms.
    shelfExt = 0
    #shelfSpecs = []
    shelfSpecs = {'w':0.5, 'h':0.5, 'd': 0.3, 'x':0.8, 'z':2.7}
    # 'w': block width, 'h': block height, 'd': block depth (shelf size; offset from wall)
    # 'x': horizontal start position, 'z': vertical start position
    
    # Add blocks to make steps.
    stepMod = 0
    stepSpecs = {'x':0.0, 'z':-10, 'w':10.0, 'h':10.0,
    
    Campbell Barton's avatar
    Campbell Barton committed
        'v':0.7, 't':1.0, 'd':1.0 }
    
    # 'x': horizontal start position, 'z': vertical start position,
    # 'w': step area width, 'h': step area height,
    # 'v': riser height, 't': tread width, 'd': block depth (step size; offset from wall)
    
    
        #easier way to get to the random function
    def rnd(): return random()
    
        #random number from -0.5 to 0.5
    def rndc(): return (random() - 0.5)
    
        #random number from -1.0 to 1.0
    def rndd(): return (random() - 0.5)*2.0
    
    
    #Opening Test suite
    #opening test function
    
    def test(TestN = 13):
        dims = {'s':-29., 'e':29., 'b':-6., 't':TestN*7.5}
        openingSpecs = []
        for i in range(TestN):
            x = (random() - 0.5) * 6
            z = i*7.5
            v = .2 + i*(3./TestN)
            vl = 3.2 - i*(3./TestN)
            t = 0.3 + random()
            tl = 0.3 + random()
            rn = random()*2
    
    Campbell Barton's avatar
    Campbell Barton committed
            openingSpecs += [{'w':3.1 + rn, 'h':0.3 + rn, 'x':float(x),
                              'z':float(z), 'rp':0, 'b':0.,
                              'v':float(v), 'vl':float(vl),
    
                              't':float(t), 'tl':float(tl)}]
        return dims, openingSpecs
    
    #dims, openingSpecs = test(15)
    
    
    #For filling a linear space with divisions
    
    Campbell Barton's avatar
    Campbell Barton committed
    def fill(left, right, avedst, mindst=0.0, dev=0.0, pad=(0.0,0.0), num=0,
    
    Campbell Barton's avatar
    Campbell Barton committed
        Fills a linear range with points and returns an ordered list of those points
        including the end points.
    
        left: the lower boundary
        right: the upper boundary
        avedst: the average distance between points
        mindst: the minimum distance between points
        dev: the maximum random deviation from avedst
        pad: tends to move the points near the bounds right (positive) or
            left (negative).
            element 0 pads the lower bounds, element 1 pads the upper bounds
        num: substitutes a numerical limit for the right limit.  fill will then make
            a num+1 element list
        center: flag to center the elements in the range, 0 == disabled
    
    
        poslist = [left]
        curpos = left+pad[0]
    
    
    Campbell Barton's avatar
    Campbell Barton committed
        # Set offset by average spacing, then add blocks (fall through);
    
        # if not at right edge.
        if center:
            curpos += ((right-left-mindst*2)%avedst)/2+mindst
            if curpos-poslist[-1]<mindst: curpos = poslist[-1]+mindst+rnd()*dev/2
    
            # clip to right edge.
            if (right-curpos<mindst) or (right-curpos< mindst-pad[1]):
                poslist.append(right)
                return poslist
    
            else: poslist.append(curpos)
    
        #unused... for now.
        if num:
            idx = len(poslist)
    
            while idx<num+1:
                curpos += avedst+rndd()*dev
                if curpos-poslist[-1]<mindst:
                    curpos = poslist[-1]+mindst+rnd()*dev/2
                poslist.append(curpos)
                idx += 1
    
            return poslist
    
        # make block edges
        else:
    
    Campbell Barton's avatar
    Campbell Barton committed
            while True: # loop for blocks
                curpos += avedst+rndd()*dev
    
                if curpos-poslist[-1]<mindst:
                    curpos = poslist[-1]+mindst+rnd()*dev/2
                # close off edges at limit
                if (right-curpos<mindst) or (right-curpos< mindst-pad[1]):
                    poslist.append(right)
                    return poslist
    
                else: poslist.append(curpos)
    
    
    #For generating block geometry
    
    Campbell Barton's avatar
    Campbell Barton committed
    def MakeABlock(bounds, segsize, vll=0, Offsets=None, FaceExclude=[],
    
                   bevel=0, xBevScl=1):
    
    Campbell Barton's avatar
    Campbell Barton committed
        MakeABlock returns lists of points and faces to be made into a square
    
                cornered block, subdivided along the length, with optional bevels.
    
    Campbell Barton's avatar
    Campbell Barton committed
        bounds: a list of boundary positions:
    
            0:left, 1:right, 2:bottom, 3:top, 4:back, 5:front
    
    Campbell Barton's avatar
    Campbell Barton committed
        segsize: the maximum size before lengthwise subdivision occurs
        vll: the number of vertexes already in the mesh. len(mesh.verts) should
    
    Campbell Barton's avatar
    Campbell Barton committed
        Offsets: list of coordinate delta values.
            Offsets are lists, [x,y,z] in
                [
                0:left_bottom_back,
                1:left_bottom_front,
                2:left_top_back,
                3:left_top_front,
                4:right_bottom_back,
                5:right_bottom_front,
                6:right_top_back,
                7:right_top_front,
                ]
    
        FaceExclude: list of faces to exclude from the faces list.  see bounds above for indices
    
    Campbell Barton's avatar
    Campbell Barton committed
        xBevScl: how much to divide the end (+- x axis) bevel dimensions.  Set to current average radius to compensate for angular distortion on curved blocks
    
    
        slices = fill(bounds[0], bounds[1], segsize, segsize, center=1)
        points = []
        faces = []
    
        if Offsets == None:
            points.append([slices[0],bounds[4],bounds[2]])
            points.append([slices[0],bounds[5],bounds[2]])
            points.append([slices[0],bounds[5],bounds[3]])
            points.append([slices[0],bounds[4],bounds[3]])
    
            for x in slices[1:-1]:
                points.append([x,bounds[4],bounds[2]])
                points.append([x,bounds[5],bounds[2]])
                points.append([x,bounds[5],bounds[3]])
                points.append([x,bounds[4],bounds[3]])
    
            points.append([slices[-1],bounds[4],bounds[2]])
            points.append([slices[-1],bounds[5],bounds[2]])
            points.append([slices[-1],bounds[5],bounds[3]])
            points.append([slices[-1],bounds[4],bounds[3]])
    
        else:
            points.append([slices[0]+Offsets[0][0],bounds[4]+Offsets[0][1],bounds[2]+Offsets[0][2]])
            points.append([slices[0]+Offsets[1][0],bounds[5]+Offsets[1][1],bounds[2]+Offsets[1][2]])
            points.append([slices[0]+Offsets[3][0],bounds[5]+Offsets[3][1],bounds[3]+Offsets[3][2]])
            points.append([slices[0]+Offsets[2][0],bounds[4]+Offsets[2][1],bounds[3]+Offsets[2][2]])
    
            for x in slices[1:-1]:
                xwt = (x-bounds[0])/(bounds[1]-bounds[0])
                points.append([x+Offsets[0][0]*(1-xwt)+Offsets[4][0]*xwt,bounds[4]+Offsets[0][1]*(1-xwt)+Offsets[4][1]*xwt,bounds[2]+Offsets[0][2]*(1-xwt)+Offsets[4][2]*xwt])
                points.append([x+Offsets[1][0]*(1-xwt)+Offsets[5][0]*xwt,bounds[5]+Offsets[1][1]*(1-xwt)+Offsets[5][1]*xwt,bounds[2]+Offsets[1][2]*(1-xwt)+Offsets[5][2]*xwt])
                points.append([x+Offsets[3][0]*(1-xwt)+Offsets[7][0]*xwt,bounds[5]+Offsets[3][1]*(1-xwt)+Offsets[7][1]*xwt,bounds[3]+Offsets[3][2]*(1-xwt)+Offsets[7][2]*xwt])
                points.append([x+Offsets[2][0]*(1-xwt)+Offsets[6][0]*xwt,bounds[4]+Offsets[2][1]*(1-xwt)+Offsets[6][1]*xwt,bounds[3]+Offsets[2][2]*(1-xwt)+Offsets[6][2]*xwt])
    
            points.append([slices[-1]+Offsets[4][0],bounds[4]+Offsets[4][1],bounds[2]+Offsets[4][2]])
            points.append([slices[-1]+Offsets[5][0],bounds[5]+Offsets[5][1],bounds[2]+Offsets[5][2]])
            points.append([slices[-1]+Offsets[7][0],bounds[5]+Offsets[7][1],bounds[3]+Offsets[7][2]])
            points.append([slices[-1]+Offsets[6][0],bounds[4]+Offsets[6][1],bounds[3]+Offsets[6][2]])
    
        faces.append([vll,vll+3,vll+2,vll+1])
    
        for x in range(len(slices)-1):
            faces.append([vll,vll+1,vll+5,vll+4])
            vll+=1
            faces.append([vll,vll+1,vll+5,vll+4])
            vll+=1
            faces.append([vll,vll+1,vll+5,vll+4])
            vll+=1
            faces.append([vll,vll-3,vll+1,vll+4])
            vll+=1
    
        faces.append([vll,vll+1,vll+2,vll+3])
    
        return points, faces
    #
    #
    #
    
    #For generating Keystone Geometry
    def MakeAKeystone(xpos, width, zpos, ztop, zbtm, thick, bevel, vll=0, FaceExclude=[], xBevScl=1):
    
    Campbell Barton's avatar
    Campbell Barton committed
        MakeAKeystone returns lists of points and faces to be made into a square cornered keystone, with optional bevels.
        xpos: x position of the centerline
        width: x width of the keystone at the widest point (discounting bevels)
        zpos: z position of the widest point
        ztop: distance from zpos to the top
        zbtm: distance from zpos to the bottom
        thick: thickness
    
        bevel: the amount to raise the back vertex to account for arch beveling
    
    Campbell Barton's avatar
    Campbell Barton committed
        vll: the number of vertexes already in the mesh. len(mesh.verts) should give this number
        faceExclude: list of faces to exclude from the faces list.  0:left, 1:right, 2:bottom, 3:top, 4:back, 5:front
        xBevScl: how much to divide the end (+- x axis) bevel dimensions.  Set to current average radius to compensate for angular distortion on curved blocks
    
    
        points = []
        faces = []
        faceinclude = [1 for x in range(6)]
        for x in FaceExclude: faceinclude[x]=0
        Top = zpos + ztop
        Btm = zpos - zbtm
        Wid = width/2.
        Thk = thick/2.
    
    Campbell Barton's avatar
    Campbell Barton committed
    
    
        # The front top point
        points.append([xpos, Thk, Top])
        # The front left point
        points.append([xpos-Wid, Thk, zpos])
        # The front bottom point
        points.append([xpos, Thk, Btm])
        # The front right point
        points.append([xpos+Wid, Thk, zpos])
    
    Campbell Barton's avatar
    Campbell Barton committed
    
    
        MirrorPoints = []
        for i in points:
            MirrorPoints.append([i[0],-i[1],i[2]])
        points += MirrorPoints
        points[6][2] += bevel
    
        faces.append([3,2,1,0])
        faces.append([4,5,6,7])
        faces.append([4,7,3,0])
        faces.append([5,4,0,1])
        faces.append([6,5,1,2])
        faces.append([7,6,2,3])
        # Offset the vertex numbers by the number of verticies already in the list
        for i in range(len(faces)):
            for j in range(len(faces[i])): faces[i][j] += vll
    
        return points, faces
    
    
    #for finding line/circle intercepts
    def circ(offs=0.,r=1.):
        __doc__ = """\
    
    Campbell Barton's avatar
    Campbell Barton committed
        offs is the distance perpendicular to the line to the center of the circle
        r is the radius of the circle
        circ returns the distance paralell to the line to the center of the circle at the intercept.
        """
    
        offs = abs(offs)
        if offs > r: return None
        elif offs == r: return 0.
        else: return sqrt(r**2 - offs**2)
    
    
    #class openings in the wall
    class opening:
        __doc__ = """\
    
    Campbell Barton's avatar
    Campbell Barton committed
        This is the class for holding the data for the openings in the wall.
        It has methods for returning the edges of the opening for any given position value,
        as well as bevel settings and top and bottom positions.
        It stores the 'style' of the opening, and all other pertinent information.
        """
    
        # x = 0. # x position of the opening
        # z = 0. # x position of the opening
        # w = 0. # width of the opening
        # h = 0. # height of the opening
        r = 0  # top radius of the arch (derived from 'v')
        rl = 0 # lower radius of the arch (derived from 'vl')
        rt = 0 # top arch thickness
        rtl = 0# lower arch thickness
        ts = 0 # Opening side thickness, if greater than average width, replaces it.
        c = 0  # top arch corner position (for low arches), distance from the top of the straight sides
        cl = 0 # lower arch corner position (for low arches), distance from the top of the straight sides
        # form = 0 # arch type (unused for now)
        # b = 0. # back face bevel distance, like an arrow slit
        v = 0. # top arch height
        vl = 0.# lower arch height
        # variable "s" is used for "side" in the "edge" function.
        # it is a signed int, multiplied by the width to get + or - of the center
    
        def btm(self):
            if self.vl <= self.w/2 : return self.z-self.h/2-self.vl-self.rtl
    
    Campbell Barton's avatar
    Campbell Barton committed
            else: return self.z - sqrt((self.rl+self.rtl)**2 - (self.rl - self.w/2 )**2)  - self.h/2
    
    
    
        def top(self):
            if self.v <= self.w/2 : return self.z+self.h/2+self.v+self.rt
    
    Campbell Barton's avatar
    Campbell Barton committed
            else: return sqrt((self.r+self.rt)**2 - (self.r - self.w/2 )**2) + self.z + self.h/2
    
    
    
        #crits returns the critical split points, or discontinuities, used for making rows
        def crits(self):
            critlist = []
            if self.vl>0: # for lower arch
                # add the top point if it is pointed
                #if self.vl >= self.w/2.: critlist.append(self.btm())
                if self.vl < self.w/2.:#else: for low arches, with wedge blocks under them
                    #critlist.append(self.btm())
                    critlist.append(self.z-self.h/2 - self.cl)
    
            if self.h>0: # if it has a height, append points at the top and bottom of the main square section
                critlist += [self.z-self.h/2,self.z+self.h/2]
            else:  # otherwise, append just one in the center
                critlist.append(self.z)
    
            if self.v>0:  # for the upper arch
                if self.v < self.w/2.: # add the splits for the upper wedge blocks, if needed
                    critlist.append(self.z+self.h/2 + self.c)
                    #critlist.append(self.top())
                #otherwise just add the top point, if it is pointed
                #else: critlist.append(self.top())
    
            return critlist
    
    
        # get the side position of the opening.
        # ht is the z position; s is the side: 1 for right, -1 for left
        # if the height passed is above or below the opening, return None
        #
        def edgeS(self, ht, s):
            # set the row radius: 1 for standard wall (flat)
            if radialized:
                if slope: r1 = abs(dims['t']*sin(ht*PI/(dims['t']*2)))
                else: r1 = abs(ht)
            else: r1 = 1
    
            #Go through all the options, and return the correct value
            if ht < self.btm(): #too low
                return None
            elif ht > self.top(): #too high
                return None
    
            # Check for circ returning None - prevent TypeError (script failure) with float.
    
            # in this range, pass the lower arch info
            elif ht <= self.z-self.h/2-self.cl:
                if self.vl > self.w/2:
                    circVal = circ(ht-self.z+self.h/2,self.rl+self.rtl)
                    if circVal == None:
                        return None
                    else: return self.x + s*(self.w/2.-self.rl+circVal)/r1
                else:
                    circVal = circ(ht-self.z+self.h/2+self.vl-self.rl,self.rl+self.rtl)
                    if circVal == None:
                        return None
                    else: return self.x + s*circVal/r1
    
            #in this range, pass the top arch info
            elif ht >= self.z+self.h/2+self.c:
                if self.v > self.w/2:
                    circVal = circ(ht-self.z-self.h/2,self.r+self.rt)
                    if circVal == None:
                        return None
                    else: return self.x + s*(self.w/2.-self.r+circVal)/r1
                else:
                    circVal = circ(ht-(self.z+self.h/2+self.v-self.r),self.r+self.rt)
                    if circVal == None:
                        return None
                    else: return self.x + s*circVal/r1
    
            #in this range pass the lower corner edge info
            elif ht <= self.z-self.h/2:
                d = sqrt(self.rtl**2 - self.cl**2)
                if self.cl > self.rtl/sqrt(2.): return self.x + s*(self.w/2 + (self.z - self.h/2 - ht)*d/self.cl)/r1
                else: return self.x + s*( self.w/2 + d )/r1
    
            #in this range pass the upper corner edge info
            elif ht >= self.z+self.h/2:
                d = sqrt(self.rt**2 - self.c**2)
                if self.c > self.rt/sqrt(2.): return self.x + s*(self.w/2 + (ht - self.z - self.h/2 )*d/self.c)/r1
                else: return self.x + s*( self.w/2 + d )/r1
    
            #in this range, pass the middle info (straight sides)
            else: return self.x + s*self.w/2/r1
    
    
        # get the top or bottom of the opening
        # ht is the x position; s is the side: 1 for top, -1 for bottom
        #
        def edgeV(self, ht, s):
            dist = abs(self.x-ht)
            def radialAdjust(dist, sideVal):
    
                """take the distance and adjust for radial geometry, return dist.
                """
    
                if radialized:
                    if slope:
                        dist = dist * abs(dims['t']*sin(sideVal*PI/(dims['t']*2)))
                    else:
                        dist = dist * sideVal
                return dist
    
    Campbell Barton's avatar
    Campbell Barton committed
    
    
            if s > 0 :#and (dist <= self.edgeS(self.z+self.h/2+self.c,1)-self.x): #check top down
                #hack for radialized masonry, import approx Z instead of self.top()
                dist = radialAdjust(dist, self.top())
    
                #no arch on top, flat
                if not self.r: return self.z+self.h/2
    
                #pointed arch on top
                elif self.v > self.w/2:
                    circVal = circ(dist-self.w/2+self.r,self.r+self.rt)
                    if circVal == None:
                        return None
                    else: return self.z+self.h/2+circVal
    
                #domed arch on top
                else:
                    circVal = circ(dist,self.r+self.rt)
                    if circVal == None:
                        return None
                    else: return self.z+self.h/2+self.v-self.r+circVal
    
            else:#and (dist <= self.edgeS(self.z-self.h/2-self.cl,1)-self.x): #check bottom up
                #hack for radialized masonry, import approx Z instead of self.top()
                dist = radialAdjust(dist, self.btm())
    
                #no arch on bottom
                if not self.rl: return self.z-self.h/2
    
                #pointed arch on bottom
                elif self.vl > self.w/2:
                    circVal = circ(dist-self.w/2+self.rl,self.rl+self.rtl)
                    if circVal == None:
                        return None
                    else: return self.z-self.h/2-circVal
    
    
    Campbell Barton's avatar
    Campbell Barton committed
                #old conditional? if (dist-self.w/2+self.rl)<=(self.rl+self.rtl):
    
                #domed arch on bottom
                else:
                    circVal = circ(dist,self.rl+self.rtl) # dist-self.w/2+self.rl
                    if circVal == None:
                        return None
                    else: return self.z-self.h/2-self.vl+self.rl-circVal
    
    
    Campbell Barton's avatar
    Campbell Barton committed
        # and this never happens - but, leave it as failsafe :)
    
            print("got all the way out of the edgeV!  Not good!")
            print("opening x = ", self.x, ", opening z = ", self.z)
            return 0.0
        #
        def edgeBev(self, ht):
            if ht > (self.z + self.h/2): return 0.0
            if ht < (self.z - self.h/2): return 0.0
            if radialized:
                if slope: r1 = abs(dims['t']*sin(ht*PI/(dims['t']*2)))
                else: r1 = abs(ht)
            else: r1 = 1
            bevel = self.b / r1
            return bevel
    #
    ##
    #
    
        def __init__(self, xpos, zpos, width, height, archHeight=0, archThk=0,
                     archHeightLower=0, archThkLower=0, bevel=0, edgeThk=0):
            self.x = float(xpos)
            self.z = float(zpos)
            self.w = float(width)
            self.h = float(height)
            self.rt = archThk
            self.rtl = archThkLower
            self.v = archHeight
            self.vl = archHeightLower
            if self.w <= 0: self.w = SMALL
    
            #find the upper arch radius
            if archHeight >= width/2:
                # just one arch, low long
                self.r = (self.v**2)/self.w + self.w/4
            elif archHeight <= 0:
                # No arches
                self.r = 0
                self.v = 0
            else:
                # Two arches
                self.r = (self.w**2)/(8*self.v) + self.v/2.
                self.c = self.rt*cos(atan(self.w/(2*(self.r-self.v))))
    
            #find the lower arch radius
            if archHeightLower >= width/2:
                self.rl = (self.vl**2)/self.w + self.w/4
            elif archHeightLower <= 0:
                self.rl = 0
                self.vl = 0
            else:
                self.rl = (self.w**2)/(8*self.vl) + self.vl/2.
                self.cl = self.rtl*cos(atan(self.w/(2*(self.rl-self.vl))))
    
            #self.form = something?
            self.b = float(bevel)
            self.ts = edgeThk
    #
    #
    
    #class for the whole wall boundaries; a sub-class of "opening"
    class OpeningInv(opening):
        #this is supposed to switch the sides of the opening
        #so the wall will properly enclose the whole wall.
        #We'll see if it works.
    
        def edgeS(self, ht, s):
            return opening.edgeS(self, ht, -s)
    
        def edgeV(self, ht, s):
            return opening.edgeV(self, ht, -s)
    
    #class rows in the wall
    class rowOb:
        __doc__ = """\
    
    Campbell Barton's avatar
    Campbell Barton committed
        This is the class for holding the data for individual rows of blocks.
        each row is required to have some edge blocks, and can also have
        intermediate sections of "normal" blocks.
        """
    
    
        #z = 0.
        #h = 0.
        radius = 1
        EdgeOffset = 0.
    #    BlocksEdge = []
    #    RowSegments = []
    #    BlocksNorm = []
    
        def FillBlocks(self):
            # Set the radius variable, in the case of radial geometry
            if radialized:
                if slope: self.radius = dims['t']*(sin(self.z*PI/(dims['t']*2)))
                else: self.radius = self.z
    
            #initialize internal variables from global settings
    
            SetH = settings['h']
            SetHwt = settings['hwt']
            SetWid = settings['w']
            SetWidMin = settings['wm']
            SetWidVar = settings['wv']
            SetGrt = settings['g']
            SetGrtVar = settings['gv']
            SetRowHeightLink = settings['rwhl']
            SetDepth = settings['d']
            SetDepthVar = settings['dv']
    
            #height weight, used for making shorter rows have narrower blocks, and vice-versa
            hwt = ((self.h/SetH-1)*SetHwt+1)
    
            # set variables for persistent values: loop optimization, readability, single ref for changes.
    
            avgDist = hwt*SetWid/self.radius
            minDist = SetWidMin/self.radius
            deviation = hwt*SetWidVar/self.radius
            grtOffset = SetGrt/(2*self.radius)
    
            # init loop variables that may change...
    
            grt = (SetGrt + rndc()*SetGrtVar)/(self.radius)
            ThisBlockHeight = self.h+rndc()*(1-SetRowHeightLink)*SetGrtVar
            ThisBlockDepth = rndd()*SetDepthVar+SetDepth
    
            for segment in self.RowSegments:
                divs = fill(segment[0]+grtOffset, segment[1]-grtOffset, avgDist, minDist, deviation)
    
                #loop through the divisions, adding blocks for each one
                for i in range(len(divs)-1):
                    ThisBlockx = (divs[i]+divs[i+1])/2
                    ThisBlockw = divs[i+1]-divs[i]-grt
    
                    self.BlocksNorm.append([ThisBlockx, self.z, ThisBlockw, ThisBlockHeight, ThisBlockDepth, None])
    
                    if SetDepthVar: # vary depth
                        ThisBlockDepth = rndd()*SetDepthVar+SetDepth
    
                    if SetGrtVar: # vary grout
                        grt = (SetGrt + rndc()*SetGrtVar)/(self.radius)
                        ThisBlockHeight = self.h+rndc()*(1-SetRowHeightLink)*SetGrtVar
    
    
        def __init__(self,centerheight,rowheight,edgeoffset = 0.):
            self.z = float(centerheight)
            self.h = float(rowheight)
            self.EdgeOffset = float(edgeoffset)
    
    #THIS INITILIZATION IS IMPORTANT!  OTHERWISE ALL OBJECTS WILL HAVE THE SAME LISTS!
            self.BlocksEdge = []
            self.RowSegments = []
            self.BlocksNorm = []
    
    #
    def arch(ra,rt,x,z, archStart, archEnd, bevel, bevAngle, vll):
    
    Campbell Barton's avatar
    Campbell Barton committed
        Makes a list of faces and vertexes for arches.
        ra: the radius of the arch, to the center of the bricks
        rt: the thickness of the arch
        x: x center location of the circular arc, as if the arch opening were centered on x = 0
        z: z center location of the arch
        anglebeg: start angle of the arch, in radians, from vertical?
        angleend: end angle of the arch, in radians, from vertical?
        bevel: how much to bevel the inside of the arch.
        vll: how long is the vertex list already?
    
        avlist = []
        aflist = []
    
        #initialize internal variables for global settings
    #overkill?
        SetH = settings['h']
        SetHwt = settings['hwt']
        SetWid = settings['w']
        SetWidMin = settings['wm']
        SetWidVar = settings['wv']
        SetGrt = settings['g']
        SetGrtVar = settings['gv']
        SetRowHeightLink = settings['rwhl']
        SetDepth = settings['d']
        SetDepthVar = settings['dv']
    
        # Init loop variables
    
    Campbell Barton's avatar
    Campbell Barton committed
    
    
        def bevelEdgeOffset(offsets, bevel, side):
    
            Take the block offsets and modify it for the correct bevel.
    
    Campbell Barton's avatar
    Campbell Barton committed
    
    
            offsets = the offset list. See MakeABlock
            bevel = how much to offset the edge
            side = -1 for left (right side), 1 for right (left side)
    
            left = (0,2,3)
            right = (4,6,7)
            if side == 1: pointsToAffect = right
            else: pointsToAffect = left
            for num in pointsToAffect:
    
    Campbell Barton's avatar
    Campbell Barton committed
                offsets[num] = offsets[num][:]
                offsets[num][0] += -bevel * side
    
    
        ArchInner = ra-rt/2
        ArchOuter = ra+rt/2-SetGrt + rndc()*SetGrtVar
    
        DepthBack = -SetDepth/2-rndc()*SetDepthVar
        DepthFront = SetDepth/2+rndc()*SetDepthVar
    
        if radialized: subdivision = settings['sdv']
        else: subdivision = 0.12
    
        grt = (SetGrt + rndc()*SetGrtVar)/(2*ra) # init grout offset for loop
        # set up the offsets, it will be the same for every block
        offsets = ([[0]*2 + [bevel]] + [[0]*3]*3)*2
    
        #make the divisions in the "length" of the arch
        divs = fill(archStart, archEnd, settings['w']/ra, settings['wm']/ra, settings['wv']/ra)
    
        for i in range(len(divs)-1):
            if i == 0:
                ThisOffset = offsets[:]
                bevelEdgeOffset(ThisOffset, bevAngle, -1)
            elif i == len(divs)-2:
                ThisOffset = offsets[:]
                bevelEdgeOffset(ThisOffset, bevAngle, 1)
            else:
                ThisOffset = offsets
    
            geom = MakeABlock([divs[i]+grt, divs[i+1]-grt, ArchInner, ArchOuter, DepthBack, DepthFront],
                              subdivision, len(avlist) + vll, ThisOffset, [], None, ra)
    
    Campbell Barton's avatar
    Campbell Barton committed
    
    
            avlist += geom[0]
            aflist += geom[1]
    
            if SetDepthVar: # vary depth
                DepthBack = -SetDepth/2-rndc()*SetDepthVar
                DepthFront = SetDepth/2+rndc()*SetDepthVar
    
            if SetGrtVar: # vary grout
                grt = (settings['g'] + rndc()*SetGrtVar)/(2*ra)
                ArchOuter = ra+rt/2-SetGrt + rndc()*SetGrtVar
    
        for i,vert in enumerate(avlist):
            v0 = vert[2]*sin(vert[0]) + x
            v1 = vert[1]
            v2 = vert[2]*cos(vert[0]) + z
    
            if radialized==1:
                if slope==1: r1 = dims['t']*(sin(v2*PI/(dims['t']*2)))
                else: r1 = v2
                v0 = v0/r1
    
            avlist[i] = [v0,v1,v2]
    
        return (avlist,aflist)
    
    #
    def sketch():
        __doc__ = """\
    
    Campbell Barton's avatar
    Campbell Barton committed
        The 'sketch' function creates a list of openings from the general specifications passed to it.
        It takes curved and domed walls into account, placing the openings at the appropriate angular locations
        """
    
        boundlist = []
        for x in openingSpecs:
            if x['rp']:
                if radialized: r1 = x['z']
                else: r1 = 1
    
                if x['x'] > (x['w'] + settings['wm']):spacing = x['x']/r1
                else: spacing = (x['w'] + settings['wm'])/r1
    
                minspacing = (x['w'] + settings['wm'])/r1
    
                divs = fill(dims['s'],dims['e'],spacing,minspacing,center=1)
    
                for posidx in range(len(divs)-2):
                    boundlist.append(opening(divs[posidx+1],x['z'],x['w'],x['h'],x['v'],x['t'],x['vl'],x['tl'],x['b']))
    
            else: boundlist.append(opening(x['x'],x['z'],x['w'],x['h'],x['v'],x['t'],x['vl'],x['tl'],x['b']))
            #check for overlaping edges?
    
        return boundlist
    
    
    def wedgeBlocks(row, opening, leftPos, rightPos, edgeBinary, r1):
        __doc__ = """\
    
    Campbell Barton's avatar
    Campbell Barton committed
        Makes wedge blocks for the left and right sides, depending
        example:
        wedgeBlocks(row, LeftWedgeEdge, LNerEdge, LEB, r1)
        wedgeBlocks(row, RNerEdge, RightWedgeEdge, REB, r1)
        """
        wedgeEdges = fill(leftPos, rightPos, settings['w']/r1, settings['wm']/r1,
    
                          settings['wv']/r1)
    
        for i in range(len(wedgeEdges)-1):
            x = (wedgeEdges[i+1] + wedgeEdges[i])/2
            grt = (settings['g'] + rndd()*settings['gv'])/r1
            w = wedgeEdges[i+1] - wedgeEdges[i] - grt
    
            ThisBlockDepth = rndd()*settings['dv']+settings['d']
    
    #edgeV may return "None" - causing TypeError for math op.
    #use 0 until wedgeBlocks operation worked out
            edgeVal = opening.edgeV(x-w/2,edgeBinary)
            if edgeVal == None: edgeVal = 0.0
    
            LeftVertOffset =  -( row.z - (row.h/2)*edgeBinary - edgeVal )
    
    #edgeV may return "None" - causing TypeError for math op.
    #use 0 until wedgeBlocks operation worked out
            edgeVal = opening.edgeV(x+w/2,edgeBinary)
            if edgeVal == None: edgeVal = 0.0
    
            RightVertOffset = -( row.z - (row.h/2)*edgeBinary - edgeVal )
    
            #Wedges are on top = off, blank, off, blank
            #Wedges are on btm = blank, off, blank, off
            ThisBlockOffsets = [[0,0,LeftVertOffset]]*2 + [[0]*3]*2 + [[0,0,RightVertOffset]]*2
    
            # Instert or append "blank" for top or bottom wedges.
            if edgeBinary == 1: ThisBlockOffsets = ThisBlockOffsets + [[0]*3]*2
            else: ThisBlockOffsets = [[0]*3]*2 + ThisBlockOffsets
    
            row.BlocksEdge.append([x,row.z,w,row.h,ThisBlockDepth,ThisBlockOffsets])
    
        return None
    
    def bevelBlockOffsets(offsets, bevel, side):
    
    Campbell Barton's avatar
    Campbell Barton committed
        Take the block offsets and modify it for the correct bevel.
    
    
        offsets = the offset list. See MakeABlock
        bevel = how much to offset the edge
        side = -1 for left (right side), 1 for right (left side)
    
    #    left = (4,6)
    #    right = (0,2)
        if side == 1: pointsToAffect = (0,2) # right
        else: pointsToAffect = (4,6) # left
        for num in pointsToAffect:
    
    Campbell Barton's avatar
    Campbell Barton committed
            offsets[num] = offsets[num][:]
            offsets[num][0] += bevel * side
    
    
    def rowProcessing(row, Thesketch, WallBoundaries):
        __doc__ = """\
    
    Campbell Barton's avatar
    Campbell Barton committed
        Take row and opening data and process a single row, adding edge and fill blocks to the row data.
        """
    
        #set end blocks
        #check for openings, record top and bottom of row for right and left of each
        #if both top and bottom intersect create blocks on each edge, appropriate to the size of the overlap
        #if only one side intersects, run fill to get edge positions, but this should never happen
        #
    
        if radialized:#this checks for radial stonework, and sets the row radius if required
            if slope: r1 = abs(dims['t']*sin(row.z*PI/(dims['t']*2)))
            else: r1 = abs(row.z)
        else: r1 = 1
    
        # set the edge grout thickness, especially with radial stonework in mind
        edgrt = settings['ge']*(settings['g']/2 + rndc()*settings['gv'])/(2*r1)
    
        # Sets up a list of  intersections of top of row with openings,
        #from left to right [left edge of opening, right edge of opening, etc...]
        #initially just the left and right edge of the wall
        edgetop = [[dims['s']+row.EdgeOffset/r1+edgrt,WallBoundaries], [dims['e']+row.EdgeOffset/r1-edgrt,WallBoundaries]]
        # Same as edgetop, but for the bottms of the rows
        edgebtm = [[dims['s']+row.EdgeOffset/r1+edgrt,WallBoundaries], [dims['e']+row.EdgeOffset/r1-edgrt,WallBoundaries]]
    
        # set up some useful values for the top and bottom of the rows.
        rowTop = row.z+row.h/2
        rowBtm = row.z-row.h/2
    
        for hole in Thesketch:
            #check the top and bottom of the row, looking at the opening from the right
            e = [hole.edgeS(rowTop, -1), hole.edgeS(rowBtm, -1)]
    
            # If either one hit the opening, make split points for the left side of the opening.
            if e[0] or e[1]:
                e += [hole.edgeS(rowTop, 1), hole.edgeS(rowBtm, 1)]
    
                # If one of them missed for some reason, set that value to
                # the middle of the opening.
                for i,pos in enumerate(e):
                    if pos == None: e[i] = hole.x
    
                # add the intersects to the list of edge points
                edgetop.append([e[0],hole])
                edgetop.append([e[2],hole])
                edgebtm.append([e[1],hole])
                edgebtm.append([e[3],hole])
    
        # We want to make the walls in order, so sort the intersects.
        # This is where you would want to remove edge points that are out of order
        # so that you don't get the "oddity where overlapping openings create blocks inversely" problem
        edgetop.sort()
        edgebtm.sort()
    
        #these two loops trim the edges to the limits of the wall.  This way openings extending outside the wall don't enlarge the wall.
        while True:
            try:
                if (edgetop[-1][0] > dims['e']+row.EdgeOffset/r1) or (edgebtm[-1][0] > dims['e']+row.EdgeOffset/r1):
                    edgetop[-2:] = []
                    edgebtm[-2:] = []
                else: break
            except IndexError: break
        #still trimming the edges...
        while True:
            try:
                if (edgetop[0][0] < dims['s']+row.EdgeOffset/r1) or (edgebtm[0][0] < dims['s']+row.EdgeOffset/r1):
                    edgetop[:2] = []
                    edgebtm[:2] = []
                else: break
            except IndexError: break
    
        #make those edge blocks and rows!  Wooo!
        #This loop goes through each section, (a pair of points in edgetop)
        #and places the edge blocks and inbetween normal block zones into the row object
        for OpnSplitNo in range(int(len(edgetop)/2)):
            #left edge is edge<x>[2*OpnSplitNo], right edge edgex[2*OpnSplitNo+1]
            leftEdgeIndex = 2*OpnSplitNo
            rightEdgeIndex = 2*OpnSplitNo + 1
            # get the openings, to save time and confusion
            leftOpening = edgetop[leftEdgeIndex][1]
            rightOpening = edgetop[rightEdgeIndex][1]
            #find the difference between the edge top and bottom on both sides
            LTop = edgetop[leftEdgeIndex][0]
            LBtm = edgebtm[leftEdgeIndex][0]
            RTop = edgetop[rightEdgeIndex][0]
            RBtm = edgebtm[rightEdgeIndex][0]
            LDiff = LBtm-LTop
            RDiff = RTop-RBtm
    
            #which is furthur out on each side, top or bottom?
            if LDiff > 0:
                LFarEdge = LTop #The furthest edge left
                LNerEdge = LBtm #the nearer edge left
                LEB = 1 #Left Edge Boolean, set to 1 if furthest edge is top, -1 if it is bottom
    
    Campbell Barton's avatar
    Campbell Barton committed
            else:
    
                LFarEdge = LBtm
                LNerEdge = LTop
                LEB = -1
    
            if RDiff > 0:
                RFarEdge = RTop #The furthest edge right
                RNerEdge = RBtm #the nearer edge right
                REB = 1 #Right Edge Boolean, set to 1 if furthest edge is top, -1 if it is bottom
    
    
    Campbell Barton's avatar
    Campbell Barton committed
            else:
    
                RFarEdge = RBtm #The furthest edge right
                RNerEdge = RTop
                REB = -1 #Right Edge Boolean, set to 1 if furthest edge is top, -1 if it is bottom
    
            #The space between the closest edges of the openings in this section of the row
            InnerDiff = RNerEdge - LNerEdge
            #The mid point between the nearest edges