前情回顾
在上一节的CSDN中:
实现了直线和圆弧的绘制。
但是不能移动和缩放。
还需要一个方法,对这些数据进行组织。
正文
组织图元
铺垫:
怎么组织图元呢?
也许可以参考画家算法的处理顺序。
也许还能借用python的二维列表
根据相交关系,来组织零散的图元。
怎么判断直线与直线相交?
——未知
怎么判断圆弧与圆弧相交?
——未知
怎么判断直线与圆弧相交?
——点到直线距离公式
计算两点之间的距离,可能会用到numpy:
https://www.runoob.com/numpy/numpy-tutorial.htmlhttps://www.runoob.com/numpy/numpy-tutorial.html 然后,怎么重组?重组以后存哪?用什么数据结构存?
暂定是这样:
二维list里,得能判断每个元素具体是什么图元类。应该可以借助type实现:
#读取
dxf = dxfgrabber.readfile(inputFilePath)
lineNum=0
arcNum=0
for e in dxf.entities:
if e.dxftype == 'LINE':
LINELIST.append(Line(lineNum,e.start,e.end))
lineNum+=1
elif e.dxftype=='ARC':
ARCLIST.append(Arc(arcNum,e.center,e.radius,e.start_angle,e.end_angle))
arcNum+=1
#图元重组
#获取种类
if Line==type(LINELIST[0]):
print("is line")
if Arc==type(ARCLIST[0]):
print("is arc")
二维list,也许应该叫嵌套list:
https://www.runoob.com/python3/python3-list.htmlhttps://www.runoob.com/python3/python3-list.html
测试:
a=[1,2,4]
b=[4,5,6]
x=[]
x.append(a)
x.append(b)
print(x[0])
print(x[1])
c=['x','y','x']
x.append(c)
print(x[2])
print(x[2][0])
输出结果
python下如何使用switch-case——python3.10以上才能用。3.7不行。
#python-switch-case
i=10
match i:
case 10:
pass
case 11:
pass
怎么处理OpenGL的鼠标事件?比如,点击,按下并移动?
https://cuiqingcai.com/1776.htmlhttps://cuiqingcai.com/1776.html
from asyncio.windows_events import NULL
from os import stat
import dxfgrabber
import math
import numpy as np
from OpenGL.GL import *
from OpenGL.GLU import *
from OpenGL.GLUT import *
inputFilePath='e:/test3-11/Drawing1.dxf' #输入文件的路径
#供读取的数据结构
class Line:
def __init__(self,id,start,end):
self.id=id
self.start=start
self.end=end
LINELIST=[]
class Arc:
def __init__(self,id,center,radius,startAngle,endAngle) -> None:
self.id=id
self.center=center
self.radius=radius
self.start_angle=startAngle
self.end_angle=endAngle
pass
ARCLIST=[]
VIRTUALLIST=[]
SCALE=1.0#用于缩放
DX,DY=0,0#用于移动
#读取
dxf = dxfgrabber.readfile(inputFilePath)
lineNum=0
arcNum=0
for e in dxf.entities:
if e.dxftype == 'LINE':
LINELIST.append(Line(lineNum,e.start,e.end))
lineNum+=1
elif e.dxftype=='ARC':
ARCLIST.append(Arc(arcNum,e.center,e.radius,e.start_angle,e.end_angle))
arcNum+=1
#图元重组
def intersectionTest()->list:#加入新图元后,与之前加入的图元进行相交测试,返回与之相交的图元列表
global VIRTUALLIST
result=[]
if len(VIRTUALLIST)==1:
return result
entity=VIRTUALLIST[-1]#列表的最后一个就是新加入的图元
if type(entity)==Line:#直线与其它图元的相交测试
for virtualEntity in VIRTUALLIST[0:len(VIRTUALLIST)]:
if type(virtualEntity)==Line:
pass
elif type(virtualEntity)==Arc:
#计算圆弧起点与终点,与直线重合,则认为相交
x=virtualEntity.center[0]+virtualEntity.radius*math.cos(virtualEntity.start_angle/180*math.pi)
y=virtualEntity.center[1]+virtualEntity.radius*math.sin(virtualEntity.end_angle/180*math.pi)
else:
pass
elif type(entity)==Arc:
for virtualEntity in VIRTUALLIST[0:len(VIRTUALLIST)]:
if type(virtualEntity)==Line:#直线去找圆弧们,测试相交
x=entity.center[0]+entity.radius*math.cos(entity.start_angle/180*math.pi)
y=entity.center[1]+entity.radius*math.sin(entity.end_angle/180*math.pi)
p0=np.array([x,y])
p1=np.array(virtualEntity.start[0:2])
p2=np.array(virtualEntity.end[0:2])
pass
elif type(virtualEntity)==Arc:
pass
else:
pass
else:
pass
for line in LINELIST:
VIRTUALLIST.append(line)
intersectionTest()
for arc in ARCLIST:
VIRTUALLIST.append(arc)
intersectionTest()
#显示——OpenGL
def init():
glClearColor(0.3, 0.3, 0.3, 1.0)
gluOrtho2D(0, 400.0, 0, 400)#窗口坐标系的大小,左下角是原点
def drawFunc():
glClear(GL_COLOR_BUFFER_BIT)
glPushMatrix()
glScale(SCALE,SCALE,1.0)#缩放——OpenGL里没有鼠标滚轮.pyQT里有吗?
glTranslatef(DX,DY,0)
#绘制所有直线
glBegin(GL_LINES)
for line in LINELIST:
glVertex2f(line.start[0],line.start[1])
glVertex2f(line.end[0],line.end[1])
glEnd()
#用描点的形式绘制所有弧线
glBegin(GL_POINTS)
for arc in ARCLIST:
start_angle=arc.start_angle/180.0*math.pi
end_angle=arc.end_angle/180*math.pi
#CAD里,圆弧都是从起点到终点,顺时针转的;所以,这里只取正值
dAngle=abs(end_angle-start_angle)
pointNum=500#一个弧500个点
for i in range(0,pointNum):
x=arc.center[0]+arc.radius*math.cos(start_angle+dAngle*i/pointNum)
y=arc.center[1]+arc.radius*math.sin(start_angle+dAngle*i/pointNum)
glVertex2f(x,y)
glEnd()
glPopMatrix()
glFlush()
#方向键选择图元
def specialKey(key=0,x=0,y=0):
global SCALE
if (key==GLUT_KEY_UP):
SCALE+=0.1
elif(key==GLUT_KEY_DOWN):
SCALE-=0.1
glutPostRedisplay()
mouseX,mouseY=0,0
def mouseClick(button=0,state=0,x=0,y=0):
glutPostRedisplay()
def mouseDownAndMove(x=0,y=0):
global DX,DY
global mouseX,mouseY
DX+=x-mouseX
DY+=-(y-mouseY)#因为原点在左上角,Y轴与图形的Y轴颠倒,故取反
mouseX=x
mouseY=y
glutPostRedisplay()
def mouseMove(x=0,y=0):
global mouseX,mouseY
mouseX=x
mouseY=y
glutInit()
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGBA)
glutInitWindowSize(400, 400)
glutCreateWindow("test")
glutDisplayFunc(drawFunc)#回调函数
glutMouseFunc(mouseClick)#鼠标点击时调用
glutMotionFunc(mouseDownAndMove)#鼠标按下并移动时调用——原点在左上角
glutPassiveMotionFunc(mouseMove)
glutSpecialFunc(specialKey)#键盘交互
init()
glutMainLoop()
python切片:
https://www.jianshu.com/p/15715d6f4dadhttps://www.jianshu.com/p/15715d6f4dad
a=[1,2,3,4,5]
for i in a[0:-1]:
print(i)
输出:看来不包括最后的-1索引的那个
python 优先级
https://www.jb51.net/article/198848.htmhttps://www.jb51.net/article/198848.htm
用于重组图元的一种可能的数据结构:
https://www.runoob.com/python3/python3-set.htmlhttps://www.runoob.com/python3/python3-set.html测试:
#什么数据结构比较好呢?列表里套很多集合行吗?
p=[]
p0=set()
p1=set()
p2=set()
for i in range(5):
p0.add(i)
for i in range(5):
p1.add(i)
for i in range(5):
p2.add(i)
p2.add(4)#集合,自动去重
p.append(p0)
p.append(p1)
p.append(p2)
print(p)
if 3 in p[0]:
print("is")
测试结果
求两个集合的并集
https://www.runoob.com/python3/ref-set-union.htmlhttps://www.runoob.com/python3/ref-set-union.html判断两个集合是否包含相同的元素,如果没有返回 True,否则返回 False。
https://www.runoob.com/python3/ref-set-isdisjoint.htmlhttps://www.runoob.com/python3/ref-set-isdisjoint.html这两个方法也许能用的上……
它返回false……
哦……is——dis——joint,了解了
正式开始
#正式开始重组图元
from asyncio.windows_events import NULL
from os import stat
import dxfgrabber
import math
import numpy as np
from OpenGL.GL import *
from OpenGL.GLU import *
from OpenGL.GLUT import *
inputFilePath='e:/test3-11/Drawing4.dxf' #输入文件的路径
#供读取的数据结构
class Line:
def __init__(self,id,start,end):
self.id=id
self.start=start
self.end=end
LINELIST=[]
class Arc:
def __init__(self,id,center,radius,startAngle,endAngle) -> None:
self.id=id
self.center=center
self.radius=radius
self.start_angle=startAngle
self.end_angle=endAngle
pass
ARCLIST=[]
VIRTUALLIST=[]
SCALE=1.0#用于缩放
DX,DY=0,0#用于移动
#读取
dxf = dxfgrabber.readfile(inputFilePath)
lineNum=0
arcNum=0
for e in dxf.entities:
if e.dxftype == 'LINE':
LINELIST.append(Line(lineNum,e.start,e.end))
lineNum+=1
elif e.dxftype=='ARC':
ARCLIST.append(Arc(arcNum,e.center,e.radius,e.start_angle,e.end_angle))
arcNum+=1
#图元重组
def intersectionTestLineAndArc(line,arc)->bool:
#python怎么解方程——先从简处理吧
x=arc.center[0]+arc.radius*math.cos(arc.start_angle/180*math.pi)
y=arc.center[1]+arc.radius*math.sin(arc.start_angle/180*math.pi)
p0=np.array([x,y])
x=arc.center[0]+arc.radius*math.cos(arc.end_angle/180*math.pi)
y=arc.center[1]+arc.radius*math.sin(arc.end_angle/180*math.pi)
p1=np.array([x,y])
p2=np.array(line.start[0:2])
p3=np.array(line.end[0:2])
d=list(range(4))
d[0]=np.sum(np.square(p0-p2))
d[1]=np.sum(np.square(p0-p3))
d[2]=np.sum(np.square(p1-p2))
d[3]=np.sum(np.square(p1-p3))
if min(d)<=1:
return True
return False
def intersectionTest()->set:#加入新图元后,与之前加入的图元进行相交测试,返回与之相交的图元集合
global VIRTUALLIST
result=set()#空集合
if len(VIRTUALLIST)==1:
return result
entity=VIRTUALLIST[-1]#列表的最后一个就是新加入的图元
for virtualEntity in VIRTUALLIST[0:-1]:#目前,只能处理直线和弧的关系,排列组合……2*2
if type(entity)==Line and type(virtualEntity)==Line:
pass
elif type(entity)==Line and type(virtualEntity)==Arc:
pass
elif type(entity)==Arc and type(virtualEntity)==Arc:
pass
elif type(entity)==Arc and type(virtualEntity)==Line:#本例中,只会用到这个分支
if intersectionTestLineAndArc(virtualEntity,entity):
result.add(virtualEntity)
result.add(entity)
pass
else:
pass
pass
else:
pass
return result
INTERSECTIONLIST=[]
for line in LINELIST:
VIRTUALLIST.append(line)
result=intersectionTest()
for arc in ARCLIST:
VIRTUALLIST.append(arc)
result=intersectionTest()
if len(result)!=0:
newCluster=True#是否是新的一簇图形
if len(INTERSECTIONLIST)==0:
INTERSECTIONLIST.append(result)
newCluster=False
else:
for iL in INTERSECTIONLIST:
if not iL.isdisjoint(result):#双重否定,等于肯定
temp=iL.union(result)
#获取下标
index=INTERSECTIONLIST.index(iL)
#修改list中的指定set:
INTERSECTIONLIST[index]=temp
newCluster=False
if newCluster:
INTERSECTIONLIST.append(result)
else:
continue
#按照相交关系,重组完成
print(INTERSECTIONLIST)
#显示——OpenGL
def init():
glClearColor(0.3, 0.3, 0.3, 1.0)
gluOrtho2D(0, 400.0, 0, 400)#窗口坐标系的大小,左下角是原点
def drawFunc():
glClear(GL_COLOR_BUFFER_BIT)
glPushMatrix()
glScale(SCALE,SCALE,1.0)#缩放——OpenGL里没有鼠标滚轮.pyQT里有吗?
glTranslatef(DX,DY,0)
#绘制所有直线
glBegin(GL_LINES)
for line in LINELIST:
glVertex2f(line.start[0],line.start[1])
glVertex2f(line.end[0],line.end[1])
glEnd()
#用描点的形式绘制所有弧线
glBegin(GL_POINTS)
for arc in ARCLIST:
start_angle=arc.start_angle/180.0*math.pi
end_angle=arc.end_angle/180*math.pi
#CAD里,圆弧都是从起点到终点,顺时针转的;所以,这里只取正值
dAngle=abs(end_angle-start_angle)
pointNum=500#一个弧500个点
for i in range(0,pointNum):
x=arc.center[0]+arc.radius*math.cos(start_angle+dAngle*i/pointNum)
y=arc.center[1]+arc.radius*math.sin(start_angle+dAngle*i/pointNum)
glVertex2f(x,y)
glEnd()
glPopMatrix()
glFlush()
#方向键选择图元
def specialKey(key=0,x=0,y=0):
global SCALE
if (key==GLUT_KEY_UP):
SCALE+=0.1
elif(key==GLUT_KEY_DOWN):
SCALE-=0.1
glutPostRedisplay()
mouseX,mouseY=0,0
def mouseClick(button=0,state=0,x=0,y=0):
glutPostRedisplay()
def mouseDownAndMove(x=0,y=0):
global DX,DY
global mouseX,mouseY
DX+=x-mouseX
DY+=-(y-mouseY)#因为原点在左上角,Y轴与图形的Y轴颠倒,故取反
mouseX=x
mouseY=y
glutPostRedisplay()
def mouseMove(x=0,y=0):
global mouseX,mouseY
mouseX=x
mouseY=y
glutInit()
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGBA)
glutInitWindowSize(400, 400)
glutCreateWindow("test")
glutDisplayFunc(drawFunc)#回调函数
glutMouseFunc(mouseClick)#鼠标点击时调用
glutMotionFunc(mouseDownAndMove)#鼠标按下并移动时调用——原点在左上角
glutPassiveMotionFunc(mouseMove)
glutSpecialFunc(specialKey)#键盘交互
init()
glutMainLoop()
结果如下——希望不是只对这一个DXF文件有效……
数据结构,debug,调试,有系统的编程,编着改着改着编着……脑力劳动。
困难,顺利,永远都有。
矛盾的双方,相比较而存在,相斗争而发展。
没有矛盾就没有世界。
下回预告:
怎么用鼠标,交互选择图元呢?
鼠标框选?需要所有图元和框出来矩形求交才行。又涉及到一些数学上的计算。
也许以后还会用到pyqt?
目前,硬件条件良好,暂时不卡,希望能用到六月底吧。