1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Linq;
|
---|
4 | using System.Text;
|
---|
5 | using System.Threading.Tasks;
|
---|
6 |
|
---|
7 | namespace HeuristicLab.Problems.BinPacking3D.Geometry {
|
---|
8 | /// <summary>
|
---|
9 | /// A line is given as a point and a directing vector
|
---|
10 | /// </summary>
|
---|
11 | public class Line3D {
|
---|
12 | public Vector3D Point;
|
---|
13 | public Vector3D Direction;
|
---|
14 |
|
---|
15 | public Line3D(Vector3D point, Vector3D direction) {
|
---|
16 | Point = point;
|
---|
17 | Direction = direction;
|
---|
18 | }
|
---|
19 | public Line3D(PackingPosition position, Vector3D direction) {
|
---|
20 | Point = new Vector3D(position);
|
---|
21 | Direction = direction;
|
---|
22 | }
|
---|
23 |
|
---|
24 | public bool Intersects(Plane3D plane) {
|
---|
25 | return plane.Intersects(this);
|
---|
26 | }
|
---|
27 |
|
---|
28 | public Vector3D Intersect(Plane3D plane) {
|
---|
29 | return plane.Intersect(this);
|
---|
30 | }
|
---|
31 |
|
---|
32 | /// <summary>
|
---|
33 | /// Returns the intersection point of two lines.
|
---|
34 | /// It the lines doesn't intersect it returns null.
|
---|
35 | /// </summary>
|
---|
36 | /// <param name="line"></param>
|
---|
37 | /// <returns></returns>
|
---|
38 | public Vector3D Intersect(Line3D line) {
|
---|
39 | double r = 0;
|
---|
40 | double s = 0;
|
---|
41 |
|
---|
42 | // if they have the same source point, this point can be returned.
|
---|
43 | if (this.Point.Equals(line.Point)) {
|
---|
44 | return this.Point;
|
---|
45 | }
|
---|
46 |
|
---|
47 | if (Direction.X != 0) {
|
---|
48 | r = (line.Point.X - this.Point.X) / (double)Direction.X;
|
---|
49 | } else if (Direction.Y != 0) {
|
---|
50 | r = (line.Point.Y - this.Point.Y) / (double)Direction.Y;
|
---|
51 | } else if (Direction.Z != 0) {
|
---|
52 | r = (line.Point.Z - this.Point.Z) / (double)Direction.Z;
|
---|
53 | }
|
---|
54 |
|
---|
55 | if (line.Direction.X != 0) {
|
---|
56 | s = (this.Point.X - line.Point.X) / (double)line.Direction.X;
|
---|
57 | } else if (line.Direction.Y != 0) {
|
---|
58 | s = (this.Point.Y - line.Point.Y) / (double)line.Direction.Y;
|
---|
59 | } else if (line.Direction.Z != 0) {
|
---|
60 | s = (this.Point.Z - line.Point.Z) / (double)line.Direction.Z;
|
---|
61 | }
|
---|
62 | var p1 = r * this.Direction + this.Point;
|
---|
63 | var p2 = s * line.Direction + line.Point;
|
---|
64 | var c = p1.Equals(p2);
|
---|
65 | if (s!=0 && r!=0 && p1.Equals(p2)) {
|
---|
66 | return p1;
|
---|
67 | }
|
---|
68 |
|
---|
69 | return null;
|
---|
70 | }
|
---|
71 | }
|
---|
72 | }
|
---|