# Multiple geometries in the JavaView viewer.
# @author   Konrad Polthier, Sergei Chekanov 
'''
Periodic curves on rotation torus. A (m,n)-torus-knot is a curve on the torus, which is specified
by winding m times around the main axis of the torus and n times around the tube of the torus. 
Three geometries displayed: The torus, the torus knot as a polygon and a tube around the
torus knot polygon. 
'''

from jv.geom import PgElementSet,PgPolygon
from jv.number import PuDouble,PuInteger
from jv.project import PvCameraIf,PvDisplayIf 
from jv.vecmath import PdVector
from jv.number import PdColor
from jhplot import HJavaView
from jvx.curve import PgTube
from java.awt import Color
from java.lang import Math

radius   = 2.   # Radius of torus
thick    = 1.   # Thickness of torus

m_torus = PgElementSet(3)
m_torus.setName("Torus")
m_torus.computeTorus(21, 15, radius, thick)
m_torus.showElements(False)

m_knot = PgPolygon(3)
m_knot.setName("TorusKnot")
m_knot.setGlobalEdgeColor(Color.blue)

m_tube = PgTube(3)
m_tube.setName("Tube")
m_tube.setPolygon(m_knot)

thick = 3 
def evalTorus(p, u, v):
   su = Math.sin(u)
   cu = Math.cos(u)
   sv = Math.sin(v)
   cv = Math.cos(v)
   p.set(cu*(thick*cv+radius), su*(thick*cv+radius), thick*sv)


# now create knote
numVertices=100 
numUWindings=5 
numZWindings=4  
m_knot.assureVertexColors()
m_knot.showVertexColors(True)
m_knot.showEdgeColors(True)
m_knot.setGlobalVertexSize(4.)
m_knot.setGlobalEdgeSize(3.)
m_knot.setNumVertices(numVertices)
deltaU   = 2.*Math.PI*numUWindings/(numVertices-1.)
deltaV   = 2.*Math.PI*numZWindings/(numVertices-1.)
u = 0.
v = 0.
for i in xrange(numVertices):
   evalTorus(m_knot.getVertex(i), u, v)
   u  += deltaU
   v  += deltaV
   dist = 255*i/(numVertices-1)
   m_knot.setVertexColor(i, PdColor.hsv2rgb(dist, 255, 255))
m_knot.makeEdgeFromVertexColors()

m_tube.setEnabledArrowPanel(False)
m_tube.setDefThickness(0.2)
m_tube.setDefDiscr(5)
m_tube.assureElementColors()
m_tube.setEnabledInduceColors(True)

c1= HJavaView()
c1.draw([m_torus,m_knot,m_tube])
view=c1.getView()
disp=view.getDisplay()

c1.visible()
