Changeset 82:e7f50f998458 in rrlib_geometry


Ignore:
Timestamp:
20.02.2017 16:35:14 (3 years ago)
Author:
Max Reichardt <max.reichardt@…>
Branch:
14.08
Phase:
public
Message:

Adds intersection method for lines with bounding boxes

Files:
4 edited

Legend:

Unmodified
Added
Removed
  • tLine.h

    r74 r82  
    7171}; 
    7272 
     73template <size_t Tdimension, typename TElement> 
     74class tLineSegment; 
     75 
    7376//---------------------------------------------------------------------- 
    7477// Class declaration 
     
    114117 
    115118  tIntersectionType GetIntersection(typename tShape::tPoint &intersection_point, const tLine &line) const; 
     119 
     120  /*! 
     121   * Calculates intersection of this line with bounding box. 
     122   * 
     123   * \param bounding_box Bounding box for intersection 
     124   * \return 'first' contains whether line intersects bounding box; 'second' the intersection (has the same direction as this line) 
     125   */ 
     126  virtual std::pair<bool, tLineSegment<Tdimension, TElement>> GetIntersection(typename tShape::tBoundingBox &bounding_box) const; 
    116127 
    117128  virtual tLine &Translate(const math::tVector<Tdimension, TElement> &translation); 
  • tLine.hpp

    r74 r82  
    215215 
    216216//---------------------------------------------------------------------- 
     217// tLine GetIntersection 
     218//---------------------------------------------------------------------- 
     219template <size_t Tdimension, typename TElement> 
     220std::pair<bool, tLineSegment<Tdimension, TElement>> tLine<Tdimension, TElement>::GetIntersection(typename tShape::tBoundingBox &bounding_box) const 
     221{ 
     222  // Bounding box is centered in zero 
     223  typedef typename tShape::tPoint tPoint; 
     224  auto box_center = (bounding_box.Max() + bounding_box.Min()) * 0.5; 
     225  tPoint begin_relative_to_box(Support() - box_center); 
     226 
     227  // Determine largest direction component (numerically most stable) 
     228  size_t max_direction_component = 0; 
     229  double max_direction_component_value = 0; 
     230  for (size_t i = 0; i < Tdimension; i++) 
     231  { 
     232    if (std::fabs(Direction()[i]) > max_direction_component_value) 
     233    { 
     234      max_direction_component = i; 
     235      max_direction_component_value = std::fabs(Direction()[i]); 
     236    } 
     237  } 
     238 
     239  // Calculate relevant line segment and intersect it 
     240  double to_box_center_factor = -(begin_relative_to_box[max_direction_component] / Direction()[max_direction_component]); 
     241  double box_dimension_range_factor = std::fabs((bounding_box.Max()[max_direction_component] - bounding_box.Min()[max_direction_component]) / Direction()[max_direction_component]); 
     242  tLineSegment<Tdimension, TElement> relevant_segment(tPoint(Support() + Direction() * (to_box_center_factor - box_dimension_range_factor)), 
     243      tPoint(Support() + Direction() * (to_box_center_factor + box_dimension_range_factor))); 
     244  return relevant_segment.GetIntersection(bounding_box); 
     245} 
     246 
     247//---------------------------------------------------------------------- 
    217248// tLine Translate 
    218249//---------------------------------------------------------------------- 
  • tLineSegment.h

    r81 r82  
    111111  } 
    112112 
    113   virtual const typename tShape::tPoint GetClosestPoint(const typename tShape::tPoint &reference_point) const; 
     113  virtual const typename tShape::tPoint GetClosestPoint(const typename tShape::tPoint &reference_point) const override; 
    114114 
    115   virtual tLineSegment &Translate(const math::tVector<Tdimension, TElement> &translation); 
    116   virtual tLineSegment &Rotate(const math::tMatrix<Tdimension, Tdimension, TElement> &rotation); 
    117   virtual tLineSegment &Transform(const math::tMatrix < Tdimension + 1, Tdimension + 1, TElement > &transformation); 
     115  virtual tLineSegment &Translate(const math::tVector<Tdimension, TElement> &translation) override; 
     116  virtual tLineSegment &Rotate(const math::tMatrix<Tdimension, Tdimension, TElement> &rotation) override; 
     117  virtual tLineSegment &Transform(const math::tMatrix < Tdimension + 1, Tdimension + 1, TElement > &transformation) override; 
    118118 
    119119  /*! 
     
    121121   * 
    122122   * \param bounding_box Bounding box for intersection 
    123    * \return 'first' contains whether line intersects bounding box; 'second' the intersection (part of line_segment; has the same direction as this line_segment) 
     123   * \return 'first' contains whether line intersects bounding box; 'second' the intersection (has the same direction as this line segment) 
    124124   */ 
    125   std::pair<bool, tLineSegment> GetIntersection(typename tShape::tBoundingBox &bounding_box) const; 
     125  virtual std::pair<bool, tLineSegment> GetIntersection(typename tShape::tBoundingBox &bounding_box) const override; 
    126126  using tLine::GetIntersection; 
    127127 
     
    133133  typename tShape::tPoint end; 
    134134 
    135   virtual void UpdateBoundingBox(typename tShape::tBoundingBox &bounding_box) const; 
    136   virtual void UpdateCenterOfGravity(typename tShape::tPoint &center_of_gravity) const; 
     135  virtual void UpdateBoundingBox(typename tShape::tBoundingBox &bounding_box) const override; 
     136  virtual void UpdateCenterOfGravity(typename tShape::tPoint &center_of_gravity) const override; 
    137137 
    138138}; 
  • tests/geometry.cpp

    r81 r82  
    138138      AssertLineSegmentsEqual(tLineSegment(tPoint(-1 + x, 4, 5), tPoint(2 + x, 4, 2)).GetIntersection(box).second, tLineSegment(tPoint(0 + x, 4, 4), tPoint(1 + x, 4, 3))); 
    139139      AssertLineSegmentsEqual(tLineSegment(tPoint(0 + x, 4, 4), tPoint(12 + x, 5, 8)).GetIntersection(box).second, tLineSegment(tPoint(0 + x, 4, 4), tPoint(0 + x, 4, 4))); 
     140 
     141      // Check line segments 
     142      AssertLineSegmentsEqual(tLine<3, double>(tPoint(-111 + x, 4, 115), rrlib::math::tVec3d(-1, 0, 1)).GetIntersection(box).second, tLineSegment(tPoint(1 + x, 4, 3), tPoint(0 + x, 4, 4))); 
    140143    } 
    141144  } 
Note: See TracChangeset for help on using the changeset viewer.