Commit fe9b4980 authored by sumpfralle's avatar sumpfralle

added a separate ContourModel to allow engravings

added some more functions to make "Line" more compatible with "Triangle"


git-svn-id: https://pycam.svn.sourceforge.net/svnroot/pycam/trunk@351 bbaffbd6-741e-11dd-a85d-61de82d9cad9
parent 8e1710cb
...@@ -20,6 +20,13 @@ You should have received a copy of the GNU General Public License ...@@ -20,6 +20,13 @@ You should have received a copy of the GNU General Public License
along with PyCAM. If not, see <http://www.gnu.org/licenses/>. along with PyCAM. If not, see <http://www.gnu.org/licenses/>.
""" """
try:
import OpenGL.GL as GL
GL_enabled = True
except:
GL_enabled = False
import math import math
...@@ -59,3 +66,32 @@ class Line: ...@@ -59,3 +66,32 @@ class Line:
def dist_to_point(self, p): def dist_to_point(self, p):
return sqrt(self.dist_to_point_sq(p)) return sqrt(self.dist_to_point_sq(p))
def minx(self):
return min(self.p1.x, self.p2.x)
def miny(self):
return min(self.p1.y, self.p2.y)
def minz(self):
return min(self.p1.z, self.p2.z)
def maxx(self):
return max(self.p1.x, self.p2.x)
def maxy(self):
return max(self.p1.y, self.p2.y)
def maxz(self):
return max(self.p1.z, self.p2.z)
def to_OpenGL(self):
if GL_enabled:
GL.glBegin(GL.GL_LINES)
GL.glVertex3f(p1.x, p1.y, p1.z)
GL.glVertex3f(p2.x, p2.y, p2.z)
GL.glEnd()
def get_points(self):
return (self.p1, self.p2)
...@@ -21,10 +21,8 @@ You should have received a copy of the GNU General Public License ...@@ -21,10 +21,8 @@ You should have received a copy of the GNU General Public License
along with PyCAM. If not, see <http://www.gnu.org/licenses/>. along with PyCAM. If not, see <http://www.gnu.org/licenses/>.
""" """
from utils import * from pycam.Geometry import Triangle, Line
from Point import * from utils import INFINITE
from Line import *
from Triangle import *
try: try:
import OpenGL.GL as GL import OpenGL.GL as GL
...@@ -47,13 +45,13 @@ MODEL_TRANSFORMATIONS = { ...@@ -47,13 +45,13 @@ MODEL_TRANSFORMATIONS = {
} }
class Model: class BaseModel(object):
id = 0 id = 0
def __init__(self): def __init__(self):
self.id = Model.id self.id = BaseModel.id
Model.id += 1 BaseModel.id += 1
self._triangles = [] self._item_groups = []
self.name = "model%d" % self.id self.name = "model%d" % self.id
self.minx = None self.minx = None
self.miny = None self.miny = None
...@@ -61,68 +59,49 @@ class Model: ...@@ -61,68 +59,49 @@ class Model:
self.maxx = None self.maxx = None
self.maxy = None self.maxy = None
self.maxz = None self.maxz = None
self._maxsize = None
def __add__(self, other_model): def __add__(self, other_model):
""" combine two models """ """ combine two models """
result = Model() result = self.__cls__()
for t in self._triangles: for item_group in self._item_groups + other_model._item_groups:
result.append(t) for item in item_group:
for t in other_model._triangles: result.append(item)
result.append(t)
return result return result
def to_OpenGL(self): def to_OpenGL(self):
if not GL_enabled: for item_group in self._item_groups:
return for item in item_group:
if True: item.to_OpenGL()
GL.glBegin(GL.GL_TRIANGLES)
for t in self._triangles:
GL.glVertex3f(t.p1.x, t.p1.y, t.p1.z)
GL.glVertex3f(t.p2.x, t.p2.y, t.p2.z)
GL.glVertex3f(t.p3.x, t.p3.y, t.p3.z)
GL.glEnd()
else:
for t in self._triangles:
t.to_OpenGL()
def _update_limits(self, t): def _update_limits(self, item):
if self.minx is None: if self.minx is None:
self.minx = t.minx() self.minx = item.minx()
self.miny = t.miny() self.miny = item.miny()
self.minz = t.minz() self.minz = item.minz()
self.maxx = t.maxx() self.maxx = item.maxx()
self.maxy = t.maxy() self.maxy = item.maxy()
self.maxz = t.maxz() self.maxz = item.maxz()
else: else:
self.minx = min(self.minx, t.minx()) self.minx = min(self.minx, item.minx())
self.miny = min(self.miny, t.miny()) self.miny = min(self.miny, item.miny())
self.minz = min(self.minz, t.minz()) self.minz = min(self.minz, item.minz())
self.maxx = max(self.maxx, t.maxx()) self.maxx = max(self.maxx, item.maxx())
self.maxy = max(self.maxy, t.maxy()) self.maxy = max(self.maxy, item.maxy())
self.maxz = max(self.maxz, t.maxz()) self.maxz = max(self.maxz, item.maxz())
def append(self, t): def append(self, item):
self._update_limits(t) self._update_limits(item)
self._triangles.append(t)
def maxsize(self): def maxsize(self):
if self._maxsize is None: return max(abs(self.maxx), abs(self.minx), abs(self.maxy),
self._maxsize = max3(max(abs(self.maxx),abs(self.minx)),max(abs(self.maxy),abs(self.miny)),max(abs(self.maxz),abs(self.minz))) abs(self.miny), abs(self.maxz), abs(self.minz))
return self._maxsize
def triangles(self, minx=-INFINITE,miny=-INFINITE,minz=-INFINITE,maxx=+INFINITE,maxy=+INFINITE,maxz=+INFINITE):
if minx==-INFINITE and miny==-INFINITE and minz==-INFINITE and maxx==+INFINITE and maxy==+INFINITE and maxz==+INFINITE:
return self._triangles
if hasattr(self, "t_kdtree"):
return self.t_kdtree.Search(minx,maxx,miny,maxy)
return self._triangles
def subdivide(self, depth): def subdivide(self, depth):
model = Model() model = self.__cls__()
for t in self._triangles: for item_group in self._item_groups:
for s in t.subdivide(depth): for item in item_group:
model.append(s) for s in item.subdivide(depth):
model.append(s)
return model return model
def reset_cache(self): def reset_cache(self):
...@@ -132,23 +111,25 @@ class Model: ...@@ -132,23 +111,25 @@ class Model:
self.maxx = None self.maxx = None
self.maxy = None self.maxy = None
self.maxz = None self.maxz = None
for t in self._triangles: for item_group in self._item_groups:
self._update_limits(t) for item in item_group:
self._maxsize = None self._update_limits(item)
def transform_by_matrix(self, matrix): def transform_by_matrix(self, matrix):
processed = [] processed = []
for tr in self._triangles: for item_group in self._item_groups:
for point in (tr.p1, tr.p2, tr.p3): for item in item_group:
if not point.id in processed: for point in item.get_points():
processed.append(point.id) if not point.id in processed:
x = point.x * matrix[0][0] + point.y * matrix[0][1] + point.z * matrix[0][2] + matrix[0][3] processed.append(point.id)
y = point.x * matrix[1][0] + point.y * matrix[1][1] + point.z * matrix[1][2] + matrix[1][3] x = point.x * matrix[0][0] + point.y * matrix[0][1] + point.z * matrix[0][2] + matrix[0][3]
z = point.x * matrix[2][0] + point.y * matrix[2][1] + point.z * matrix[2][2] + matrix[2][3] y = point.x * matrix[1][0] + point.y * matrix[1][1] + point.z * matrix[1][2] + matrix[1][3]
point.x = x z = point.x * matrix[2][0] + point.y * matrix[2][1] + point.z * matrix[2][2] + matrix[2][3]
point.y = y point.x = x
point.z = z point.y = y
tr.reset_cache() point.z = z
if hasattr(item, "reset_cache"):
item.reset_cache()
self.reset_cache() self.reset_cache()
def transform_by_template(self, direction="normal"): def transform_by_template(self, direction="normal"):
...@@ -167,3 +148,37 @@ class Model: ...@@ -167,3 +148,37 @@ class Model:
matrix = ((scale_x, 0, 0, 0), (0, scale_y, 0, 0), (0, 0, scale_z, 0)) matrix = ((scale_x, 0, 0, 0), (0, scale_y, 0, 0), (0, 0, scale_z, 0))
self.transform_by_matrix(matrix) self.transform_by_matrix(matrix)
class Model(BaseModel):
def __init__(self):
super(Model, self).__init__()
self._triangles = []
self._item_groups.append(self._triangles)
def append(self, item):
super(Model, self).append(item)
if isinstance(item, Triangle):
self._triangles.append(item)
def triangles(self, minx=-INFINITE,miny=-INFINITE,minz=-INFINITE,maxx=+INFINITE,maxy=+INFINITE,maxz=+INFINITE):
if minx==-INFINITE and miny==-INFINITE and minz==-INFINITE and maxx==+INFINITE and maxy==+INFINITE and maxz==+INFINITE:
return self._triangles
if hasattr(self, "t_kdtree"):
return self.t_kdtree.Search(minx,maxx,miny,maxy)
return self._triangles
class ContourModel(BaseModel):
def __init__(self):
super(ContourModel, self).__init__()
self.name = "contourmodel%d" % self.id
self._lines = []
self._item_groups.append(self._lines)
def append(self, item):
super(ContourModel, self).append(item)
if isinstance(item, Line):
self._lines.append(item)
...@@ -97,7 +97,7 @@ class Triangle: ...@@ -97,7 +97,7 @@ class Triangle:
self._sphere = GLU.gluNewQuadric() self._sphere = GLU.gluNewQuadric()
GLU.gluSphere(self._sphere, self._radius, 10, 10) GLU.gluSphere(self._sphere, self._radius, 10, 10)
GL.glPopMatrix() GL.glPopMatrix()
if True: # draw triangle id on triangle face if False: # draw triangle id on triangle face
GL.glPushMatrix() GL.glPushMatrix()
cc = GL.glGetFloatv(GL.GL_CURRENT_COLOR) cc = GL.glGetFloatv(GL.GL_CURRENT_COLOR)
c = self.center() c = self.center()
...@@ -275,3 +275,6 @@ class Triangle: ...@@ -275,3 +275,6 @@ class Triangle:
self._normal = None self._normal = None
self._plane = None self._plane = None
def get_points(self):
return (self.p1, self.p2, self.p3)
...@@ -28,7 +28,6 @@ __all__ = ["utils", "Line", "Model", "Path", "Plane", "Point", "Triangle", ...@@ -28,7 +28,6 @@ __all__ = ["utils", "Line", "Model", "Path", "Plane", "Point", "Triangle",
from Point import Point from Point import Point
from Line import Line from Line import Line
from Triangle import Triangle from Triangle import Triangle
from Model import Model
from Path import Path from Path import Path
from Plane import Plane from Plane import Plane
from utils import * from utils import *
......
...@@ -21,9 +21,10 @@ You should have received a copy of the GNU General Public License ...@@ -21,9 +21,10 @@ You should have received a copy of the GNU General Public License
along with PyCAM. If not, see <http://www.gnu.org/licenses/>. along with PyCAM. If not, see <http://www.gnu.org/licenses/>.
""" """
from pycam.Geometry import * from pycam.Geometry import Point, Line, Triangle
from pycam.Geometry.PointKdtree import PointKdtree from pycam.Geometry.PointKdtree import PointKdtree
from pycam.Geometry.TriangleKdtree import TriangleKdtree from pycam.Geometry.TriangleKdtree import TriangleKdtree
from pycam.Geometry.Model import Model
from struct import unpack from struct import unpack
import re import re
......
...@@ -20,7 +20,8 @@ You should have received a copy of the GNU General Public License ...@@ -20,7 +20,8 @@ You should have received a copy of the GNU General Public License
along with PyCAM. If not, see <http://www.gnu.org/licenses/>. along with PyCAM. If not, see <http://www.gnu.org/licenses/>.
""" """
from pycam.Geometry import * from pycam.Geometry import Triangle, Line, Point
from pycam.Geometry.Model import Model
def TestModel(): def TestModel():
points = [] points = []
......
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