Skip to content

Commit 3d34784

Browse files
committed
misc updates
1 parent 3a6f865 commit 3d34784

File tree

9 files changed

+51
-107
lines changed

9 files changed

+51
-107
lines changed

CHANGELOG.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Changelog
22

3-
## [v0.8.0] - ???
3+
## [v0.8.0] - 2022-05-20 11:01:59
44

55
### Added
66

@@ -12,6 +12,7 @@
1212
- Project.toml
1313
- some point3d comparisons
1414
- fixed wrong spherical/cartesian conversions
15+
- eachindex()
1516

1617
### Removed
1718

Project.toml

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ Requires = "ae029012-a4dd-5104-9daa-d747884805df"
1414

1515
[compat]
1616
julia = "1"
17-
Luxor = "3.1"
18-
StaticArrays = "0.10, 0.11, 0.12, 1.0"
17+
Luxor = "3.1, 3.2"
18+
StaticArrays = "0.10, 0.11, 0.12, 1"
1919
Rotations = "1"
2020
Colors = "0.7, 0.8, 0.9, 0.10, 0.11, 0.12, 0.13, 0.14"
2121
LaTeXStrings = "1.1, 1.2, 1.3"

docs/src/assets/figures/eyepoint.gif

244 KB
Loading

docs/src/objects.md

+8
Original file line numberDiff line numberDiff line change
@@ -294,3 +294,11 @@ nothing # hide
294294
```
295295

296296
![custom object](assets/figures/juliaspheres.svg)
297+
298+
This code uses the surface normal of each rectangular facet to change the color. The surface normal is an imaginary line that meets the facet at right angles, and indicates the direction of that facet. If you measure the distance between the surface normal and the direction of, say, the direction of a line from the origin to the eyepoint, you can obtain a value that indicates the orientation of the facet. You can then use this, as here, to change the color: an angle approaching π suggests that the facet is almost facing the viewer, and you can color it accordingly.
299+
300+
![surface normal](assets/figures/eyepoint.gif)
301+
302+
!!! note
303+
304+
It's hard work doing it all like this! There are easier ways...

examples/latex3d.jl

+25-13
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,36 @@ using Thebes
22
using Luxor
33
using LaTeXStrings
44
using MathTeXEngine
5-
using MathTeXEngine
65
using Rotations
76

8-
@draw begin
7+
function frame(scene, framenumber)
8+
eased_n = scene.easingfunction(framenumber - scene.framerange.start,
9+
0, 1, scene.framerange.stop - scene.framerange.start)
10+
911
background(0.0, 0.05, 0.1)
1012
helloworld()
11-
perspective(300)
12-
eyepoint(200, 200, 200)
13+
perspective(600)
14+
eyepoint(200, 20, 50)
1315
sethue("white")
14-
fontsize(20)
16+
fontsize(40)
1517
setline(1)
16-
e = L"e^{i\pi} + 1 = 0"
17-
for i in 0:π/10:2π - π/10
18-
text3D(e,
19-
sphericaltocartesian(50, i, π/2),
20-
action=:stroke,
21-
about=sphericaltocartesian(50, i, π/2),
22-
rotation=RotZ(i),
23-
portion=1.0)
18+
19+
t₁ = L"e^{i\pi} + 1 = 0"
20+
21+
sethue("gold")
22+
23+
for x in 0:25:500
24+
setopacity(rescale(x, 0, 500, 1, 0))
25+
leftpt = Point3D(-x, 0, 0)
26+
text3D(t₁, leftpt,
27+
action=:stroke,
28+
halign=:center,
29+
about=leftpt,
30+
rotation=RotYXZ(-π/2, π, -π/2),
31+
portion=eased_n)
2432
end
33+
2534
end
35+
36+
amovie = Movie(600, 600, "latex the movie")
37+
animate(amovie, Scene(amovie, frame, 1:150), creategif=true, pathname="/tmp/latexanimation.gif")

src/Object.jl

+8-8
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ function make(vf, name="unnamed")
5252
# don't redefine when passed an array
5353
vertices = deepcopy(vf[1])
5454
faces = deepcopy(vf[2])
55-
labels = collect(1:length(faces))
55+
labels = collect(eachindex(faces))
5656
return Object(vertices, faces, labels, name)
5757
end
5858

@@ -106,7 +106,7 @@ function objecttopoly(o::Object)
106106
end
107107
facepolys = Array[]
108108
if length(o.faces) > 0
109-
for n in 1:length(o.faces)
109+
for n in eachindex(o.faces)
110110
push!(facepolys, vertices2D[o.faces[n]])
111111
end
112112
end
@@ -251,7 +251,7 @@ end
251251
Set the position of object to Point3D(x, y, z).
252252
"""
253253
function moveby!(o::Object, x, y, z)
254-
for n in 1:length(o.vertices)
254+
for n in eachindex(o.vertices)
255255
nv = o.vertices[n]
256256
o.vertices[n] = Point3D(nv.x + x, nv.y + y, nv.z + z)
257257
end
@@ -278,7 +278,7 @@ moveby!(o::Object, pt::Point3D) = moveby!(o::Object, pt.x, pt.y, pt.z)
278278
Scale object by x in x, y in y, and z in z.
279279
"""
280280
function scaleby!(o::Object, x, y, z)
281-
for n in 1:length(o.vertices)
281+
for n in eachindex(o.vertices)
282282
nv = o.vertices[n]
283283
o.vertices[n] = Point3D(nv.x * x, nv.y * y, nv.z * z)
284284
end
@@ -306,14 +306,14 @@ Rotate an object through rotation `r`, or around the x, y,
306306
and/or z axis by `angleX`, `angleY`, `angleZ`.
307307
"""
308308
function rotateby!(o::Object, angleX, angleY, angleZ)
309-
for n in 1:length(o.vertices)
309+
for n in eachindex(o.vertices)
310310
o.vertices[n] = RotXYZ(angleX, angleY, angleZ) * o.vertices[n]
311311
end
312312
return o
313313
end
314314

315315
function rotateby!(o::Object, r::Rotation)
316-
for n in 1:length(o.vertices)
316+
for n in eachindex(o.vertices)
317317
o.vertices[n] = r * o.vertices[n]
318318
end
319319
return o
@@ -341,7 +341,7 @@ end
341341
Rotate an object around a point by rotation r, or angleX, angleY, angleZ.
342342
"""
343343
function rotateby!(o::Object, pt::Point3D, angleX, angleY, angleZ)
344-
for n in 1:length(o.vertices)
344+
for n in eachindex(o.vertices)
345345
v = o.vertices[n] - pt
346346
v = rotateX(v, angleX)
347347
v = rotateY(v, angleY)
@@ -352,7 +352,7 @@ function rotateby!(o::Object, pt::Point3D, angleX, angleY, angleZ)
352352
end
353353

354354
function rotateby!(o::Object, pt::Point3D, r::Rotation=RotXYZ(0, 0, 0))
355-
for n in 1:length(o.vertices)
355+
for n in eachindex(o.vertices)
356356
v = o.vertices[n] * r
357357
o.vertices[n] = v
358358
end

src/Point3D.jl

+2-2
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,7 @@ moveby!(ptlist::Array{Point3D, 1}, x, y, z) = moveby!(ptlist, Point3D(x, y, z))
343343
Scales a list of points by multiplying by `x` in X, `y` in Y, `z` in Z.
344344
"""
345345
function scaleby!(ptlist::Array{Point3D, 1}, x, y, z)
346-
for n in 1:length(ptlist)
346+
for n in eachindex(ptlist)
347347
v = ptlist[n]
348348
ptlist[n] = Point3D(v.x * x, v.y * y, v.z * z)
349349
end
@@ -357,7 +357,7 @@ Finds one of these.
357357
"""
358358
function surfacenormal(ptlist::Array{Point3D, 1})
359359
normal = Point3D(0, 0, 0)
360-
for i in 1:length(ptlist)
360+
for i in eachindex(ptlist)
361361
vertexCurrent = ptlist[i]
362362
vertexNext = ptlist[mod1(i + 1, end)]
363363
x = normal.x + ( (vertexCurrent.y - vertexNext.y) * (vertexCurrent.z + vertexNext.z))

src/latex.jl

+3-80
Original file line numberDiff line numberDiff line change
@@ -2,85 +2,7 @@ using LaTeXStrings
22
import MathTeXEngine:
33
generate_tex_elements, inkwidth, inkheight, bottominkbound, TeXChar, HLine
44

5-
# function text3D(str::LaTeXString, anchor::Point3D;
6-
# halign=:left,
7-
# valign=:baseline,
8-
# about=Point3D(0., 0., 0.),
9-
# rotation::Rotation=RotXYZ(0, 0, 0))
10-
#
11-
# # this is just a hacked version of the Luxor code
12-
#
13-
# # Function from MathTexEngine
14-
# sentence = generate_tex_elements(str)
15-
#
16-
# # Get current font size.
17-
# font_size = get_fontsize()
18-
#
19-
# textw, texth = latextextsize(str)
20-
# bottom_pt, top_pt = rawlatexboundingbox(str)
21-
#
22-
# translate_x, translate_y = Luxor.texalign(halign, valign, bottom_pt, top_pt, font_size)
23-
#
24-
# pt = project(anchor)
25-
#
26-
# rotationfixed=true
27-
# # Writes text using ModernCMU font.
28-
# for text in sentence
29-
# if isnothing(pt)
30-
# continue
31-
# end
32-
# @layer begin
33-
# translate(pt)
34-
# if !rotationfixed
35-
# #rotate(angle) ?
36-
# translate(translate_x, translate_y)
37-
# else
38-
# l_pt, r_pt = Luxor.latexboundingbox(str, halign = halign, valign = valign)
39-
# translate((l_pt + r_pt)/2)
40-
# #rotate(angle) ?
41-
# translate(Point(translate_x, translate_y) - (l_pt + r_pt)/2)
42-
# end
43-
# if text[1] isa TeXChar
44-
# fontface(text[1].font.family_name)
45-
# fontsize(font_size * text[3])
46-
#
47-
# textoutlines(string(text[1].char), Point(text[2]...) * font_size * (1, -1), :path,
48-
# halign=halign,
49-
# valign=valign,
50-
# startnewpath=true)
51-
# o = getpathflat()
52-
# newpath() # otherwise the path would be drawn twice
53-
# for e in o
54-
# if e.element_type == 0
55-
# (x, y) = e.points
56-
# newpt = rotateby(Point3D(anchor.x + x, anchor.y -y, anchor.z), about, rotation)
57-
# pin(newpt, gfunction = (p3, p2) -> move(p2))
58-
# elseif e.element_type == 1
59-
# (x, y) = e.points
60-
# newpt = rotateby(Point3D(anchor.x + x, anchor.y -y, anchor.z), about, rotation)
61-
# pin(newpt, gfunction = (p3, p2) -> line(p2))
62-
# elseif e.element_type == 3
63-
# closepath()
64-
# else
65-
# error("unknown path element " * repr(e.element_type) * repr(e.points))
66-
# end
67-
# end
68-
# fillpath()
69-
#
70-
# elseif text[1] isa HLine
71-
# pointstart = Point(text[2]...) * font_size * (1, -1)
72-
# pointend = pointstart + Point(text[1].width, 0) * font_size
73-
#
74-
# newstartpt = rotateby(Point3D(anchor.x + pointstart.x, anchor.y - pointstart.y, anchor.z), about, rotation)
75-
# newendpt = rotateby(Point3D(anchor.x + pointend.x, anchor.y - pointend.y, anchor.z), about, rotation)
76-
#
77-
# pin(newstartpt, newendpt)
78-
# end
79-
# end
80-
# end
81-
# end
82-
83-
# try this version using Paths
5+
# try this version using Paths
846

857
"""
868
text3D(str::LaTeXString, anchor::Point3D;
@@ -93,7 +15,7 @@ text3D(str::LaTeXString, anchor::Point3D;
9315
startnewpath=true,
9416
action=:fill)
9517
96-
Like `text3D()` but draws LaTeX strings.
18+
Like `text3D()` but draws LaTeX strings. Needs Luxor >= 3.1.
9719
"""
9820
function text3D(str::LaTeXString, anchor::Point3D;
9921
halign=:left,
@@ -104,6 +26,7 @@ function text3D(str::LaTeXString, anchor::Point3D;
10426
steps = 20,
10527
startnewpath=true,
10628
action=:fill)
29+
10730
text(str, paths=true, halign=halign, valign=valign) # angle? rotationfixed?
10831
latexpath = storepath()
10932
drawpath(latexpath, portion, anchor, action=action, startnewpath=startnewpath, about=about, rotation=rotation, steps=steps)

test/test-4.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ function main()
2424
for (pos, n) in tiles
2525
@layer begin
2626
translate(pos)
27-
object = make(moreobjects[rand(1:length(moreobjects))])
27+
object = make(moreobjects[rand(eachindex(moreobjects))])
2828

2929
scaleby!(object, 30, 30, 30)
3030
moveby!(object, 1 * rand(), 1 * rand(), 10 * rand())

0 commit comments

Comments
 (0)