BVB Source Codes

CRYENGINE Show FlyHelpers_PathLocation.h Source code

Return Download CRYENGINE: download FlyHelpers_PathLocation.h Source code - Download CRYENGINE Source code - Type:.h
  1. // Copyright 2001-2016 Crytek GmbH / Crytek Group. All rights reserved.
  2.  
  3. #ifndef __FLY_HELPERS__PATH_LOCATION__H__
  4. #define __FLY_HELPERS__PATH_LOCATION__H__
  5.  
  6. #include "FlyHelpers_Path.h"
  7.  
  8. namespace FlyHelpers
  9. {
  10.  
  11. struct PathLocation
  12. {
  13.         PathLocation()
  14.                 : segmentIndex(0)
  15.                 , normalizedSegmentPosition(0)
  16.         {
  17.         }
  18.  
  19.         bool operator<(const PathLocation& rhs) const
  20.         {
  21.                 return ((segmentIndex < rhs.segmentIndex) || (segmentIndex == rhs.segmentIndex && normalizedSegmentPosition < rhs.normalizedSegmentPosition));
  22.         }
  23.  
  24.         bool operator<=(const PathLocation& rhs) const
  25.         {
  26.                 return ((segmentIndex < rhs.segmentIndex) || (segmentIndex == rhs.segmentIndex && normalizedSegmentPosition <= rhs.normalizedSegmentPosition));
  27.         }
  28.  
  29.         bool operator==(const PathLocation& rhs) const
  30.         {
  31.                 return (segmentIndex == rhs.segmentIndex && normalizedSegmentPosition == rhs.normalizedSegmentPosition);
  32.         }
  33.  
  34.         Vec3 CalculatePathPosition(const Path& path) const
  35.         {
  36.                 const Lineseg segment = path.GetSegment(segmentIndex);
  37.                 return segment.GetPoint(normalizedSegmentPosition);
  38.         }
  39.  
  40.         Vec3 CalculatePathPositionCatmullRomLooping(const Path& path) const
  41.         {
  42.                 const size_t segmentCount = path.GetSegmentCount();
  43.                 const size_t i0 = (segmentCount + segmentIndex - 1) % segmentCount;
  44.                 const size_t i1 = segmentIndex;
  45.                 const size_t i2 = segmentIndex + 1;
  46.                 const size_t i3 = (segmentIndex + 2) % segmentCount;
  47.  
  48.                 return CalculatePathPositionCatmullRomImpl(path, i0, i1, i2, i3);
  49.         }
  50.  
  51.         Vec3 CalculatePathPositionCatmullRom(const Path& path) const
  52.         {
  53.                 const size_t segmentCount = path.GetSegmentCount();
  54.                 const size_t i0 = (0 < segmentIndex) ? segmentIndex - 1 : 0;
  55.                 const size_t i1 = segmentIndex;
  56.                 const size_t i2 = segmentIndex + 1;
  57.                 const size_t i3 = min(segmentIndex + 2, segmentCount);
  58.  
  59.                 return CalculatePathPositionCatmullRomImpl(path, i0, i1, i2, i3);
  60.         }
  61.  
  62.         float CalculateDistanceToPreviousPathPoint(const Path& path) const
  63.         {
  64.                 return (path.GetSegmentLength(segmentIndex) * normalizedSegmentPosition);
  65.         }
  66.  
  67.         float CalculateDistanceToNextPathPoint(const Path& path) const
  68.         {
  69.                 return (path.GetSegmentLength(segmentIndex) * (1.0f - normalizedSegmentPosition));
  70.         }
  71.  
  72.         float CalculateDistanceAlongPathTo(const Path& path, const PathLocation& other) const
  73.         {
  74.                 if (other < *this)
  75.                 {
  76.                         return other.CalculateDistanceAlongPathTo(path, *this);
  77.                 }
  78.                 else
  79.                 {
  80.                         if (segmentIndex == other.segmentIndex)
  81.                         {
  82.                                 return path.GetSegmentLength(segmentIndex) * (other.normalizedSegmentPosition - normalizedSegmentPosition);
  83.                         }
  84.                         else
  85.                         {
  86.                                 float distance = CalculateDistanceToNextPathPoint(path);
  87.                                 for (size_t i = segmentIndex + 1; i < other.segmentIndex; ++i)
  88.                                 {
  89.                                         distance += path.GetSegmentLength(i);
  90.                                 }
  91.                                 distance += other.CalculateDistanceToPreviousPathPoint(path);
  92.                                 return distance;
  93.                         }
  94.                 }
  95.         }
  96.  
  97.         float CalculateDistanceAlongPathInTheCurrentDirectionTo(const Path& path, const PathLocation& other) const
  98.         {
  99.                 const float distance = CalculateDistanceAlongPathTo(path, other);
  100.                 if (*this <= other)
  101.                 {
  102.                         return distance;
  103.                 }
  104.                 else
  105.                 {
  106.                         const float totalDistance = path.GetTotalPathDistance();
  107.                         return totalDistance - distance;
  108.                 }
  109.         }
  110.  
  111.         size_t segmentIndex;
  112.         float  normalizedSegmentPosition;
  113.  
  114. private:
  115.         Vec3 CalculatePathPositionCatmullRomImpl(const Path& path, const size_t i0, const size_t i1, const size_t i2, const size_t i3) const
  116.         {
  117.                 const float x = normalizedSegmentPosition;
  118.                 const float xx = x * x;
  119.                 const float xxx = xx * x;
  120.  
  121.                 const Vec3& p0 = path.GetPoint(i0);
  122.                 const Vec3& p1 = path.GetPoint(i1);
  123.                 const Vec3& p2 = path.GetPoint(i2);
  124.                 const Vec3& p3 = path.GetPoint(i3);
  125.  
  126.                 return 0.5f * ((2 * p1) + (-p0 + p2) * x + (2 * p0 - 5 * p1 + 4 * p2 - p3) * xx + (-p0 + 3 * p1 - 3 * p2 + p3) * xxx);
  127.         }
  128. };
  129.  
  130. }
  131.  
  132. #endif
  133.  
downloadFlyHelpers_PathLocation.h Source code - Download CRYENGINE Source code
Related Source Codes/Software:
postal - 2017-06-11
reactide - Reactide is the first dedicated IDE for React web ... 2017-06-11
rkt - rkt is a pod-native container engine for Linux. It... 2017-06-11
uWebSockets - Tiny WebSockets https://for... 2017-06-11
realworld - TodoMVC for the RealWorld - Exemplary fullstack Me... 2017-06-11
CRYENGINE - CRYENGINE is a powerful real-time game development... 2017-06-11
goreplay - GoReplay is an open-source tool for capturing and ... 2017-06-10
pyenv - Simple Python version management 2017-06-10
redux-saga - An alternative side effect model for Redux apps ... 2017-06-10
angular-starter - 2017-06-10

 Back to top