-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcheckConcave_and_triangulate.py
116 lines (103 loc) · 3.99 KB
/
checkConcave_and_triangulate.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#***************************************************************************
#* *
#* Copyright (c) 2009, 2010 *
#* Xiaolong Cheng <[email protected]> *
#* *
#* This program is free software; you can redistribute it and/or modify *
#* it under the terms of the GNU Lesser General Public License (LGPL) *
#* as published by the Free Software Foundation; either version 2 of *
#* the License, or (at your option) any later version. *
#* for detail see the LICENCE text file. *
#* *
#* This program is distributed in the hope that it will be useful, *
#* but WITHOUT ANY WARRANTY; without even the implied warranty of *
#* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
#* GNU Library General Public License for more details. *
#* *
#* You should have received a copy of the GNU Library General Public *
#* License along with this program; if not, write to the Free Software *
#* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 *
#* USA *
#* *
#***************************************************************************
import PyTriangulation as Py
from PyTriangulation import Vector2d as point
from PyTriangulation import pyvector_point2 as pl
from FreeCAD import Vector
#plist = pl()
#plist.append( point(0,0))
#plist.append( point(4,0))
#plist.append( point(1,1))
#plist.append( point(0,4))
#result = pl()
#Py.triangulate(plist , result)
#
#print result.size()
#t = result.size()/3
#for i in range(t):
# p1 = result[i*3]
# p2 = result[i*3+1]
# p3 = result[i*3+2]
# print 'Triangle %d : (%f , %f ) (%f , %f ) , (%f , %f )'%(i,p1.x , p1.y , p2.x , p2.y , p3.x , p3.y)
def getUp(p1 , p2 , p3):
ax = p1[0]-p2[0]
ay = p1[1]-p2[1]
bx = p2[0]-p3[0]
by = p2[1]-p3[1]
tmpRe = ax*by - bx*ay
if tmpRe>0:
tmpRe=1
elif tmpRe<=0:
tmpRe = -1
else:
print 'error'
# print tmpRe
return tmpRe
def IfConcave(plist2):
if len(plist2)<3:
# raise Exception('vertices number < 3')
print '***************vertices number < 3*******************'
return
if plist2[0]==plist2[-1]:
plist = plist2[:-1]
else:
plist = plist2
res = getUp(plist[0],plist[1],plist[2])
length = len(plist)
for i in range(1,length-1):
p1 = plist[i]
p2 = plist[(i+1)%length]
p3 = plist[(i+2)%length]
tmp = getUp(p1,p2,p3)
# print res , tmp
if res != tmp:
return True
return False
def triangulate(plist2):
pnts = pl()
if plist2[0]==plist2[-1]:
plist = plist2[:-1]
else:
plist = plist2
for t in plist:
pnts.append(point(t[0],t[1]))
result = pl()
Py.triangulate(pnts , result)
tmpRe = []
for i , t in enumerate(result):
tmpRe.append(Vector(t.x,t.y,-1))
return tmpRe
def ifSegmentAndPolygonIntersect(segment2 , polygon2):
seg = pl()
poly = pl()
polygon = polygon2
if polygon2[0]==polygon2[-1]:
polygon = polygon2[:-1]
for t in segment2:
seg.append(point(t[0],t[1]))
for t in polygon:
poly.append(point(t[0],t[1]))
return Py.ifIntersection(seg , poly)
#pts = [Vector (22.7365, 8.45805, -7), Vector (25.2035, 10.0582, -7), Vector (24.8621, 7.91085, -7), Vector (24.018, 7.7756, -7), Vector (23.4091, 8.19992, -7), Vector (22.7365, 8.45805, -7)]
#print IfConcave(pts)
#print triangulate(pts)