forked from patmo141/odc_public
-
Notifications
You must be signed in to change notification settings - Fork 0
/
common_drawing.py
316 lines (257 loc) · 9.58 KB
/
common_drawing.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
'''
Copyright (C) 2014 CG Cookie
http://cgcookie.com
hello@cgcookie.com
Created by Jonathan Denning, Jonathan Williamson, and Patrick Moore
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
'''
import bpy
import bgl
import blf
import bmesh
import time
import math
import random
from itertools import chain,combinations
from collections import deque
from bpy_extras import view3d_utils
from mathutils import Vector, Matrix, Quaternion
from mathutils.geometry import intersect_line_plane, intersect_point_line, distance_point_to_plane, intersect_line_line_2d, intersect_line_line
from bpy_extras.view3d_utils import location_3d_to_region_2d, region_2d_to_vector_3d, region_2d_to_location_3d, region_2d_to_origin_3d
from common_utilities import dprint
def bgl_col(rgb, alpha):
'''
takes a Vector of len 3 (eg, a color setting)
returns a 4 item tuple (r,g,b,a) for use with
bgl drawing.
'''
#TODO Test variables for acceptability
color = (rgb[0], rgb[1], rgb[2], alpha)
return color
def draw_points(context, points, color, size):
'''
draw a bunch of dots
args:
points: a list of tuples representing x,y SCREEN coordinate eg [(10,30),(11,31),...]
color: tuple (r,g,b,a)
size: integer? maybe a float
'''
bgl.glColor4f(*color)
bgl.glPointSize(size)
bgl.glBegin(bgl.GL_POINTS)
for coord in points:
bgl.glVertex2f(*coord)
bgl.glEnd()
return
def draw_3d_points(context, points, color, size):
'''
draw a bunch of dots
args:
points: a list of tuples representing x,y SCREEN coordinate eg [(10,30),(11,31),...]
color: tuple (r,g,b,a)
size: integer? maybe a float
'''
points_2d = [location_3d_to_region_2d(context.region, context.space_data.region_3d, loc) for loc in points]
bgl.glColor4f(*color)
bgl.glPointSize(size)
bgl.glBegin(bgl.GL_POINTS)
for coord in points_2d:
#TODO: Debug this problem....perhaps loc_3d is returning points off of the screen.
if coord:
bgl.glVertex2f(*coord)
else:
print('how the f did nones get in here')
print(coord)
bgl.glEnd()
return
def draw_3d_points_and_index(context, points, color, size):
'''
draw a bunch of dots
args:
points: a list of tuples representing x,y SCREEN coordinate eg [(10,30),(11,31),...]
color: tuple (r,g,b,a)
size: integer? maybe a float
'''
points_2d = [location_3d_to_region_2d(context.region, context.space_data.region_3d, loc) for loc in points]
bgl.glColor4f(*color)
bgl.glPointSize(size)
bgl.glBegin(bgl.GL_POINTS)
for coord in points_2d:
#TODO: Debug this problem....perhaps loc_3d is returning points off of the screen.
if coord:
bgl.glVertex2f(*coord)
else:
continue
bgl.glEnd()
blf.size(0, 14, 72)
for i, coord in enumerate(points_2d):
blf.position(0,coord[0]+3, coord[1]+3, 0)
blf.draw(0,str(i))
return
def draw_circle(context, c,n,r,col,step=10):
x = Vector((0.42,-0.42,0.42)).cross(n).normalized() * r
y = n.cross(x).normalized() * r
d2r = math.pi/180
p3d = [c+x*math.cos(i*d2r)+y*math.sin(i*d2r) for i in range(0,360+step,step)]
draw_polyline_from_3dpoints(context, p3d, col, 1, "GL_LINE_SMOOTH")
def draw_3d_points(context, points, color, size):
'''
draw a bunch of dots
args:
points: a list of tuples representing x,y SCREEN coordinate eg [(10,30),(11,31),...]
color: tuple (r,g,b,a)
size: integer? maybe a float
'''
points_2d = [location_3d_to_region_2d(context.region, context.space_data.region_3d, loc) for loc in points]
bgl.glColor4f(*color)
bgl.glPointSize(size)
bgl.glBegin(bgl.GL_POINTS)
for coord in points_2d:
#TODO: Debug this problem....perhaps loc_3d is returning points off of the screen.
if coord:
bgl.glVertex2f(*coord)
else:
print('how the f did nones get in here')
print(coord)
bgl.glEnd()
return
def draw_polyline_from_points(context, points, color, thickness, LINE_TYPE):
'''
a simple way to draw a line
args:
points: a list of tuples representing x,y SCREEN coordinate eg [(10,30),(11,31),...]
color: tuple (r,g,b,a)
thickness: integer? maybe a float
LINE_TYPE: eg...bgl.GL_LINE_STIPPLE or
'''
if LINE_TYPE == "GL_LINE_STIPPLE":
bgl.glLineStipple(4, 0x5555) #play with this later
bgl.glEnable(bgl.GL_LINE_STIPPLE)
bgl.glEnable(bgl.GL_BLEND)
current_width = bgl.GL_LINE_WIDTH
bgl.glColor4f(*color)
bgl.glLineWidth(thickness)
bgl.glBegin(bgl.GL_LINE_STRIP)
for coord in points:
bgl.glVertex2f(*coord)
bgl.glEnd()
bgl.glLineWidth(1)
if LINE_TYPE == "GL_LINE_STIPPLE":
bgl.glDisable(bgl.GL_LINE_STIPPLE)
bgl.glEnable(bgl.GL_BLEND) # back to uninterupted lines
return
def draw_polyline_from_3dpoints(context, points_3d, color, thickness, LINE_TYPE):
'''
a simple way to draw a line
slow...becuase it must convert to screen every time
but allows you to pan and zoom around
args:
points_3d: a list of tuples representing x,y SCREEN coordinate eg [(10,30),(11,31),...]
color: tuple (r,g,b,a)
thickness: integer? maybe a float
LINE_TYPE: eg...bgl.GL_LINE_STIPPLE or
'''
points = [location_3d_to_region_2d(context.region, context.space_data.region_3d, loc) for loc in points_3d]
if LINE_TYPE == "GL_LINE_STIPPLE":
bgl.glLineStipple(4, 0x5555) #play with this later
bgl.glEnable(bgl.GL_LINE_STIPPLE)
bgl.glEnable(bgl.GL_BLEND)
bgl.glColor4f(*color)
bgl.glLineWidth(thickness)
bgl.glBegin(bgl.GL_LINE_STRIP)
for coord in points:
if coord:
bgl.glVertex2f(*coord)
bgl.glEnd()
if LINE_TYPE == "GL_LINE_STIPPLE":
bgl.glDisable(bgl.GL_LINE_STIPPLE)
bgl.glEnable(bgl.GL_BLEND) # back to uninterupted lines
bgl.glLineWidth(1)
return
def draw_quads_from_3dpoints(context, points_3d, color):
'''
a simple way to draw a set of quads
slow...becuase it must convert to screen every time
but allows you to pan and zoom around
args:
points_3d: a list of tuples as x,y,z
color: tuple (r,g,b,a)
'''
points = [location_3d_to_region_2d(context.region, context.space_data.region_3d, loc) for loc in points_3d]
bgl.glEnable(bgl.GL_BLEND)
bgl.glColor4f(*color)
bgl.glBegin(bgl.GL_QUADS)
for coord in points:
bgl.glVertex2f(*coord)
bgl.glEnd()
return
def draw_outline_or_region(mode, points, color):
'''
arg:
mode - either bgl.GL_POLYGON or bgl.GL_LINE_LOOP
color - will need to be set beforehand using theme colors. eg
bgl.glColor4f(self.ri, self.gi, self.bi, self.ai)
'''
bgl.glColor4f(color[0],color[1],color[2],color[3])
if mode == 'GL_LINE_LOOP':
bgl.glBegin(bgl.GL_LINE_LOOP)
else:
bgl.glEnable(bgl.GL_BLEND)
bgl.glBegin(bgl.GL_POLYGON)
# start with corner right-bottom
for i in range(0,len(points)):
bgl.glVertex2f(points[i][0],points[i][1])
bgl.glEnd()
def round_box(minx, miny, maxx, maxy, rad, corners = [True, True, True, True]):
'''
TODO, make smarter indexing decisions so only some corners have
to be rounded
'''
vec0 = [[0.195, 0.02],
[0.383, 0.067],
[0.55, 0.169],
[0.707, 0.293],
[0.831, 0.45],
[0.924, 0.617],
[0.98, 0.805]]
#cache so we only scale the corners once
vec = [[0,0]]*len(vec0)
for i in range(0,len(vec0)):
vec[i] = [vec0[i][0]*rad, vec0[i][1]*rad]
verts = [[0,0]]*(9*4)
# start with corner right-bottom
verts[0] = [maxx-rad,miny]
for i in range(1,8):
verts[i]= [maxx - rad + vec[i-1][0], miny + vec[i-1][1]] #done
verts[8] = [maxx, miny + rad] #done
#corner right-top
verts[9] = [maxx, maxy - rad]
for i in range(10,17):
verts[i]= [maxx - vec[i-10][1], maxy - rad + vec[i-10][0]]
verts[17] = [maxx-rad, maxy]
#corver left top
verts[18] = [minx + rad, maxy]
for i in range(19,26):
verts[i]= [minx + rad - vec[i-19][0], maxy - vec[i-19][1]] #done
verts[26] = [minx, maxy - rad]
#corner left bottom
verts[27] = [minx, miny+rad]
for i in range(28,35):
verts[i]= [minx + vec[i-28][1], miny + rad - vec[i-28][0]] #done
verts[35]=[minx + rad, miny]
return verts
def draw_bmedge(context, bmedge, mx, thickness, color):
'''
simple wrapper to drawp a bmedge
'''
points = [mx * bmedge.verts[0].co, mx*bmedge.verts[1].co]
draw_polyline_from_3dpoints(context, points, color, thickness, 'GL_LINE_SMOOTH')