Commit 507d2a4e authored by sumpfralle's avatar sumpfralle

improved flexibility of "draw_cone" code

replaced coordinate system axes with thicker lines ending in cones


git-svn-id: https://pycam.svn.sourceforge.net/svnroot/pycam/trunk@1216 bbaffbd6-741e-11dd-a85d-61de82d9cad9
parent f4fe31df
...@@ -54,17 +54,23 @@ def keep_matrix(func): ...@@ -54,17 +54,23 @@ def keep_matrix(func):
return keep_matrix_wrapper return keep_matrix_wrapper
@keep_matrix @keep_matrix
def draw_direction_cone(p1, p2): def draw_direction_cone(p1, p2, position=0.5, precision=12, size=0.1):
# convert p1 and p2 from list/tuple to Point
if not hasattr(p1, "sub"):
p1 = Point(*p1)
if not hasattr(p2, "sub"):
p2 = Point(*p2)
distance = p2.sub(p1) distance = p2.sub(p1)
length = distance.norm length = distance.norm
direction = distance.normalized() direction = distance.normalized()
if direction is None: if direction is None:
# zero-length line # zero-length line
return return
cone_radius = length / 30 cone_length = length * size
cone_length = length / 10 cone_radius = cone_length / 3.0
# move the cone to the middle of the line # move the cone to the middle of the line
GL.glTranslatef((p1.x + p2.x) / 2, (p1.y + p2.y) / 2, (p1.z + p2.z) / 2) GL.glTranslatef((p1.x + p2.x) * position,
(p1.y + p2.y) * position, (p1.z + p2.z) * position)
# rotate the cone according to the line direction # rotate the cone according to the line direction
# The cross product is a good rotation axis. # The cross product is a good rotation axis.
cross = direction.cross(Point(0, 0, -1)) cross = direction.cross(Point(0, 0, -1))
...@@ -87,9 +93,9 @@ def draw_direction_cone(p1, p2): ...@@ -87,9 +93,9 @@ def draw_direction_cone(p1, p2):
# The line goes up the z axis - nothing to be done. # The line goes up the z axis - nothing to be done.
pass pass
# center the cone # center the cone
GL.glTranslatef(0, 0, -cone_length / 2) GL.glTranslatef(0, 0, -cone_length * position)
# draw the cone # draw the cone
GLUT.glutSolidCone(cone_radius, cone_length, 12, 1) GLUT.glutSolidCone(cone_radius, cone_length, precision, 1)
@keep_gl_mode @keep_gl_mode
@keep_matrix @keep_matrix
......
...@@ -21,7 +21,7 @@ along with PyCAM. If not, see <http://www.gnu.org/licenses/>. ...@@ -21,7 +21,7 @@ along with PyCAM. If not, see <http://www.gnu.org/licenses/>.
""" """
import pycam.Plugins import pycam.Plugins
from pycam.Geometry.utils import number from pycam.Gui.OpenGLTools import draw_direction_cone
class OpenGLViewAxes(pycam.Plugins.PluginBase): class OpenGLViewAxes(pycam.Plugins.PluginBase):
...@@ -31,9 +31,7 @@ class OpenGLViewAxes(pycam.Plugins.PluginBase): ...@@ -31,9 +31,7 @@ class OpenGLViewAxes(pycam.Plugins.PluginBase):
def setup(self): def setup(self):
import OpenGL.GL import OpenGL.GL
import OpenGL.GLUT
self._GL = OpenGL.GL self._GL = OpenGL.GL
self._GLUT = OpenGL.GLUT
self.core.register_event("visualize-items", self.draw_axes) self.core.register_event("visualize-items", self.draw_axes)
self.core.get("register_display_item")("show_axes", self.core.get("register_display_item")("show_axes",
"Show Coordinate System", 50) "Show Coordinate System", 50)
...@@ -55,53 +53,32 @@ class OpenGLViewAxes(pycam.Plugins.PluginBase): ...@@ -55,53 +53,32 @@ class OpenGLViewAxes(pycam.Plugins.PluginBase):
self.core.call_chain("get_draw_dimension", low, high) self.core.call_chain("get_draw_dimension", low, high)
if None in low or None in high: if None in low or None in high:
low, high = (0, 0, 0), (10, 10, 10) low, high = (0, 0, 0), (10, 10, 10)
size_x = abs(high[0]) length = 1.2 * max(max(high), abs(min(low)))
size_y = abs(high[1]) origin = (0, 0, 0)
size_z = abs(high[2]) cone_length = 0.05
size = number(1.7) * max(size_x, size_y, size_z) old_line_width = GL.glGetFloatv(GL.GL_LINE_WIDTH)
# the divider is just based on playing with numbers
scale = size / number(1500.0)
string_distance = number(1.1) * size
# otherwise plain colors like the next glColor4f wouldn't work
if self.core.get("view_light"): if self.core.get("view_light"):
GL.glDisable(GL.GL_LIGHTING) GL.glDisable(GL.GL_LIGHTING)
GL.glBegin(GL.GL_LINES) GL.glLineWidth(1.5)
GL.glColor4f(1, 0, 0, 1) # draw a colored line ending in a cone for each axis
GL.glVertex3f(0, 0, 0) for index in range(3):
GL.glVertex3f(size, 0, 0) end = [0, 0, 0]
GL.glEnd() end[index] = length
self.draw_string(string_distance, 0, 0, 'xy', "X", scale=scale) color = [0.0, 0.0, 0.0]
GL.glBegin(GL.GL_LINES) # reduced brightness (not 1.0)
GL.glColor3f(0, 1, 0) color[index] = 0.8
GL.glVertex3f(0, 0, 0) GL.glColor3f(*color)
GL.glVertex3f(0, size, 0) # we need to wait until the color change is active
GL.glEnd() GL.glFinish()
self.draw_string(0, string_distance, 0, 'yz', "Y", scale=scale) GL.glBegin(GL.GL_LINES)
GL.glBegin(GL.GL_LINES) GL.glVertex3f(*origin)
GL.glColor3f(0, 0, 1) GL.glVertex3f(*end)
GL.glVertex3f(0, 0, 0) GL.glEnd()
GL.glVertex3f(0, 0, size) # Position the cone slightly behind the end of the line - otherwise
GL.glEnd() # the end of the line (width=2) is visible at the top of the cone.
self.draw_string(0, 0, string_distance, 'xz', "Z", scale=scale) draw_direction_cone(origin, end, position=1.0 + cone_length,
precision=32, size=cone_length)
GL.glLineWidth(old_line_width)
if self.core.get("view_light"): if self.core.get("view_light"):
GL.glEnable(GL.GL_LIGHTING) GL.glEnable(GL.GL_LIGHTING)
def draw_string(self, x, y, z, p, s, scale=.01):
GL = self._GL
GLUT = self._GLUT
GL.glPushMatrix()
GL.glTranslatef(x, y, z)
if p == 'xy':
GL.glRotatef(90, 1, 0, 0)
elif p == 'yz':
GL.glRotatef(90, 0, 1, 0)
GL.glRotatef(90, 0, 0, 1)
elif p == 'xz':
GL.glRotatef(90, 0, 1, 0)
GL.glRotatef(90, 0, 0, 1)
GL.glRotatef(45, 0, 1, 0)
GL.glScalef(scale, scale, scale)
for c in str(s):
GLUT.glutStrokeCharacter(GLUT.GLUT_STROKE_ROMAN, ord(c))
GL.glPopMatrix()
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment