This repository has been archived by the owner on Oct 23, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 40
Added a new "Surface" type: ConvexPolygon which support planner simple convex polygons #213
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
f73294e
added two new libraries dependencies - one is part of the Julia basic…
8ea6bd2
added some comments and changed the type of the local points to SVect…
2e04866
Removed the GeometricalPredicates from the project. This package was …
12dd2a7
ConvexPolygon is now back to using a Vector{SVector{2,T}} instead of …
d9ded68
removed the normal(surface, x, y) for planner shapes.
4fef38f
Merge branch 'main' into galran/simplePolygon
BrianGun f3597b6
Merge branch 'main' into galran/simplePolygon
BrianGun File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
# MIT license | ||
# Copyright (c) Microsoft Corporation. All rights reserved. | ||
# See LICENSE in the project root for full license information. | ||
|
||
import Statistics | ||
|
||
const _abs_err_orientation_2d = 2*eps(Float64) | ||
|
||
""" | ||
ConvexPolygon{T} <: Surface{T} | ||
|
||
General Convex Polygon surface, not a valid CSG object. | ||
The rotation of the polygon around its normal is defined by `rotationvec`. | ||
`rotationvec×surfacenormal` is taken as the vector along the u axis. | ||
|
||
```julia | ||
ConvexPolygon(local_frame::Transform{T}, local_polygon_points::Vector{SVector{2, T}}, interface::NullOrFresnel{T} = nullinterface(T)) | ||
``` | ||
|
||
The local frame defines the plane (spans by the right and up vectors) with the plane normal given by the forward vector. | ||
the local_polygon_points are given with respect to the local frame and are 2D points. | ||
NOTE: This class uses static vectors to hold the points which will lead to more efficient performance, but should not be used with polygons with more than 20-30 points. | ||
""" | ||
struct ConvexPolygon{T<:Real} <: Surface{T} | ||
plane::Plane{T,3} | ||
local_frame::Transform{T} | ||
local_points::Vector{SVector{2, T}} | ||
|
||
# for efficency | ||
_local_frame_inv::Transform{T} # cache the inverse matrix to avoid computing it for every intersection test | ||
_local_lines::Vector{SVector{3, SVector{2, T}}} # defines the edge points + a third point representing the slopes in order to save some calculationsduring ray checking | ||
_length::Int64 # cache the length of the polygon | ||
|
||
function ConvexPolygon( | ||
local_frame::Transform{T}, | ||
local_polygon_points::Vector{SVector{2, T}}, | ||
interface::NullOrFresnel{T} = NullInterface(T) | ||
) where {T<:Real} | ||
|
||
# need at least 3 points to define apolygon | ||
@assert length(local_polygon_points) > 3 | ||
|
||
local_center = Statistics.mean(local_polygon_points) | ||
world_center = local2world(local_frame) * Vec3(local_center[1], local_center[2], zero(T)) | ||
|
||
# poly_points = [GeometricalPredicates.Point2D(p[1], p[2]) for p in local_polygon_points] | ||
# poly = GeometricalPredicates.Polygon2D(poly_points...) | ||
pts = local_polygon_points | ||
local_lines = SVector( | ||
[SVector( | ||
pts[i], # A | ||
pts[mod(i, length(pts))+1], # B | ||
# bx = Bx - Ax, by = By - Ay | ||
SVector(pts[mod(i, length(pts))+1][1] - pts[i][1], pts[mod(i, length(pts))+1][2] - pts[i][2])) | ||
for i in 1:length(pts)]... | ||
) | ||
|
||
plane = Plane(forward(local_frame), world_center, interface = interface) | ||
new{T}(plane, local_frame, local_polygon_points, inv(local_frame), local_lines, length(local_lines)) | ||
end | ||
end | ||
export ConvexPolygon | ||
|
||
# Base.show(io::IO, poly::ConvexPolygon{T}) where {T<:Real} = print(io, "ConvexPolygon{$T}($(centroid(hex)), $(normal(hex)), $(hex.side_length), $(interface(hex)))") | ||
centroid(poly::ConvexPolygon) = poly.plane.pointonplane | ||
interface(poly::ConvexPolygon) = interface(poly.plane) | ||
normal(poly::ConvexPolygon) = normal(poly.plane) | ||
|
||
function surfaceintersection(poly::ConvexPolygon{T}, r::AbstractRay{T,3}) where {T<:Real} | ||
interval = surfaceintersection(poly.plane, r) | ||
if interval isa EmptyInterval{T} || isinfiniteinterval(interval) | ||
return EmptyInterval(T) # no ray plane intersection or inside plane but no hit | ||
else | ||
intersect = halfspaceintersection(interval) | ||
p = point(intersect) | ||
|
||
local_p = poly._local_frame_inv * p | ||
|
||
@inline function orientation(l::SVector{3, SVector{2, T}})::Int8 where {T<:Real} | ||
cx = local_p[1] - l[1][1] # getx(p) - getx(geta(l)) | ||
cy = local_p[2] - l[1][2] # gety(p) - gety(geta(l)) | ||
_pr2 = -(l[3][1]*cy) + l[3][2]*cx # _pr2 = -l._bx*cy + l._by*cx | ||
if _pr2 < -_abs_err_orientation_2d | ||
1 | ||
elseif _pr2 > _abs_err_orientation_2d | ||
-1 | ||
else | ||
0 # can implement something more accurate in the future to minimize numerical errors | ||
end | ||
end | ||
|
||
@inline function inpolygon() | ||
side = orientation(poly._local_lines[1]) | ||
for i = 2:poly._length | ||
orientation(poly._local_lines[i]) == side || return false | ||
end | ||
return true | ||
end | ||
|
||
if !inpolygon() | ||
return EmptyInterval(T) | ||
else | ||
if dot(normal(poly), direction(r)) < zero(T) | ||
return positivehalfspace(intersect) | ||
else | ||
return rayorigininterval(intersect) | ||
end | ||
end | ||
end | ||
end | ||
|
||
|
||
""" | ||
makemesh(poly::ConvexPolygon{T}, ::Int = 0) where {T<:Real} -> TriangleMesh | ||
|
||
Create a triangle mesh that can be rendered by iterating on the polygon's edges and for each edge use the centroid as the third vertex of the triangle. | ||
""" | ||
function makemesh(poly::ConvexPolygon{T}, ::Int = 0) where {T<:Real} | ||
c = centroid(poly) | ||
|
||
l2w = local2world(poly.local_frame) | ||
len = length(poly.local_points) | ||
|
||
triangles = [] | ||
for i in 1:len | ||
p1 = poly.local_points[i] | ||
p2 = poly.local_points[mod(i,len) + 1] | ||
|
||
tri = Triangle( | ||
Vector(l2w * Vec3(p2[1], p2[2], zero(T))), | ||
Vector(c), | ||
Vector(l2w * Vec3(p1[1], p1[2], zero(T)))) | ||
push!(triangles, tri) | ||
end | ||
|
||
triangles = Vector{Triangle{T}}(triangles) | ||
|
||
return TriangleMesh(triangles) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ | |
# ----------------------------------------------------------------------------------------------- | ||
# GEOMETRY | ||
# ----------------------------------------------------------------------------------------------- | ||
using ...OpticSim | ||
using StaticArrays | ||
using LinearAlgebra | ||
|
||
|
@@ -233,6 +234,40 @@ function Transform(rotation::AbstractArray{T,2}, translation::AbstractArray{T,1} | |
translation[1], translation[2], translation[3], one(T)) | ||
end | ||
|
||
|
||
# define some utility functions | ||
|
||
""" | ||
right(t::Transform{<:Real}) -> Vec3 | ||
|
||
Assuming t is a 3D rigid transform representing a local left-handed coordinate system, this function will return the first column, representing the "X" axis. | ||
""" | ||
right(t::Transform{<:Real}) = normalize(Vec3(t[1,1], t[2,1], t[3,1])) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. documentation strings would be helpful here to explain what these functions do and what they are used for. Graphics people will know but optics people might not. |
||
|
||
""" | ||
up(t::Transform{<:Real}) -> Vec3 | ||
|
||
Assuming t is a 3D rigid transform representing a local left-handed coordinate system, this function will return the second column, representing the "Y" axis. | ||
""" | ||
up(t::Transform{<:Real}) = normalize(Vec3(t[1,2], t[2,2], t[3,2])) | ||
|
||
""" | ||
forward(t::Transform{<:Real}) -> Vec3 | ||
|
||
Assuming t is a 3D rigid transform representing a local left-handed coordinate system, this function will return the third column, representing the "Z" axis. | ||
""" | ||
forward(t::Transform{<:Real}) = normalize(Vec3(t[1,3], t[2,3], t[3,3])) | ||
|
||
""" | ||
origin(t::Transform{<:Real}) -> Vec3 | ||
|
||
Assuming t is a 3D rigid transform representing a local left-handed coordinate system, this function will return the fourth column, containing the translation part of the transform in 3D. | ||
""" | ||
OpticSim.origin(t::Transform{<:Real}) = Vec3(t[1,4], t[2,4], t[3,4]) | ||
|
||
export right, up, forward, origin | ||
|
||
|
||
""" | ||
rotationX(angle::T) where {T<:Real} -> Transform | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is makemesh used for drawing the convex poly in 3D? Maybe a documentation string as to the purpose of this function.