"""
Houdini to lucille RIB exporter.

  Written by Syoyo Fujita(syoyo@lucillerender.org)
"""

import os, sys
import re
import math

ribnodelist = {}

def vnormalize(v):

    d2 = v[0] * v[0] + v[1] * v[1] + v[2] * v[2]
    d = math.sqrt(d2)

    if (d > 1.0e-12):
        invd = 1.0 / d
        v[0] *= invd
        v[1] *= invd
        v[2] *= invd

def vcross(a, b):

    v = [0.0, 0.0, 0.0]
    
    v[0] = a[1] * b[2] - b[1] * a[2]
    v[1] = a[2] * b[0] - b[2] * a[0]
    v[2] = a[0] * b[1] - b[0] * a[1]

    return v

def ortho(n):

    w = [0.0, 0.0, 0.0]

    if (n[0] < 0.6 and n[0] > -0.6):
        w[0] = 1.0
    elif (n[1] < 0.6 and n[1] > -0.6):
        w[1] = 1.0
    elif (n[2] < 0.6 and n[2] > -0.6):
        w[2] = 1.0
    else:
        w[0] = 1.0

    u = vcross(w, n)
    vnormalize(u)

    v = vcross(n, u)
    vnormalize(v)