"""
     pygl/test_tree.py: CCP4MG Molecular Graphics Program
     Copyright (C) 2001-2005 University of York, CCLRC

     This library is free software: you can redistribute it and/or
     modify it under the terms of the GNU Lesser General Public License
     version 3, modified in accordance with the provisions of the 
     license to address the requirements of UK law.
 
     You should have received a copy of the modified GNU Lesser General 
     Public License along with this library.  If not, copies may be 
     downloaded from http://www.ccp4.ac.uk/ccp4license.php
 
     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 Lesser General Public License for more details.
"""


import tree
from MMDB import *
from MMUT import *
from MGAPP import *
from atom_util import *
import COLOUR

from resources import *
r = CResources()

MMDBManager1 = CMMANManager(r.CMGSBase,r.CMolBondParams)

InitMatType()
RC = MMDBManager1.ReadCoorFile("3HHR.pdb")
AselHnd = MMDBManager1.NewSelection()
MMDBManager1.SelectAtoms_ID(AselHnd, 0,"*",ANY_RES,"*",ANY_RES,"*","*","*","*","*",SKEY_OR )
ASelAtoms = newPPCAtom()
AnSelAtoms = MMDBManager1.GetSelIndex (AselHnd,ASelAtoms)
Ancontacts = ptrcreate("int")
Acontact = newPSContact()
ATMatrix = newpmat44()
MMDBManager1.SeekContacts_v_v(ASelAtoms,AnSelAtoms,ASelAtoms,AnSelAtoms,d2r(0.0),d2r(2.0),0,Acontact,Ancontacts,0,ATMatrix,0);

import time
t0 = time.time()
mytree = tree.Tree()
mytree.SetCoords(ASelAtoms,AnSelAtoms,Acontact,ptrvalue(Ancontacts),0)
t1 = time.time()

#mytree.Print()

#import sys
#sys.exit()

all_cart = mytree.GetAllCartesians()

def get_atoms(mytree,vertex):
    cp = ptradd(all_cart,3*vertex.GetID())
    #cp = mytree.GetCartesian(vertex)
    cart_point =  ptrvalue(cp), ptrvalue(cp,1), ptrvalue(cp,2)

    """
    atom = CAtomPtr(vertex.GetCAtom())
    x1 = atom.x
    y1 = atom.y
    z1 = atom.z
    deviation = (cart_point[0]-x1)*(cart_point[0]-x1) + \
                (cart_point[1]-y1)*(cart_point[1]-y1) + \
                (cart_point[2]-z1)*(cart_point[2]-z1)
    if deviation > 0.0000001:
       print "Error, deviation", deviation, cart_point
    res = CResiduePtr(atom.GetResidue())
    if IsAminoacid(ptrvalue(res.name,0,"char")):
        if ptrvalue(atom.name,0,"char") == " N  ":
          nhydrogen = ptrcreate("int");
          hydrogens = GetHydrogens(res,nhydrogen)
          for i in range(ptrvalue(nhydrogen)):
            h = CAtomPtr(getPCAtom(hydrogens,i))
            hpoint = h.x, h.y, h.z
            print hpoint
    """
    for i in range(vertex.GetNumberOfChildren()):
        get_atoms(mytree,vertex.GetChild(i))

t2 = time.time()
for i in range(mytree.GetNumberOfVertices()):
    vertex = mytree.GetCoord(i)
    if vertex.GetParentID() == -1:
        get_atoms(mytree,vertex)

t3 = time.time()

j = 0
for i in range(mytree.GetNumberOfVertices()):
  cart_point = ptrvalue(all_cart,j), ptrvalue(all_cart,j+1), ptrvalue(all_cart,j+2) 
  """
  vertex = mytree.GetCoord(i)
  atom = CAtomPtr(vertex.GetCAtom())
  x1 = atom.x
  y1 = atom.y
  z1 = atom.z
  deviation = (cart_point[0]-x1)*(cart_point[0]-x1) + \
                (cart_point[1]-y1)*(cart_point[1]-y1) + \
                (cart_point[2]-z1)*(cart_point[2]-z1)
  if deviation > 0.0000001:
       print "Error, deviation", deviation, cart_point
  """
  j = j + 3

t4 = time.time()

import cprim
cobj = cprim.Displayobject()
atomColourVector = ptrcreate("int",0,mytree.GetNumberOfVertices())
cprim.build_tree_primitives(mytree,atomColourVector,cobj)
t5 = time.time()
cobj.draw()

t6 = time.time()

print "Time to set tree", t1 - t0
print "Time to GetAllCart", t2 - t1
print "Time to get coords", t3 - t2
print "Time to get all coords", t4 - t3
print "Time to build C++ primitives", t5 - t4
print "Time to draw C++ primitives", t6 - t5
