#!/usr/bin/env python
#Sam Calisch 2012
#uses otherlab/core: https://github.com/otherlab/core/
from __future__ import division
import sys
from other.core.value import parser
from other.core.openmesh import *
from numpy import *

#NOTE: To run this script with the provided loft.{h,cpp} routines, you must create a module.cpp file that will create the python module exposing the c++ code.

PropManager.add('point_filename','points').set_help('file containing points defining loft')
stride = PropManager.add('stride',10).set_help('how many points per section')
mesh_filename = PropManager.add('mesh_filename','mesh.stl').set_help('filename to export mesh')

def main():

  def read_points():
    point_filename = PropManager.get('point_filename')()
    f = open(point_filename,'r')
    i = 0
    pts = array([])
    row = array([])
    for line in f:
      d = asarray(map(float,line.split(',')))
      if(i < stride()):
        if len(row)==0:
          row = array([d])
        else:
          row = vstack((row,d))
        i = i+1
      else:
        if len(pts)==0:
          pts = array([row])
        else:
          pts = vstack((pts,[row]))
        i=0
        row=array([])
    return pts

  parser.parse("This is a lofting program.")
  pts = read_points()
  print pts.shape
  tm = TriMesh()
  tm.add_vertices(pts[0])
  for i in range(len(pts)-1):
    tm.add_vertices(pts[i+1])
    tris = compute_loft(pts[i],pts[i+1])

#    for use with compute_lofts_circ, slightly different arguments
#    cyl = vstack((pts[i],pts[i+1])).reshape(-1,3)
#    tris = compute_lofts_circ(cyl,stride(),True)

    tm.add_faces(tris.elements + stride()*i)

  tm.write(mesh_filename())


if __name__=='__main__':
  main()
