Planeshift
|
00001 // 00002 // Copyright (c) 2009-2010 Mikko Mononen [email protected] 00003 // 00004 // This software is provided 'as-is', without any express or implied 00005 // warranty. In no event will the authors be held liable for any damages 00006 // arising from the use of this software. 00007 // Permission is granted to anyone to use this software for any purpose, 00008 // including commercial applications, and to alter it and redistribute it 00009 // freely, subject to the following restrictions: 00010 // 1. The origin of this software must not be misrepresented; you must not 00011 // claim that you wrote the original software. If you use this software 00012 // in a product, an acknowledgment in the product documentation would be 00013 // appreciated but is not required. 00014 // 2. Altered source versions must be plainly marked as such, and must not be 00015 // misrepresented as being the original software. 00016 // 3. This notice may not be removed or altered from any source distribution. 00017 // 00018 00019 #ifndef DETOUROBSTACLEAVOIDANCE_H 00020 #define DETOUROBSTACLEAVOIDANCE_H 00021 00022 struct dtObstacleCircle 00023 { 00024 float p[3]; 00025 float vel[3]; 00026 float dvel[3]; 00027 float rad; 00028 float dp[3], np[3]; 00029 }; 00030 00031 struct dtObstacleSegment 00032 { 00033 float p[3], q[3]; 00034 bool touch; 00035 }; 00036 00037 00038 class dtObstacleAvoidanceDebugData 00039 { 00040 public: 00041 dtObstacleAvoidanceDebugData(); 00042 ~dtObstacleAvoidanceDebugData(); 00043 00044 bool init(const int maxSamples); 00045 void reset(); 00046 void addSample(const float* vel, const float ssize, const float pen, 00047 const float vpen, const float vcpen, const float spen, const float tpen); 00048 00049 void normalizeSamples(); 00050 00051 inline int getSampleCount() const { return m_nsamples; } 00052 inline const float* getSampleVelocity(const int i) const { return &m_vel[i*3]; } 00053 inline float getSampleSize(const int i) const { return m_ssize[i]; } 00054 inline float getSamplePenalty(const int i) const { return m_pen[i]; } 00055 inline float getSampleDesiredVelocityPenalty(const int i) const { return m_vpen[i]; } 00056 inline float getSampleCurrentVelocityPenalty(const int i) const { return m_vcpen[i]; } 00057 inline float getSamplePreferredSidePenalty(const int i) const { return m_spen[i]; } 00058 inline float getSampleCollisionTimePenalty(const int i) const { return m_tpen[i]; } 00059 00060 private: 00061 int m_nsamples; 00062 int m_maxSamples; 00063 float* m_vel; 00064 float* m_ssize; 00065 float* m_pen; 00066 float* m_vpen; 00067 float* m_vcpen; 00068 float* m_spen; 00069 float* m_tpen; 00070 }; 00071 00072 dtObstacleAvoidanceDebugData* dtAllocObstacleAvoidanceDebugData(); 00073 void dtFreeObstacleAvoidanceDebugData(dtObstacleAvoidanceDebugData* ptr); 00074 00075 00076 static const int DT_MAX_PATTERN_DIVS = 32; 00077 static const int DT_MAX_PATTERN_RINGS = 4; 00078 00079 struct dtObstacleAvoidanceParams 00080 { 00081 float velBias; 00082 float weightDesVel; 00083 float weightCurVel; 00084 float weightSide; 00085 float weightToi; 00086 float horizTime; 00087 unsigned char gridSize; 00088 unsigned char adaptiveDivs; 00089 unsigned char adaptiveRings; 00090 unsigned char adaptiveDepth; 00091 }; 00092 00093 class dtObstacleAvoidanceQuery 00094 { 00095 public: 00096 dtObstacleAvoidanceQuery(); 00097 ~dtObstacleAvoidanceQuery(); 00098 00099 bool init(const int maxCircles, const int maxSegments); 00100 00101 void reset(); 00102 00103 void addCircle(const float* pos, const float rad, 00104 const float* vel, const float* dvel); 00105 00106 void addSegment(const float* p, const float* q); 00107 00108 int sampleVelocityGrid(const float* pos, const float rad, const float vmax, 00109 const float* vel, const float* dvel, float* nvel, 00110 const dtObstacleAvoidanceParams* params, 00111 dtObstacleAvoidanceDebugData* debug = 0); 00112 00113 int sampleVelocityAdaptive(const float* pos, const float rad, const float vmax, 00114 const float* vel, const float* dvel, float* nvel, 00115 const dtObstacleAvoidanceParams* params, 00116 dtObstacleAvoidanceDebugData* debug = 0); 00117 00118 inline int getObstacleCircleCount() const { return m_ncircles; } 00119 const dtObstacleCircle* getObstacleCircle(const int i) { return &m_circles[i]; } 00120 00121 inline int getObstacleSegmentCount() const { return m_nsegments; } 00122 const dtObstacleSegment* getObstacleSegment(const int i) { return &m_segments[i]; } 00123 00124 private: 00125 00126 void prepare(const float* pos, const float* dvel); 00127 00128 float processSample(const float* vcand, const float cs, 00129 const float* pos, const float rad, 00130 const float* vel, const float* dvel, 00131 dtObstacleAvoidanceDebugData* debug); 00132 00133 dtObstacleCircle* insertCircle(const float dist); 00134 dtObstacleSegment* insertSegment(const float dist); 00135 00136 dtObstacleAvoidanceParams m_params; 00137 float m_invHorizTime; 00138 float m_vmax; 00139 float m_invVmax; 00140 00141 int m_maxCircles; 00142 dtObstacleCircle* m_circles; 00143 int m_ncircles; 00144 00145 int m_maxSegments; 00146 dtObstacleSegment* m_segments; 00147 int m_nsegments; 00148 }; 00149 00150 dtObstacleAvoidanceQuery* dtAllocObstacleAvoidanceQuery(); 00151 void dtFreeObstacleAvoidanceQuery(dtObstacleAvoidanceQuery* ptr); 00152 00153 00154 #endif // DETOUROBSTACLEAVOIDANCE_H