# # Copyright (c) 2003 by Benja Fallenstein # # This file is part of Gzz. # # Gzz is free software; you can redistribute it and/or modify it under # the terms of the GNU Lesser General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # Gzz 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. # # You should have received a copy of the GNU Lesser General # Public License along with Gzz; if not, write to the Free # Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, # MA 02111-1307 USA # import gzz, java import jarray import math from test.tools.gfx import getvs from com.hp.hpl.mesa.rdf import jena model = jena.mem.ModelMem() p = model.createProperty("http://fenfire.org/2003/02/test#", "test-property") r = model.createResource() s = model.createResource() t = model.createResource() u = model.createResource() v = model.createResource() w = model.createResource() nodes, dirs = {}, {} class NodeView(gzz.loom.NodeView): def render(self, vs, cs, node, dir): nodes[node] = cs dirs[node] = dir class Cmp(java.util.Comparator): def compare(self, r1, r2): return [t,r,s,v,u,w].index(r1) - [t,r,s,v,u,w].index(r2) view = gzz.loom.WheelView(NodeView()) def distance(p1, p2): """Return the distance between p1 and p2 (AWT points)""" return math.sqrt((p1.x-p2.x)**2 + (p1.y-p2.y)**2) def angle(p1, p2): """ If p1 is the origin, return the angle between a line from p1 to p2 and the x axis. The zero angle is at the clock three and expands clockwise. """ x, y = (p2.x-p1.x) * 1.0, (p2.y-p1.y) * 1.0 if x == 0 and y < 0: return math.pi * 1.5 elif x == 0 and y == 0: return 0.0 elif x == 0 and y > 0: return math.pi/2 elif x > 0 and y < 0: return math.pi * 1.5 + math.atan(-y/x) elif x > 0 and y == 0: return 0.0 elif x > 0 and y > 0: return math.atan(y/x) elif x < 0 and y < 0: return math.pi + math.atan(-y/-x) elif x < 0 and y == 0: return math.pi elif x < 0 and y > 0: return math.pi/2 + math.atan(y/-x) def getZ(vs, cs): """ If vs is VobScene and cs a coordinate system transformation within it, return the z coordinate of point (0,0,0) after transformation. """ tmp = jarray.zeros(3, 'f') vs.coords.transformPoints3(cs, [0.0, 0.0, 0.0], tmp) return (tmp[2]) def testWheel(): r.addProperty(p, s) r.addProperty(p, t) r.addProperty(p, u) r.addProperty(p, v) w.addProperty(p, r) vs = getvs() cursor = gzz.loom.Cursor(Cmp(), r, 1, s) assert cursor.rotation == 0 view.render(vs, 0, cursor) cs_r, cs_s, cs_t, cs_u, cs_v, cs_w = \ [vs.matcher.getCS(0, x) for x in (r,s,t,u,v,w)] pr, ps, pt, pu, pv, pw = \ [vs.coords.transformPoint(cs, 0, 0, None) for cs in [cs_r, cs_s, cs_t, cs_u, cs_v, cs_w]] prz, psz, ptz, puz, pvz, pwz = \ [getZ(vs, cs) for cs in [cs_r, cs_s, cs_t, cs_u, cs_v, cs_w]] # Check that selected nodes are drawn on same level assert pw.y == pr.y == ps.y assert pwz == prz == psz # Check that the positive side of the wheel seem to turn right assert pt.y < ps.y < pv.y assert pt.x < ps.x > pv.x assert ptz > psz < pvz < puz # Check that all nodes have approximately the same distance # from r for px in (pt, pu, pv, pw): assert abs(distance(pr,ps) - distance(pr,px)) < 1 # Check the angles assert angle(pr, ps) == 0 assert angle(pr, pv) == 2*math.pi/8 assert angle(pr, pu) == 2*math.pi/8*2 assert angle(pr, pw) == math.pi assert angle(pr, pt) == 2*math.pi - angle(pr, pv) # Test that WheelView calls its NodeView # with all the correct parameters. assert nodes[r] == cs_r assert nodes[s] == cs_s assert nodes[t] == cs_t assert nodes[u] == cs_u assert nodes[v] == cs_v assert nodes[w] == cs_w assert dirs[r] == 0 assert dirs[s] == dirs[t] == dirs[u] == dirs[v] == 1 assert dirs[w] == -1