Skip to content

Commit 7149485

Browse files
committed
Started markers chapter
1 parent fa570c2 commit 7149485

37 files changed

+1034
-134
lines changed

08-markers.rst

+420-37
Large diffs are not rendered by default.

book.html

+442-96
Large diffs are not rendered by default.
File renamed without changes.
File renamed without changes.
File renamed without changes.

code/chapter-08/marker.py

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# -----------------------------------------------------------------------------
2+
# Python & OpenGL for Scientific Visualization
3+
# www.labri.fr/perso/nrougier/python+opengl
4+
# Copyright (c) 2017, Nicolas P. Rougier
5+
# Distributed under the 2-Clause BSD License.
6+
# -----------------------------------------------------------------------------
7+
import numpy as np
8+
from glumpy import app, gl, gloo
9+
from glumpy.transforms import Position, OrthographicProjection, PanZoom
10+
11+
# Create window
12+
window = app.Window(width=2*512, height=512, color=(1,1,1,1))
13+
14+
# What to draw when necessary
15+
@window.event
16+
def on_draw(dt):
17+
window.clear()
18+
program.draw(gl.GL_POINTS)
19+
program['orientation'][-1] += np.pi/1024.0
20+
21+
# Setup some markers
22+
n = 500+1
23+
data = np.zeros(n, dtype=[('position', np.float32, 2),
24+
('fg_color', np.float32, 4),
25+
('bg_color', np.float32, 4),
26+
('size', np.float32, 1),
27+
('orientation', np.float32, 1),
28+
('linewidth', np.float32, 1)])
29+
data = data.view(gloo.VertexBuffer)
30+
data['linewidth'] = 1
31+
data['fg_color'] = 0, 0, 0, 1
32+
data['bg_color'] = 1, 1, 1, 0
33+
data['orientation'] = 0
34+
radius, theta, dtheta = 250.0, 0.0, 5.5 / 180.0 * np.pi
35+
for i in range(500):
36+
theta += dtheta
37+
x = 256 + radius * np.cos(theta)
38+
y = 256 + radius * np.sin(theta)
39+
r = 10.0 - i * 0.02
40+
radius -= 0.45
41+
data['orientation'][i] = theta - np.pi/2
42+
data['position'][i] = x, y
43+
data['size'][i] = 2 * r
44+
data['linewidth'][i] = 1
45+
46+
data['position'][-1] = 512+256, 256
47+
data['size'][-1] = 512/np.sqrt(2)
48+
data['linewidth'][-1] = 3.0
49+
data['fg_color'][-1] = 0, 0, 0, 1
50+
data['bg_color'][-1] = .95, .95, .95, 1
51+
data['orientation'][-1] = 0
52+
53+
program = gloo.Program("markers/marker.vert", "markers/marker.frag")
54+
program.bind(data)
55+
program['antialias'] = 1.00
56+
57+
# <marker> : "arrow", "asterisk", "chevron", "clover", "club",
58+
# "cross", "diamond", "disc", "ellipse", "hbar",
59+
# "heart", "infinity", "pin", "ring", "spade",
60+
# "square", "tag", "triangle", "vbar"
61+
program['marker'] = "asterisk"
62+
63+
# <paint> : "stroke", "filled" or "outline"
64+
program['paint'] = "outline"
65+
66+
transform = OrthographicProjection(Position("position"))
67+
program['transform'] = transform
68+
window.attach(transform)
69+
70+
app.run()

code/hardware-msaa.py

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# -----------------------------------------------------------------------------
2+
# Python & OpenGL for Scientific Visualization
3+
# www.labri.fr/perso/nrougier/python+opengl
4+
# Copyright (c) 2017, Nicolas P. Rougier
5+
# Distributed under the 2-Clause BSD License.
6+
# -----------------------------------------------------------------------------
7+
import numpy as np
8+
from glumpy import app, gloo, gl
9+
10+
vertex = """
11+
uniform vec2 resolution;
12+
// attribute vec2 position;
13+
in vec2 position;
14+
void main()
15+
{ gl_Position = vec4(2.0*position/resolution-1.0, 0.0, 1.0); }
16+
"""
17+
18+
fragment = """
19+
out vec4 color;
20+
void main() { color = vec4(vec3(0.0), 1.0); }
21+
// void main() { gl_FragColor = vec4(vec3(0.0), 1.0); }
22+
"""
23+
24+
config = app.configuration.Configuration()
25+
config.samples = 6
26+
config.major_version = 3
27+
config.minor_version = 2
28+
config.profile = "core"
29+
GLSL_version = "150"
30+
window = app.Window(32, 8, color=(1,1,1,1), config=config)
31+
32+
triangle = gloo.Program(vertex, fragment, version=GLSL_version)
33+
V = np.zeros(3, dtype=[("position", np.float32,2)])
34+
V["position"] = (1.,3.), (7.,7.), (30., 1.)
35+
V = V.view(gloo.VertexArray)
36+
triangle.bind(V)
37+
38+
@window.event
39+
def on_resize(width, height):
40+
triangle["resolution"] = width, height
41+
42+
@window.event
43+
def on_init():
44+
gl.glEnable(gl.GL_MULTISAMPLE)
45+
pass
46+
47+
@window.event
48+
def on_draw(dt):
49+
window.clear()
50+
triangle.draw(gl.GL_TRIANGLE_STRIP)
51+
52+
53+
for i in range(window.config.samples):
54+
print(gl.glGetMultisamplefv(gl.GL_SAMPLE_POSITION, i))
55+
56+
app.run()
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

images/chapter-08/marker-arrow.png

2.79 KB
Loading

images/chapter-08/marker-asterisk.png

3.44 KB
Loading

images/chapter-08/marker-bar.png

1.63 KB
Loading

images/chapter-08/marker-chevron.png

2.99 KB
Loading

images/chapter-08/marker-clover.png

10.9 KB
Loading

images/chapter-08/marker-club.png

10.9 KB
Loading

images/chapter-08/marker-cross.png

4.19 KB
Loading

images/chapter-08/marker-diamond.png

3.29 KB
Loading

images/chapter-08/marker-disc.png

10.4 KB
Loading

images/chapter-08/marker-ellipse.png

7.8 KB
Loading

images/chapter-08/marker-heart.png

6.78 KB
Loading

images/chapter-08/marker-infinity.png

13.8 KB
Loading

images/chapter-08/marker-pin.png

11.3 KB
Loading

images/chapter-08/marker-ring.png

14.8 KB
Loading

images/chapter-08/marker-spade.png

8.4 KB
Loading

images/chapter-08/marker-triangle.png

2.51 KB
Loading

rst2html.py

+46-1
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,33 @@
1212

1313
class video(nodes.General, nodes.Inline, nodes.Element): pass
1414
class media(nodes.General, nodes.Inline, nodes.Element): pass
15+
class figref(nodes.Inline, nodes.TextElement): pass
1516

1617

17-
18+
# --- Figure references -------------------------------------------------------
19+
#
20+
# This class solves pending figure references throughout the whole document
21+
#
22+
# TODO: Use "section.subsection.number" format.
23+
class FigureReferences(Transform):
24+
default_priority = 260
25+
def apply(self):
26+
num = 0
27+
numbers = {}
28+
29+
for node in self.document.traverse(nodes.figure):
30+
if node['label'] is not None:
31+
num += 1
32+
node['number'] = num
33+
numbers[node['label']] = num
34+
else:
35+
node['number'] = None
36+
37+
for node in self.document.traverse(figref):
38+
if node['target'] not in numbers:
39+
continue
40+
num = '(%d)' % numbers[node['target']]
41+
node[0] = nodes.Text(num, num)
1842

1943
# --- video directive ---------------------------------------------------------
2044
#
@@ -135,6 +159,27 @@ def run(self):
135159
directives.register_directive('figure', Figure)
136160

137161

162+
# --- fig role ----------------------------------------------------------------
163+
#
164+
# `fig` role allows to refer to a figure identified by a label
165+
#
166+
def fig_role(role, rawtext, text, lineno, inliner, options={}, content=[]):
167+
text = utils.unescape(text)
168+
node = figref('(?)', '(?)', target=text)
169+
pending = nodes.pending(FigureReferences)
170+
inliner.document.note_pending(pending)
171+
return [node], []
172+
fig_role.content = True
173+
roles.register_canonical_role('fig', fig_role)
174+
175+
176+
# --- html figref --------------------------------------------------------------
177+
def html_visit_figref(self, node):
178+
self.body.append('<a href="#figure-%s">' % node['target'])
179+
def html_depart_figref(self, node):
180+
self.body.append('</a>')
181+
HTMLTranslator.visit_figref = html_visit_figref
182+
HTMLTranslator.depart_figref = html_depart_figref
138183

139184
# --- html video --------------------------------------------------------------
140185
def html_visit_video(self, node):

0 commit comments

Comments
 (0)