Skip to content
This repository has been archived by the owner on Oct 23, 2022. It is now read-only.

Commit

Permalink
BrianGun/issue257 (#264)
Browse files Browse the repository at this point in the history
* Tessellation tools: clusters
Fixes #257
added cluster lattice
modified latticebasis to use matrix to represent basis vectors instead of SVector{SVector}}. Simpler.

* see previous commit

* started working on lattice clusters

* Tessellation tools: clusters
Fixes #257

working on LatticeCluster. LatticeBasis constructor is allocating for obscure reasons. This is probably not a performance issue since this shouldn't be in performance critical parts of the code.

* Tessellation tools: clusters
Fixes #257

basic cluster lattices seem to be working. Need to write better visualization code.

* added another cluster shape hexRGBW()

* Tessellation tools: clusters
Fixes #257

added function to create example LatticeCluster

reworking draw for clusters so it is more sensible.

* Tessellation tools: clusters
Fixes #257

incomplete type signature on basis function for LatticeBasis was causing allocations and huge increase in computation. Fixed now.

* Tessellation tools: clusters
Fixes #257

variable renaming

* Tessellation tools: clusters
Fixes #257

changed draw functions to draw any type of lattice shape not just HexBasis1.

Added Hex12RGB function to make hex12 pattern. Not working yet.

* hex12RGB function appears to be working. Changed drawing functions for lattices so they will draw cell names as well as indices.

fixed error in HexBasis3 basis function

updated examples so hex drawings work with the new code

* Tessellation tools: clusters
Fixes #257

comment change

* deleted include of Projections.jl since file is deleted

* Tessellation tools: clusters
Fixes #257
removed unused code from VisRepeatingStructures.jl

* added license header

* Tessellation tools: clusters
Fixes #257

fixed naming error for file repeating_structure_examples.jl

moved example cluster functions to the example file

added more examples for creating and drawing ClusterWithProperties

* fixed bug in hexrectcells. Was returning Vector{Tuple} code had changed to require Matrix{2,N}

* updated all examples in repeatin_structure_examples.jl to work with cells defined as 2xN matrix instead of SVector of Tuple{Int,Int}

* fixed bug in tests. Had incorrect name for hexdrawneighbors().

* Tessellation tools: clusters
Fixes #257

modified Arrangement.jl to work with lattice functions.

* modifying get_shapes to use lattice functions better

* Tessellation tools: clusters
Fixes #257

simplifying get_shapes

* changed keyword radius to size for consistency

* finished modifying get_shapes
  • Loading branch information
BrianGun authored Aug 18, 2021
1 parent 1fce5a5 commit f3bfebb
Show file tree
Hide file tree
Showing 16 changed files with 396 additions and 207 deletions.
2 changes: 1 addition & 1 deletion src/Examples/Examples.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import Luxor

include("docs_examples.jl")
include("other_examples.jl")
include("repeating_structure_examples.jl.jl")
include("repeating_structure_examples.jl")

end #module Examples
export Examples
107 changes: 107 additions & 0 deletions src/Examples/repeating_structure_examples.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
# MIT license
# Copyright (c) Microsoft Corporation. All rights reserved.
# See LICENSE in the project root for full license information.



#############################################################################
drawrectlattice() = Vis.drawcells(Repeat.RectangularBasis(),50.0,SMatrix{2,4,Int64}(0,0,0,1,1,0,1,1))
export drawrectlattice

"""draw the 2 ring neighbors of the hex cell at coordinates (0,0)"""
drawhexneighbors() = Vis.drawcells(Repeat.HexBasis1(),50,Repeat.neighbors(Repeat.HexBasis1,(0,0),2))
export drawneighbors

"""draw hex cell at coordinates (0,0) and the 1 and 2 ring neighbors"""
drawhexregion() = Vis.drawcells(Repeat.HexBasis1(),50,Repeat.region(Repeat.HexBasis1,(0,0),2))
export drawhexregion

"""draw hex cells that fit within a rectangular box centered at coordinates (0,0). Use fill color yellow."""
function drawhexrect()
cells = Repeat.hexcellsinbox(2,2)
Vis.drawcells(Repeat.HexBasis1(),50,cells,color = repeat(["yellow"],length(cells)))
end
export drawhexrect

"""draw hex cells that fit within a rectangular box centered at coordinates (0,0). Use random fill colors selected for maximum distinguishability."""
function drawhexrectcolors()
cells = Repeat.hexcellsinbox(4,4)
Vis.drawcells(Repeat.HexBasis1(),30,cells)
end
export drawhexrectcolors

""" Create a LatticeCluser with three elements at (0,0),(-1,0),(-1,1) coordinates in the HexBasis1 lattice"""
function hex3cluster()
clusterelts = SVector((0,0),(-1,0),(-1,1))
eltlattice = HexBasis1()
clusterbasis = LatticeBasis(( -1,2),(2,-1))
return LatticeCluster(clusterbasis,eltlattice,clusterelts)
end
export hex3cluster

""" Create a ClusterWithProperties with three types of elements, R,G,B """
function hex3RGB()
clusterelements = SVector((0,0),(-1,0),(-1,1))
colors = [color("red"),color("green"),color("blue")]
names = ["R","G","B"]
eltlattice = HexBasis1()
clusterbasis = LatticeBasis(( -1,2),(2,-1))
lattice = LatticeCluster(clusterbasis,eltlattice,clusterelements)
properties = DataFrame(Color = colors, Name = names)
return ClusterWithProperties(lattice,properties)
end
export hex3RGB

""" Create a ClusterWithProperties with four types of elements, R,G,B,W """
function hexRGBW()
clusterelements = SVector((0,0),(-1,0),(-1,1),(0,-1))
colors = [color("red"),color("green"),color("blue"),color("white")]
names = ["R","G","B","W"]
eltlattice = HexBasis1()
clusterbasis = LatticeBasis((0,2),(2,-2))
lattice = LatticeCluster(clusterbasis,eltlattice,clusterelements)
properties = DataFrame(Color = colors, Name = names)
return ClusterWithProperties(lattice,properties)
end
export hexRGBW

function hex12RGB()
clusterelements = SVector(
(-1,1),(0,1),
(-1,0),(0,0),(1,0),
(-1,-1),(0,-1),(1,-1),(2,-1),
(0,-2),(1,-2),(2,-2)
)
red = color("red")
grn = color("green")
blu = color("blue")
colors = [
grn,blu,
blu,red,grn,
red,grn,blu,red,
blu,red,grn
]
names = [
"G3","B0",
"B2","R1","G2",
"R0","G1","B1","R3",
"B3","R2","G0"
]
eltlattice = HexBasis3()
clusterbasis = LatticeBasis((2,2),(-3,2))
lattice = LatticeCluster(clusterbasis,eltlattice,clusterelements)
properties = DataFrame(Color = colors, Name = names)
return ClusterWithProperties(lattice,properties)
end
export hex12RGB

""" draw 3 repeats of hex3RGB cluster """
drawhex3RGB() = Vis.draw(hex3RGB(),[0 1 0; 0 0 1])
export drawhex3RGB

""" draw 3 repeats of hex12RGB cluster """
drawhex12RGB() = Vis.draw(hex12RGB(),[0 1 0; 0 0 1])
export drawhex12RGB



23 changes: 0 additions & 23 deletions src/Examples/repeating_structure_examples.jl.jl

This file was deleted.

112 changes: 30 additions & 82 deletions src/Optical/ParaxialAnalysis/HeadEyeModel/Arrangement.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,107 +10,55 @@ struct Shape{N, T} <: ArrangementConvexShape{N, T}
_points::Vector{SVector{N, T}}
_coordinates::Tuple{Int,Int}

function Shape(center::SVector{N, T}, points::Vector{SVector{N, T}}, coordinates::Tuple{Int,Int}) where {N, T<:Real}
function Shape(center::SVector{N, T}, points::AbstractVector{SVector{N, T}}, coordinates::Tuple{Int,Int}) where {N, T<:Real}
return new{N, T}(center, points, coordinates)
end

"""convenience function to make Shape constructor more compatible with functions in Repeat module, which use SMatrix{N1,N2} to represent vertices rather than Vector{SVector}"""
function Shape(center::SVector{N, T}, points::SMatrix{N,N2,T}, coordinates::Tuple{Int,Int}) where {N, N2, T<:Real}
return new{N,T}(center, Vector{SVector{N,T}}([points[:,i] for i in 1:N2]),coordinates)
end
end

center(h::Shape) = h._center
points(h::Shape) = h._points
coordinates(h::Shape) = h._coordinates

function get_shapes(basis::Basis{2,T},cells::AbstractMatrix{Int}, radius::T) where{T}
basic_tile = Repeat.tilevertices(basis) * radius
cols = Base.size(cells)[2]
res = Vector{Shape{2, T}}(undef, 0)

get_shapes(type::Type{Any};) = @error "Unknown Type [$type] - available types are :hexagon, :rectangle"

# function get_shapes(type::Symbol; resolution::Tuple{Int,Int}=(2,2), size=1.5)::Vector{Shape{2, Float64}}
# if (type == :hexagon)
# return get_hexagons(resolution, size)
# elseif (type == :rectangle)
# return get_rectangles(resolution, size)
# else
# @error "Unknown Type [$type] - available types are :hexagon, :rectangle"
# return nothing
# end
# end

function get_shapes(::Type{Hexagon}; resolution::Tuple{Int,Int}=(2,2), radius=1.5)::Vector{Shape{2, Float64}}
cells = Repeat.hexcellsinbox(resolution[1],resolution[2])
hexbasis = Repeat.HexBasis1()
basic_tile = Repeat.tilevertices(hexbasis) * radius

res = Vector{Shape{2, Float64}}(undef, 0)
for c in cells
center = SVector(hexbasis[c[1], c[2]]) * radius
points = [(SVector(p...) + center) for p in eachrow(basic_tile)]
for i in 1:cols
cell = cells[:,i]
center = SVector(basis[cell[1], cell[2]]) * radius
points = [(SVector(p) + center) for p in eachcol(basic_tile)]
center = center
push!(res, Shape(center, points, c))
push!(res, Shape(center, points, (cell[1],cell[2])))
end
return res
end

function get_shapes(::Type{Rectangle}; resolution::Tuple{Int,Int}=(2,2), size=1.5)::Vector{Shape{2, Float64}}
if (typeof(size) == Float64)
w = h = size
elseif (typeof(size) == Tuple{Float64, Float64})
w, h = size
else
@error "Unsupported size format [$size] - can be either a Float64 for a square or (Float64, Float64) for a rectangle"
end
get_shapes(type::Type{Any};) = @error "Unknown Type [$type] - available types are Hexagon, Rectangle"

cells = [[c for c in Iterators.product(-resolution[1]:resolution[1], -resolution[2]:resolution[2])]...]
# hexbasis = Repeat.HexBasis1()
# basic_tile = Repeat.tilevertices(hexbasis) * radius
basic_tile = [w h; w -h; -w -h; -w h]
function get_shapes(basis::HexBasis1{2,T};resolution::Tuple{Int,Int}=(2,2), size=1.5) where{T}
println(typeof(Repeat.hexcellsinbox(resolution[1],resolution[2])))
return get_shapes(basis,Repeat.hexcellsinbox(resolution[1],resolution[2]),size)
end

res = Vector{Shape{2, Float64}}(undef, 0)
for c in cells
center = SVector(Float64(c[1])*w*2.0, Float64(c[2])*h*2.0)
points = [(SVector(p...) + center) for p in eachrow(basic_tile)]
center = center
push!(res, Shape(center, points, c))
function get_shapes(basis::RectangularBasis{2,T};resolution::Tuple{Int,Int}=(2,2), size=1.5) where{T}
temp = collect(Iterators.product(-resolution[1]:resolution[1], -resolution[2]:resolution[2]))
cells = reshape(temp,length(temp))
result = Matrix{Int64}(undef,2,length(temp))

for i in 1:length(cells)
result[1,i] = cells[i][1]
result[2,i] = cells[i][2]
end
return res

get_shapes(basis,result,size)
end

# function get_hexagons(resolution::Tuple{Int,Int}=(2,2), radius=1.5)::Vector{Shape{2, Float64}}
# cells = Repeat.hexcellsinbox(resolution[1],resolution[2])
# hexbasis = Repeat.HexBasis1()
# basic_tile = Repeat.tilevertices(hexbasis) * radius

# res = Vector{Shape{2, Float64}}(undef, 0)
# for c in cells
# center = SVector(hexbasis[c[1], c[2]]) * radius
# points = [(SVector(p...) + center) for p in eachrow(basic_tile)]
# center = center
# push!(res, Shape(center, points, c))
# end
# return res
# end

# function get_rectangles(resolution::Tuple{Int,Int}=(2,2), size=1.5)::Vector{Shape{2, Float64}}

# if (typeof(size) == Float64)
# w = h = size
# elseif (typeof(size) == Tuple{Float64, Float64})
# w, h = size
# else
# @error "Unsupported size format [$size] - can be either a Float64 for a square or (Float64, Float64) for a rectangle"
# end

# cells = [[c for c in Iterators.product(-resolution[1]:resolution[1], -resolution[2]:resolution[2])]...]
# # hexbasis = Repeat.HexBasis1()
# # basic_tile = Repeat.tilevertices(hexbasis) * radius
# basic_tile = [w h; w -h; -w -h; -w h]

# res = Vector{Shape{2, Float64}}(undef, 0)
# for c in cells
# center = SVector(Float64(c[1])*w*2.0, Float64(c[2])*h*2.0)
# points = [(SVector(p...) + center) for p in eachrow(basic_tile)]
# center = center
# push!(res, Shape(center, points, c))
# end
# return res
# end


function project(shapes::Vector{Shape{2, T}}, csg::OpticSim.CSGTree{T})::Vector{Shape{3, T}} where {T<:Real}
Expand Down
19 changes: 13 additions & 6 deletions src/Optical/ParaxialAnalysis/HeadEyeModel/Examples.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ using StaticArrays
using Colors
import Makie


emitterarray(emitters, transform = identitytransform()) = Emitters.Sources.CompositeSource(transform, emitters)

function example1()

t = identitytransform()
Expand All @@ -36,11 +39,12 @@ function example1()
# Vis.draw!(eli, transparency=true, color=RGBA(1.0, 0.2, 0.2, 0.4))

csg = HeadEye.csg_sphere(radius=20.0)
csg = HeadEye.csg_cylinder(radius = 20.0, added_rotation = rotationX/2.0))
# csg = HeadEye.csg_cylinder(radius = 20.0, added_rotation = rotationX(π/2.0))
# csg = HeadEye.csg_plane()

# shapes_2d = HeadEye.get_shapes(HeadEye.Hexagon, resolution=(8,5), radius=1.0)
shapes_2d = HeadEye.get_shapes(HeadEye.Rectangle, resolution=(5,5), size=1.0)
# shapes_2d = HeadEye.get_shapes(HexBasis1(), resolution=(8,5), size=1.0)

shapes_2d = HeadEye.get_shapes(RectangularBasis(), resolution=(5,5), size=1.0)

shapes_3d = HeadEye.project(shapes_2d, csg)

Expand Down Expand Up @@ -71,11 +75,11 @@ function example1()
end

mla = LensAssembly(paraxial_lenses...)
display = Emitters.Sources.CompositeSource(identitytransform(), emitters)
display = emitterarray(emitters)

pupil = HeadEye.pupil(HeadEye.eye(h, :left))
pupil_tr = HeadEye.tr(h, :left_pupil)
detector = Circle(HeadEye.size(pupil) / 2.0, forward(pupil_tr), origin(pupil_tr), interface = opaqueinterface())
detector = Circle(HeadEye.pupilsize(pupil) / 2.0, forward(pupil_tr), origin(pupil_tr), interface = opaqueinterface())
sys = CSGOpticalSystem(mla, detector)


Expand All @@ -89,11 +93,14 @@ function example1()
Vis.drawtracerays(sys; raygenerator=display, trackallrays = true, colorbynhits = true, test = true, numdivisions = 100, drawgen = false)
# Vis.draw!(sys)

Vis.draw!(h, draw_head=true)
Vis.draw!(h, draw_head=false)

Vis.draw!(display, debug=false)
end
end
export example1




end # module Test
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ end

tr(p::Pupil) = p._tr
text(p::Pupil) = p._size
size(p::Pupil) = p._size
pupilsize(p::Pupil) = p._size


# --------------------------------------------------------
Expand Down
1 change: 1 addition & 0 deletions src/Optical/ParaxialAnalysis/HeadEyeModel/HeadEyeModel.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ using ..OpticSim
using ..OpticSim.Geometry
using ..OpticSim.Repeat
using ..OpticSim.Emitters
using ..OpticSim.Repeat

using Colors
using StaticArrays, Statistics
Expand Down
Loading

0 comments on commit f3bfebb

Please sign in to comment.