1  #region License Information


2  /* HeuristicLab


3  * Copyright (C) 20022018 Heuristic and Evolutionary Algorithms Laboratory (HEAL)


4  *


5  * This file is part of HeuristicLab.


6  *


7  * HeuristicLab is free software: you can redistribute it and/or modify


8  * it under the terms of the GNU General Public License as published by


9  * the Free Software Foundation, either version 3 of the License, or


10  * (at your option) any later version.


11  *


12  * HeuristicLab is distributed in the hope that it will be useful,


13  * but WITHOUT ANY WARRANTY; without even the implied warranty of


14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the


15  * GNU General Public License for more details.


16  *


17  * You should have received a copy of the GNU General Public License


18  * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.


19  */


20  #endregion


21 


22  using System;


23  using System.Collections.Generic;


24  using System.Linq;


25  using System.Text;


26  using System.Threading.Tasks;


27 


28  namespace HeuristicLab.Problems.BinPacking3D.Geometry {


29  public class Edge3D {


30  public Edge3D() { }


31 


32  public Edge3D(PackingPosition start, PackingPosition end) {


33  Start = new Vector3D(start);


34  End = new Vector3D(end);


35  }


36 


37  public Edge3D(Vector3D start, Vector3D end) {


38  Start = start;


39  End = end;


40  }


41 


42  public Vector3D Start { get; set; }


43  public Vector3D End { get; set; }


44 


45 


46 


47 


48  /// <summary>


49  /// Returns true if a given point lies on the edge.


50  /// </summary>


51  /// <param name="point"></param>


52  /// <returns>Returns true if a given point lies on the edge.</returns>


53  public bool LiesOn(Vector3D point) {


54  if (point == null) {


55  return false;


56  }


57  var x = (Start.X <= point.X && point.X <= End.X)  (Start.X >= point.X && point.X >= End.X);


58  var y = (Start.Y <= point.Y && point.Y <= End.Y)  (Start.Y >= point.Y && point.Y >= End.Y);


59  var z = (Start.Z <= point.Z && point.Z <= End.Z)  (Start.Z >= point.Z && point.Z >= End.Z);


60  return x && y && z;


61  }


62 


63 


64  /// <summary>


65  /// Returns a point where the two edges intersects.


66  /// It returns null if they don't intersect.


67  /// </summary>


68  /// <param name="edge"></param>


69  /// <returns>Returns a point where the two edges intersects. Null = no intersection.</returns>


70  public Vector3D Intersects(Edge3D edge) {


71  return Intersects(this, edge);


72  }


73 


74  /// <summary>


75  /// Returns a point where two edges are intersecting.


76  /// It returns null if they don't intersect.


77  /// </summary>


78  /// <param name="e1"></param>


79  /// <param name="e2"></param>


80  /// <returns>Returns a point where the two edges intersects. Null = no intersection.</returns>


81  public static Vector3D Intersects(Edge3D e1, Edge3D e2) {


82  Line3D l1 = new Line3D(e1.Start, new Vector3D() {


83  X = e1.Start.X  e1.End.X,


84  Y = e1.Start.Y  e1.End.Y,


85  Z = e1.Start.Z  e1.End.Z


86  });


87  Line3D l2 = new Line3D(e2.Start, new Vector3D() {


88  X = e2.Start.X  e2.End.X,


89  Y = e2.Start.Y  e2.End.Y,


90  Z = e2.Start.Z  e2.End.Z


91  });


92  Vector3D point = l1.Intersect(l2);


93  if (point != null && e1.LiesOn(point) && e2.LiesOn(point)) {


94  return point;


95  }


96  return null;


97  }


98  }


99  }

