#!BPY
"""
Name: 'Hyperbolic Paraboloid (Circular)'
Blender: 245
Group: 'AddMesh'
"""
import BPyAddMesh
import Blender
from math import cos, sin, pi

def add_hyperbolic_paraboloid (radius, thetares, radiusres, alpha, beta):
    '''Generate a mesh of a hyperbolic paraboloid'''
    v = []
    f = []

    # Fill the vertices list
    for i in range (1, radiusres + 1):
        r = radius / radiusres * i
        for t in range (0, thetares):
            theta = 2 * pi / thetares * t
            x = r * cos (theta)
            y = r * sin (theta)
            z = alpha * (x ** 2) - beta * (y ** 2)
            v.append ([x, y, z])

    v.append ([0, 0, 0])

    # Fill the faces list
    for r in range (0, radiusres - 1):
        i0 = r * thetares
        for t in range (0, thetares):
            i = i0 + t
            if t == thetares - 1:
                i1 = i
                i2 = i0
                i3 = i0 + thetares
                i4 = i + thetares
            else:
                i1 = i
                i2 = i1 + 1
                i3 = i + thetares + 1
                i4 = i3 - 1
            f.append ([i1, i2, i3, i4])

    v0 = len (v) - 1
    # Append the center faces
    for t in range (0, thetares - 1):
        f.append ([t, t + 1, v0])
    f.append ([thetares - 1, 0, v0])

    return v, f

def main ():
    radius = Blender.Draw.Create (1.0)
    thetares = Blender.Draw.Create (32)
    radiusres = Blender.Draw.Create (32)
    alpha = Blender.Draw.Create (1.0)
    beta = Blender.Draw.Create (1.0)

    params = [
                ('radius:', radius, 0.0, 10.0,'x size'),
                ('thetares:', thetares, 2, 500,'theta resolution'),
                ('radiusres:', radiusres, 2, 500,'radius resolution'),
                ('alpha:', alpha, 0.0, 10.0, 'alpha parameter'),
                ('beta:', beta, 0.0, 10.0, 'beta parameter')
             ]


    if not Blender.Draw.PupBlock ('Add Hyperbolic Paraboloid', params):
        return
    
    v, f = add_hyperbolic_paraboloid (radius.val,
                                      thetares.val, radiusres.val,
                                      alpha.val, beta.val)

    BPyAddMesh.add_mesh_simple ('Circular Hyperbolic Paraboloid', v, [], f)

main ()

