Skip to content
This repository has been archived by the owner on Nov 22, 2023. It is now read-only.

Switch to StaticArrays #76

Merged
merged 8 commits into from
Apr 6, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,42 @@ Pyramid(Point3f0(0), 1f0, 1f0)
load("cat.obj") # --> GLNormalMesh, via FileIO
```
<img src="https://cloud.githubusercontent.com/assets/1010467/14317773/1c4087f6-fc0a-11e5-95c5-97d4cd840c1a.png" width="132">


# Displaying primitives

To display geometry primitives, they need to be decomposable.
This can be done for any arbitrary primitive, by overloading the following interface:
```Julia
# Lets take SimpleRectangle as an example:
# Minimal set of decomposable attributes to build up a triangle mesh
isdecomposable{T<:Point, HR<:SimpleRectangle}(::Type{T}, ::Type{HR}) = true
isdecomposable{T<:Face, HR<:SimpleRectangle}(::Type{T}, ::Type{HR}) = true

# Example implementation of decompose for points
function decompose{PT}(P::Type{Point{3, PT}}, r::SimpleRectangle, resolution=(2,2))
w,h = resolution
vec(P[(x,y,0) for x=linspace(r.x, r.x+r.w, w), y=linspace(r.y, r.y+r.h, h)])
end

function decompose{T<:Face}(::Type{T}, r::SimpleRectangle, resolution=(2,2))
w,h = resolution
faces = vec([Face{4, Int, 0}(
sub2ind(resolution, i, j), sub2ind(resolution, i+1, j),
sub2ind(resolution, i+1, j+1), sub2ind(resolution, i, j+1)
) for i=1:(w-1), j=1:(h-1)]
)
decompose(T, faces)
end
```
With these methods defined, this constructor will magically work:
```Julia
rect = SimpleRectangle(...)
mesh = GLNormalMesh(rect)
vertices(mesh) == decompose(Point3f0, rect)

faces(mesh) == decompose(GLTriangle, rect) # Face{3, UInt32, 0} == GLTriangle
normals(mesh) # automatically calculated from mesh
```
As you can see, the normals are automatically calculated only with the faces and points.
You can overwrite that behavior, by also defining decompose for the `Normal` type!
4 changes: 2 additions & 2 deletions appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ environment:
matrix:
- JULIAVERSION: "julialang/bin/winnt/x86/0.4/julia-0.4-latest-win32.exe"
- JULIAVERSION: "julialang/bin/winnt/x64/0.4/julia-0.4-latest-win64.exe"
- JULIAVERSION: "julianightlies/bin/winnt/x86/julia-latest-win32.exe"
- JULIAVERSION: "julianightlies/bin/winnt/x64/julia-latest-win64.exe"
- JULIAVERSION: "julialang/bin/winnt/x86/0.5/julia-0.5-latest-win32.exe"
- JULIAVERSION: "julialang/bin/winnt/x64/0.5/julia-0.5-latest-win64.exe"

branches:
only:
Expand Down
2 changes: 1 addition & 1 deletion src/decompose.jl
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ function decompose{PT}(P::Type{Point{3, PT}}, r::SimpleRectangle, resolution=(2,
end
function decompose{UVT}(T::Type{UV{UVT}}, r::SimpleRectangle, resolution=(2,2))
w,h = resolution
vec(T[(x,y) for x=linspace(0, 1, w), y=linspace(0, 1, h)])
vec(T[(x, y) for x = linspace(0, 1, w), y = linspace(1, 0, h)])
end
function decompose{T<:Normal}(::Type{T}, r::SimpleRectangle, resolution=(2,2))
fill(T(0,0,1), prod(resolution))
Expand Down
2 changes: 1 addition & 1 deletion src/hyperrectangles.jl
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ function SimpleRectangle{T}(position::Vec{2,T}, width::Vec{2,T})
end

function getindex{T}(a::Array{T,2}, rect::SimpleRectangle)
a[rect.x+1:rect.w, rect.y+1:rect.h]
a[rect.x+1:(rect.x+rect.w), rect.y+1:(rect.y+rect.h)]
end

function setindex!{T}(a::Array{T,2}, b::Array{T,2}, rect::SimpleRectangle)
Expand Down
6 changes: 3 additions & 3 deletions src/polygons.jl
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ function polygon2faces{P<:Point}(
)
#= allocate and initialize list of Vertices in polygon =#
result = facetype[]

# the algorithm doesn't like closed contours
if isapprox(last(contour), first(contour))
pop!(contour)
Expand Down Expand Up @@ -137,10 +137,10 @@ function topoint{T}(::Type{Point{2, T}}, p::Point{3, T})
Point{2, T}(p[1], p[2])
end

@compat function (::Type{M}){M<:AbstractMesh, P<:Point}(
@compat function (::Type{M}){M <: AbstractMesh, P <: Point}(
points::AbstractArray{P}
)
faces = polygon2faces(points, facetype(M))
VT = vertextype(M)
GLNormalMesh(faces = faces, vertices = VT[topoint(VT, p) for p in points])
M(faces = faces, vertices = VT[topoint(VT, p) for p in points])
end
30 changes: 19 additions & 11 deletions src/primitives.jl
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,25 @@ end

@compat function (meshtype::Type{T}){T <: HMesh,HT}(
c::Union{HyperCube{3,T}, HyperRectangle{3,HT}}

) xdir = Vec{3, HT}(widths(c)[1],0f0,0f0)
ydir = Vec{3, HT}(0f0,widths(c)[2],0f0)
zdir = Vec{3, HT}(0f0,0f0,widths(c)[3])
)
xdir = Vec{3, HT}(1, 0, 0)
ydir = Vec{3, HT}(0, 1, 0)
zdir = Vec{3, HT}(0, 0, 1)
o0 = zero(Vec{3, HT})
quads = [
Quad(origin(c) + zdir, xdir, ydir), # Top
Quad(origin(c), ydir, xdir), # Bottom
Quad(origin(c) + xdir, ydir, zdir), # Right
Quad(origin(c), zdir, ydir), # Left
Quad(origin(c), xdir, zdir), # Back
Quad(origin(c) + ydir, zdir, xdir) #Front
Quad(zdir, xdir, ydir), # Top
Quad(o0, ydir, xdir), # Bottom
Quad(xdir, ydir, zdir), # Right
Quad(o0, zdir, ydir), # Left
Quad(o0, xdir, zdir), # Back
Quad(ydir, zdir, xdir) #Front
]
merge(map(meshtype, quads))
mesh = merge(map(meshtype, quads))
v = vertices(mesh)
w = widths(c)
o = origin(c)
map!(v, v) do v
(v .* w) + o
end
mesh
end
12 changes: 6 additions & 6 deletions test/decompose.jl
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,15 @@ end

uvs = decompose(UV{Float64}, mesh)
uv_target = UV{Float64}[
(0.0,0.0)
(0.5,0.0)
(1.0,0.0)
(0.0,0.5)
(0.5,0.5)
(1.0,0.5)
(0.0,1.0)
(0.5,1.0)
(1.0,1.0)
(0.0,0.5)
(0.5,0.5)
(1.0,0.5)
(0.0,0.0)
(0.5,0.0)
(1.0,0.0)
]
@test uvs == uv_target
end
Expand Down