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
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
......@@ -59,3 +66,32 @@ class Line:
def dist_to_point(self, 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
along with PyCAM. If not, see <http://www.gnu.org/licenses/>.
"""
from utils import *
from Point import *
from Line import *
from Triangle import *
from pycam.Geometry import Triangle, Line
from utils import INFINITE
try:
import OpenGL.GL as GL
......@@ -47,13 +45,13 @@ MODEL_TRANSFORMATIONS = {
}
class Model:
class BaseModel(object):
id = 0
def __init__(self):
self.id = Model.id
Model.id += 1
self._triangles = []
self.id = BaseModel.id
BaseModel.id += 1
self._item_groups = []
self.name = "model%d" % self.id
self.minx = None
self.miny = None
......@@ -61,68 +59,49 @@ class Model:
self.maxx = None
self.maxy = None
self.maxz = None
self._maxsize = None
def __add__(self, other_model):
""" combine two models """
result = Model()
for t in self._triangles:
result.append(t)
for t in other_model._triangles:
result.append(t)
result = self.__cls__()
for item_group in self._item_groups + other_model._item_groups:
for item in item_group:
result.append(item)
return result
def to_OpenGL(self):
if not GL_enabled:
return
if True:
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()
for item_group in self._item_groups:
for item in item_group:
item.to_OpenGL()
def _update_limits(self, t):
def _update_limits(self, item):
if self.minx is None:
self.minx = t.minx()
self.miny = t.miny()
self.minz = t.minz()
self.maxx = t.maxx()
self.maxy = t.maxy()
self.maxz = t.maxz()
self.minx = item.minx()
self.miny = item.miny()
self.minz = item.minz()
self.maxx = item.maxx()
self.maxy = item.maxy()
self.maxz = item.maxz()
else:
self.minx = min(self.minx, t.minx())
self.miny = min(self.miny, t.miny())
self.minz = min(self.minz, t.minz())
self.maxx = max(self.maxx, t.maxx())
self.maxy = max(self.maxy, t.maxy())
self.maxz = max(self.maxz, t.maxz())
self.minx = min(self.minx, item.minx())
self.miny = min(self.miny, item.miny())
self.minz = min(self.minz, item.minz())
self.maxx = max(self.maxx, item.maxx())
self.maxy = max(self.maxy, item.maxy())
self.maxz = max(self.maxz, item.maxz())
def append(self, t):
self._update_limits(t)
self._triangles.append(t)
def append(self, item):
self._update_limits(item)
def maxsize(self):
if self._maxsize is None:
self._maxsize = max3(max(abs(self.maxx),abs(self.minx)),max(abs(self.maxy),abs(self.miny)),max(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
return max(abs(self.maxx), abs(self.minx), abs(self.maxy),
abs(self.miny), abs(self.maxz), abs(self.minz))
def subdivide(self, depth):
model = Model()
for t in self._triangles:
for s in t.subdivide(depth):
model.append(s)
model = self.__cls__()
for item_group in self._item_groups:
for item in item_group:
for s in item.subdivide(depth):
model.append(s)
return model
def reset_cache(self):
......@@ -132,23 +111,25 @@ class Model:
self.maxx = None
self.maxy = None
self.maxz = None
for t in self._triangles:
self._update_limits(t)
self._maxsize = None
for item_group in self._item_groups:
for item in item_group:
self._update_limits(item)
def transform_by_matrix(self, matrix):
processed = []
for tr in self._triangles:
for point in (tr.p1, tr.p2, tr.p3):
if not point.id in processed:
processed.append(point.id)
x = point.x * matrix[0][0] + point.y * matrix[0][1] + point.z * matrix[0][2] + matrix[0][3]
y = point.x * matrix[1][0] + point.y * matrix[1][1] + point.z * matrix[1][2] + matrix[1][3]
z = point.x * matrix[2][0] + point.y * matrix[2][1] + point.z * matrix[2][2] + matrix[2][3]
point.x = x
point.y = y
point.z = z
tr.reset_cache()
for item_group in self._item_groups:
for item in item_group:
for point in item.get_points():
if not point.id in processed:
processed.append(point.id)
x = point.x * matrix[0][0] + point.y * matrix[0][1] + point.z * matrix[0][2] + matrix[0][3]
y = point.x * matrix[1][0] + point.y * matrix[1][1] + point.z * matrix[1][2] + matrix[1][3]
z = point.x * matrix[2][0] + point.y * matrix[2][1] + point.z * matrix[2][2] + matrix[2][3]
point.x = x
point.y = y
point.z = z
if hasattr(item, "reset_cache"):
item.reset_cache()
self.reset_cache()
def transform_by_template(self, direction="normal"):
......@@ -167,3 +148,37 @@ class Model:
matrix = ((scale_x, 0, 0, 0), (0, scale_y, 0, 0), (0, 0, scale_z, 0))
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:
self._sphere = GLU.gluNewQuadric()
GLU.gluSphere(self._sphere, self._radius, 10, 10)
GL.glPopMatrix()
if True: # draw triangle id on triangle face
if False: # draw triangle id on triangle face
GL.glPushMatrix()
cc = GL.glGetFloatv(GL.GL_CURRENT_COLOR)
c = self.center()
......@@ -275,3 +275,6 @@ class Triangle:
self._normal = 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",
from Point import Point
from Line import Line
from Triangle import Triangle
from Model import Model
from Path import Path
from Plane import Plane
from utils import *
......
......@@ -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/>.
"""
from pycam.Geometry import *
from pycam.Geometry import Point, Line, Triangle
from pycam.Geometry.PointKdtree import PointKdtree
from pycam.Geometry.TriangleKdtree import TriangleKdtree
from pycam.Geometry.Model import Model
from struct import unpack
import re
......
......@@ -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/>.
"""
from pycam.Geometry import *
from pycam.Geometry import Triangle, Line, Point
from pycam.Geometry.Model import Model
def TestModel():
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