Commit 0cd29256 authored by sumpfralle's avatar sumpfralle

moved OpenGL code to OpenGLTools

moved more matrix functions to pycam.Geometry.Matrix
moved pycam.Gui.ode_objects to pycam.Physics.ode_physics


git-svn-id: https://pycam.svn.sourceforge.net/svnroot/pycam/trunk@321 bbaffbd6-741e-11dd-a85d-61de82d9cad9
parent d2f12762
......@@ -28,7 +28,7 @@ class CylindricalCutter(BaseCutter):
def get_shape(self, format="ODE"):
if format == "ODE":
import ode
import pycam.Gui.ode_objects
import pycam.Physics.ode_physics
""" We don't handle the the "additional_distance" perfectly, since
the "right" shape would be a cylinder with a small flat cap that
grows to the full expanded radius through a partial sphere. The
......@@ -70,7 +70,7 @@ class CylindricalCutter(BaseCutter):
rot_matrix_box = (cosinus, sinus, 0.0, -sinus, cosinus, 0.0, 0.0, 0.0, 1.0)
geom_connect_transform = ode.GeomTransform(geom.space)
geom_connect_transform.setBody(geom.getBody())
geom_connect = pycam.Gui.ode_objects.get_parallelepiped_geom(
geom_connect = pycam.Physics.ode_physics.get_parallelepiped_geom(
(Point(-hypotenuse / 2.0, radius, -diff_z / 2.0), Point(hypotenuse / 2.0, radius, diff_z / 2.0),
Point(hypotenuse / 2.0, -radius, diff_z / 2.0), Point(-hypotenuse / 2.0, -radius, -diff_z / 2.0)),
(Point(-hypotenuse / 2.0, radius, self.height - diff_z / 2.0), Point(hypotenuse / 2.0, radius, self.height + diff_z / 2.0),
......
......@@ -28,7 +28,7 @@ class SphericalCutter(BaseCutter):
def get_shape(self, format="ODE"):
if format == "ODE":
import ode
import pycam.Gui.ode_objects
import pycam.Physics.ode_physics
additional_distance = self.get_required_distance()
radius = self.radius + additional_distance
center_height = 0.5 * self.height + radius - additional_distance
......@@ -61,7 +61,7 @@ class SphericalCutter(BaseCutter):
rot_matrix_box = (cosinus, sinus, 0.0, -sinus, cosinus, 0.0, 0.0, 0.0, 1.0)
geom_connect_transform = ode.GeomTransform(geom.space)
geom_connect_transform.setBody(geom.getBody())
geom_connect = pycam.Gui.ode_objects.get_parallelepiped_geom(
geom_connect = pycam.Physics.ode_physics.get_parallelepiped_geom(
(Point(-hypotenuse / 2.0, radius, -diff_z / 2.0), Point(hypotenuse / 2.0, radius, diff_z / 2.0),
Point(hypotenuse / 2.0, -radius, diff_z / 2.0), Point(-hypotenuse / 2.0, -radius, -diff_z / 2.0)),
(Point(-hypotenuse / 2.0, radius, self.height - diff_z / 2.0), Point(hypotenuse / 2.0, radius, self.height + diff_z / 2.0),
......@@ -77,7 +77,7 @@ class SphericalCutter(BaseCutter):
# rotate cylinder vector
cyl_original_vector = (0, 0, hypotenuse_3d)
cyl_destination_vector = (diff_x, diff_y, diff_z)
geom_cyl.setRotation(Matrix.get_rotation_matrix(cyl_original_vector, cyl_destination_vector))
geom_cyl.setRotation(Matrix.get_rotation_matrix_from_to(cyl_original_vector, cyl_destination_vector))
# the rotation is around the center - thus we ignore negative diff values
geom_cyl.setPosition((abs(diff_x / 2.0), abs(diff_y / 2.0), radius - additional_distance))
geom_cyl_transform.setGeom(geom_cyl)
......
......@@ -6,9 +6,27 @@ from pycam.Geometry.Point import Point
import math
def get_dot_product(a, b):
""" calculate the dot product of two 3d vectors
@type a: tuple(float) | list(float)
@value a: the first vector to be multiplied
@type b: tuple(float) | list(float)
@value b: the second vector to be multiplied
@rtype: float
@return: the dot product is (a0*b0 + a1*b1 + a2*b2)
"""
return sum(map(lambda l1, l2: l1 * l2, a, b))
def get_cross_product(a, b):
""" calculate the cross product of two 3d vectors
@type a: tuple(float) | list(float) | pycam.Geometry.Point
@value a: the first vector to be multiplied
@type b: tuple(float) | list(float) | pycam.Geometry.Point
@value b: the second vector to be multiplied
@rtype: tuple(float)
@return: the cross product is a 3d vector
"""
if isinstance(a, Point):
a = (a.x, a.y, a.z)
if isinstance(b, Point):
......@@ -18,9 +36,36 @@ def get_cross_product(a, b):
a[0] * b[1] - a[1] * b[0])
def get_length(vector):
""" calculate the lengt of a 3d vector
@type vector: tuple(float) | list(float)
@value vector: the given 3d vector
@rtype: float
@return: the length of a vector is the square root of the dot product
of the vector with itself
"""
return math.sqrt(get_dot_product(vector, vector))
def get_rotation_matrix(v_orig, v_dest):
def get_rotation_matrix_from_to(v_orig, v_dest):
""" calculate the rotation matrix used to transform one vector into another
The result is useful for modifying the rotation matrix of a 3d object.
See the "extend_shape" code in each of the cutter classes (for ODE).
The simplest example is the following with the original vector pointing
along the x axis, while the destination vectors goes along the y axis:
get_rotation_matrix((1, 0, 0), (0, 1, 0))
Basically this describes a rotation around the z axis by 90 degrees.
The resulting 3x3 matrix (tuple of 9 floats) can be multiplied with any
other vector to rotate it in the same way around the z axis.
@type v_orig: tuple(float) | list(float) | pycam.Geometry.Point
@value v_orig: the original 3d vector
@type v_dest: tuple(float) | list(float) | pycam.Geometry.Point
@value v_dest: the destination 3d vector
@rtype: tuple(float)
@return: the tuple of 9 floats represents a 3x3 matrix, that can be
multiplied with any vector to rotate it in the same way, as you would
rotate v_orig to the position of v_dest
"""
if isinstance(v_orig, Point):
v_orig = (v_orig.x, v_orig.y, v_orig.z)
if isinstance(v_dest, Point):
......@@ -50,3 +95,41 @@ def get_rotation_matrix(v_orig, v_dest):
t * rot_axis.y * rot_axis.z + s * rot_axis.x,
t * rot_axis.z * rot_axis.z + c)
def get_rotation_matrix_axis_angle(rot_axis, rot_angle):
""" calculate rotation matrix for a normalized "rot_axis" vector and an angle
see http://mathworld.wolfram.com/RotationMatrix.html
@type rot_axis: tuple(float)
@value rot_axis: the vector describes the rotation axis. Its length should
be 1.0 (normalized).
@type rot_angle: float
@value rot_angle: rotation angle (radiant)
@rtype: tuple(float)
@return: the roation
"""
sin = math.sin(rot_angle)
cos = math.cos(rot_angle)
return ((cos + rot_axis[0]*rot_axis[0]*(1-cos),
rot_axis[0]*rot_axis[1]*(1-cos) - rot_axis[2]*sin,
rot_axis[0]*rot_axis[2]*(1-cos) + rot_axis[1]*sin),
(rot_axis[1]*rot_axis[0]*(1-cos) + rot_axis[2]*sin,
cos + rot_axis[1]*rot_axis[1]*(1-cos),
rot_axis[1]*rot_axis[2]*(1-cos) - rot_axis[0]*sin),
(rot_axis[2]*rot_axis[0]*(1-cos) - rot_axis[1]*sin,
rot_axis[2]*rot_axis[1]*(1-cos) + rot_axis[0]*sin,
cos + rot_axis[2]*rot_axis[2]*(1-cos)))
def multiply_vector_matrix(v, m):
""" Multiply a 3d vector with a 3x3 matrix. The result is a 3d vector.
@type v: tuple(float) | list(float)
@value v: a 3d vector as tuple or list containing three floats
@type m: tuple(tuple(float)) | list(list(float))
@value m: a 3x3 list/tuple of floats
@rtype: tuple(float)
@return: a tuple of 3 floats as the matrix product
"""
return (v[0] * m[0][0] + v[1] * m[0][1] + v[2] * m[0][2],
v[0] * m[1][0] + v[1] * m[1][1] + v[2] * m[1][2],
v[0] * m[2][0] + v[1] * m[2][1] + v[2] * m[2][2])
This diff is collapsed.
This diff is collapsed.
# Tkinter is used for "EmergencyDialog" below - but we will try to import it carefully
#import Tkinter
# "ode" is imported later, if required
#import ode_objects
import random
import sys
import os
......
#!/usr/bin/python
from pycam.Gui.ode_objects import override_ode_availability
from pycam.Physics.ode_physics import override_ode_availability
import pycam.Gui.common as GuiCommon
from optparse import OptionParser
import sys
......
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