00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00040 #include "omicron/internal.h"
00041 #include "omicron/path.h"
00042
00043 path_c::path_c()
00044 {
00045 nodes = new list_c<node_s>;
00046 lengthfactor = 1;
00047 }
00048
00049 path_c::~path_c()
00050 {
00051 AssertThis;
00052
00053 if (nodes)
00054 {
00055 node_s *n;
00056
00057 n = (node_s*)nodes->get_first();
00058 while(n)
00059 {
00060 SafeDelete(n);
00061 n = (node_s*)nodes->get_next();
00062 }
00063 }
00064 SafeDelete(nodes);
00065 }
00066
00067 void path_c::add_node(float t, vec3_t v)
00068 {
00069 AssertThis;
00070
00071 node_s *n = new node_s, *cn;
00072
00073 if (!n)
00074 return;
00075
00076 n->frac = t;
00077 n->pos = v;
00078
00079 cn = nodes->get_first();
00080
00081 if (!cn)
00082 {
00083 nodes->append(n);
00084 return;
00085 }
00086
00087 while(cn && cn->frac<t)
00088 cn = nodes->get_next();
00089
00090 if (!cn)
00091 cn = nodes->get_last();
00092
00093 nodes->add_aftercurrent(n);
00094 }
00095
00096 slong path_c::get_node_num(float t)
00097 {
00098 AssertThisV;
00099
00100 ulong i = 0;
00101
00102 node_s *cn;
00103
00104 cn = nodes->get_first();
00105
00106 while(cn)
00107 {
00108 if (cn->frac>t)
00109 return i-1;
00110
00111 i++;
00112 cn = nodes->get_next();
00113 }
00114
00115 return i-1;
00116 }
00117
00118 path_c::node_s *path_c::get_node(slong num)
00119 {
00120 AssertThisV;
00121
00122
00123 if (!closed)
00124 {
00125 if (num<0)
00126 num = 0;
00127 if (num>nodes->get_count())
00128 num = nodes->get_count();
00129 }
00130 else
00131 {
00132 while(num<0)
00133 num += nodes->get_count();
00134
00135 num %= nodes->get_count();
00136 }
00137
00138 nodes->set_current_num(num);
00139 return (node_s*)nodes->get_current();
00140 }
00141
00142 vec3_c path_c::get_pos(float tm)
00143 {
00144 AssertThisValue(vec3_c());
00145
00146 tm *= lengthfactor;
00147
00148 node_s *pn, *pn2;
00149 float tm1, tm2, frac;
00150 vec3_c pos0, pos1, pos2, pos3, tan1, tan2, vtmp;
00151 ulong num = get_node_num(tm);
00152
00153 pn = get_node(num);
00154 pn2 = get_node(num+1);
00155
00156 tm1 = pn->frac;
00157 tm2 = pn2->frac;
00158
00159 while(tm2<tm1)
00160 tm2++;
00161
00162 frac = (tm-tm1)/(tm2-tm1);
00163
00164 pos0 = get_node(num-1)->pos;
00165 pos1 = pn->pos;
00166 pos2 = pn2->pos;
00167 pos3 = get_node(num+2)->pos;
00168
00169 vtmp = pos1 - pos0;
00170 tan1 = pos2 - pos1 + vtmp;
00171
00172 vtmp = pos2 - pos1;
00173 tan2 = pos3 - pos2 + vtmp;
00174
00175 return vec3_c::hermite(pos1, tan1, pos2, tan2, frac);
00176 }
00177