Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Supports #14

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
language: go

go:
- "1.10"
- "1.14"
- tip

install:
Expand All @@ -10,4 +10,4 @@ install:
- go get github.com/stretchr/testify/require
- go get github.com/fogleman/simplify
- go get gopkg.in/alecthomas/kingpin.v2
- go get gopkg.in/yaml.v2
- go get gopkg.in/yaml.v2
35 changes: 23 additions & 12 deletions helpers/supports_by_vector.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
func MakeUndoubledLinesFromTriangles(col_triangles []Triangle) []Line {

var lines []Line
//make lines from triangles
for _, t := range col_triangles {
lines = append(lines, Line{P1: t.P1, P2:t.P2})
lines = append(lines, Line{P1: t.P1, P2:t.P3})
Expand All @@ -15,6 +16,7 @@ func MakeUndoubledLinesFromTriangles(col_triangles []Triangle) []Line {

var new_lines []Line
var lines2 []Line
//make undoubled lines
for i, l1 := range lines{
fl := false
lines2 = []Line{}
Expand All @@ -35,22 +37,31 @@ func MakeUndoubledLinesFromTriangles(col_triangles []Triangle) []Line {
}


func IntersectTriangles(t Triangle, triangles []Triangle) []Line {
func MakeInternalSupportTriangles(top_point1, top_point2 Point, lines []Line) []Triangle {

var lines []Line
for _, bt := range triangles {
if !(bt.PointBelongs(t.P1) || bt.PointBelongs(t.P2) || bt.PointBelongs(t.P3)) {
line := bt.IntersectTriangle(&t)
if line != nil {
lines = append(lines, *line)
var triangles []Triangle
fake_point := Point{X: -1, Y: -1, Z: -1}
for _, l := range lines {
Tr := NewTriangle(l.P1, l.P2, top_point1)
fl := 0
for _, l2 := range lines {
if l2.IntersectTriangle(Tr){
if !l.P1.Equal(l2.P1) && !l.P1.Equal(l2.P2) && !l.P2.Equal(l2.P1) && !l.P2.Equal(l2.P2){
fl = 1
break
}
}
}
if fl == 0 {
triangles = append(triangles, Tr)
if top_point2 != fake_point {
Tr1 := NewTriangle(l.P1, top_point1, top_point2)
Tr2 := NewTriangle(l.P2, top_point1, top_point2)
triangles = append(triangles, Tr1, Tr2)
}
}
}
if len(lines) != 0 {
return lines
}

return nil
return triangles
}


Expand Down
91 changes: 67 additions & 24 deletions primitives/line.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,25 +45,64 @@ func (l Line) IsIntersectingSegment(l1 *Line) bool {

func (l Line) IntersectLine(l1 *Line) *Point {

orientation1 := l1.P1.Orientation(l.P1, l.P2)
orientation2 := l1.P2.Orientation(l.P1, l.P2)
orientation3 := l.P1.Orientation(l1.P1, l1.P2)
orientation4 := l.P2.Orientation(l1.P1, l1.P2)
normal := make([]Vector, 4)
orientation := make([]int, 4)
var base_normal Vector

normal[0] = l1.P1.VectorTo(l.P1).Cross(l1.P1.VectorTo(l.P2))
normal[1] = l1.P2.VectorTo(l.P1).Cross(l1.P2.VectorTo(l.P2))
normal[2] = l.P1.VectorTo(l1.P1).Cross(l.P1.VectorTo(l1.P2))
normal[3] = l.P2.VectorTo(l1.P1).Cross(l.P2.VectorTo(l1.P2))

for i, n := range normal {
if AlmostZero(n.X)&&AlmostZero(n.Y)&&AlmostZero(n.Z) {
orientation[i] = 0
normal[i] = V(0,0,0)
} else {
base_normal = n
}
}

for i, n := range normal {
if base_normal.Dot(n) > 0 {
orientation[i] = 1
}
if base_normal.Dot(n) < 0 {
orientation[i] = 2
}
}

// Collinear case
if (orientation[0] == 0 && l.IsCollinearPointOnSegment(l1.P1)) {
return &l1.P1 }

if (orientation[1] == 0 && l.IsCollinearPointOnSegment(l1.P2)) {
return &l1.P2 }

if (orientation[2] == 0 && l1.IsCollinearPointOnSegment(l.P1)) {
return &l.P1 }

if (orientation[3] == 0 && l1.IsCollinearPointOnSegment(l.P2)) {
return &l.P2 }

// General case
if orientation1 != orientation2 && orientation3 != orientation4 {
if orientation[0] != orientation[1] && orientation[2] != orientation[3] {

a := l.ToVector()
b := l1.ToVector()

var m float64
if a.X != 0 {
m = (l1.P1.Y - l.P1.Y - (a.Y/a.X)*(l1.P1.X - l.P1.X)) / (b.X*a.Y/a.X - b.Y)
} else if a.Y != 0 {
m = (l1.P1.Y - l.P1.Y - (a.Y/a.X)*(l1.P1.X - l.P1.X)) / (b.X*a.Y/a.X - b.Y)

if math.IsNaN(m) {
m = (l1.P1.X - l.P1.X - (a.X/a.Y)*(l1.P1.Y - l.P1.Y)) / (a.X*b.Y/a.Y - b.X)
} else if a.Z != 0 {
}
if math.IsNaN(m) {
m = (l1.P1.X - l.P1.X - (a.X/a.Z)*(l1.P1.Z - l.P1.Z)) / (a.X*b.Z/a.Z - b.X)
}
if math.IsNaN(m) {
m = (l1.P1.Y - l.P1.Y - (a.Y/a.Z)*(l1.P1.Z - l.P1.Z)) / (a.Y*b.Z/a.Z - b.Y)
}

x := l1.P1.X + b.X*m
y := l1.P1.Y + b.Y*m
Expand All @@ -72,23 +111,27 @@ func (l Line) IntersectLine(l1 *Line) *Point {
return &Point{x, y, z}
}

// Collinear case
if (orientation1 == 0 && l.IsCollinearPointOnSegment(l1.P1)) {
println("l1.P1 ", l1.P1.Z)
return &l1.P1 }

if (orientation2 == 0 && l.IsCollinearPointOnSegment(l1.P2)) {
println("l1.P2 ", l1.P2.Z)
return &l1.P2 }
return nil; // Doesn't fall in any of the above cases

if (orientation3 == 0 && l1.IsCollinearPointOnSegment(l.P1)) {
println("l.P1 ", l.P1.Z)
return &l.P1 }
}

if (orientation4 == 0 && l1.IsCollinearPointOnSegment(l.P2)) {
println("l2.P2 ", l.P2.Z)
return &l.P2 }
func (l Line) IntersectTriangle(t Triangle) bool {

return nil; // Doesn't fall in any of the above cases
p1 := Line{P1: t.P1, P2: t.P2}.IntersectLine(&l)
p2 := Line{P1: t.P1, P2: t.P3}.IntersectLine(&l)
p3 := Line{P1: t.P2, P2: t.P3}.IntersectLine(&l)

}
if p1 != nil || p2 != nil || p3 != nil {
return true
}

/* if p1 == nil && p2 == nil && p3 == nil {
if t.PointBelongs(l.P1) { //or l.P2 - no difference
return true
}
}*/

return false

}
89 changes: 89 additions & 0 deletions primitives/test/line_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,93 @@ func TestLine_IsIntersectingSegment(t *testing.T) {
require.Equal(t, row.out, row.in1.IsIntersectingSegment(&row.in2))
})
}
}

func TestLine_IntersectLine(t *testing.T) {

cases := []struct {
in1 Line
in2 Line
out *Point
}{
// Lines intersect on XY plane
{
in1: Line{Point{3, 2, 0}, Point{8, 7, 0}},
in2: Line{Point{1, 6, 0}, Point{7, 3, 0}},
out: &Point{5, 4, 0},
},
// Lines intersect on YZ plane
{
in1: Line{Point{0, 3, 2}, Point{0, 8, 7}},
in2: Line{Point{0, 1, 6}, Point{0, 7, 3}},
out: &Point{0, 5, 4},
},

// Lines intersect on XZ plane
{
in1: Line{Point{3, 0, 2}, Point{8, 0, 7}},
in2: Line{Point{1, 0, 6}, Point{7, 0, 3}},
out: &Point{5, 0, 4},
},
// Lines intersect
{
in1: Line{Point{1, 1, -2}, Point{1, -5, 1}},
in2: Line{Point{1, -3, 0}, Point{-1, 0, -4}},
out: &Point{1, -3, 0},
},
// 1, 2, 3 points collinear, 3 lies b/w 1,2
{
in1: Line{Point{1, -3, 0}, Point{-1, 0, -4}},
in2: Line{Point{0, -1.5, -2}, Point{0, 0, 0}},
out: &Point{0, -1.5, -2},
},
// 1, 2, 3 points collinear, 3 lies b/w 1,2
{
in1: Line{Point{1, -3, 0}, Point{-3, 3, -8}},
in2: Line{Point{-1, 0, -4}, Point{0, 0, 0}},
out: &Point{-1, 0, -4},
},
// not intersect
{
in1: Line{Point{1, -3, 0}, Point{-3, 3, -8}},
in2: Line{Point{1, 0, 4}, Point{0, 0, 0}},
out: nil,
},

}

for i, row := range cases {
t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
require.Equal(t, row.out, row.in1.IntersectLine(&row.in2))
})
}
}

func TestLine_IntersectTriangle(t *testing.T) {

cases := []struct {
in1 Line
in2 Triangle
out bool
}{
//Line intersect one side of triangle
{
in1: Line{Point{1, 1, -2}, Point{1, -5, 1}},
in2: NewTriangle(Point{1, -3, 0}, Point{-1, 0, -4}, Point{0, 0, 0}),
out: true,
},
//Not intersect
{
in1: Line{Point{10, 10, 10}, Point{9, 9, 9}},
in2: NewTriangle(Point{1, -3, 0}, Point{-1, 0, -4}, Point{0, 0, 0}),
out: false,
},

}

for i, row := range cases {
t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
require.Equal(t, row.out, row.in1.IntersectTriangle(row.in2))
})
}
}
55 changes: 55 additions & 0 deletions primitives/test/plane_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,3 +104,58 @@ func TestPlane_IntersectTriangle(t *testing.T) {
})
}
}

func TestPlane_ProectionPointToPlane(t *testing.T) {
cases := []struct {
in1 Plane
in2 Point
out Point
}{
{
in1: Plane{Point{1, 1, 1}, V(1, 1, 1)},
in2: Point{0, 0, 0},
out: Point{1, 1, 1},
},
{
in1: Plane{Point{1, 1, 1}, V(0, 0, 1)},
in2: Point{0, 0, 0},
out: Point{0, 0, 1},
},
{
in1: Plane{Point{1, 1, 1}, V(0, 1, 0)},
in2: Point{0, 0, 0},
out: Point{0, 1, 0},
},
}
for i, row := range cases {
t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
res := row.in1.ProectionPointToPlane(row.in2)
require.Equal(t, row.out, res)
})
}
}

func TestPlane_PointBelongs(t *testing.T) {
cases := []struct {
in1 Plane
in2 Point
out bool
}{
{
in1: Plane{Point{1, 1, 1}, V(1, 1, 1)},
in2: Point{1, 1, 1},
out: true,
},
{
in1: Plane{Point{1, 1, 1}, V(1, 1, 1)},
in2: Point{0, 0, 0},
out: false,
},
}
for i, row := range cases {
t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
res := row.in1.PointBelongs(row.in2)
require.Equal(t, row.out, res)
})
}
}
Loading