Main Page   Class Hierarchy   Alphabetical List   Data Structures   File List   Data Fields   Globals   Related Pages  

x_path.cpp

Go to the documentation of this file.
00001 // OMICRON ENGINE HEADER FILE
00002 //
00003 // --------------------------------------------------------------------------
00004 // Copyright (C) 2001-2002 by Bjoern Paetzel <kolrabi@gmx.de>
00005 //
00006 // This file is part of the Omicron Engine.
00007 //
00008 // The Omicron Engine is free software; you can redistribute it and/or modify
00009 // it under the terms of the  GNU General Public License  as published by the
00010 // Free Software Foundation;  either version  2  of the License,  or (at your
00011 // option) any later version.
00012 //
00013 // The Omicron Engine  is distributed in the hope that it will be useful, but
00014 // WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
00015 // or  FITNESS FOR A PARTICULAR PURPOSE.  See the  GNU General Public License
00016 // for more details.
00017 //
00018 // You should have  received a copy of the  GNU General Public License  along
00019 // with The Omicron Engine;  if not,  write to the  Free Software Foundation,
00020 // Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA 
00021 //
00022 // --------------------------------------------------------------------------
00023 // Last modified:       $Date: 2002/12/07 19:02:06 $
00024 // By           :       $Author: kolrabi $
00025 // $Id: x_path.cpp,v 1.1.1.1 2002/12/07 19:02:06 kolrabi Exp $ 
00026 
00027 /*
00028 
00029   $Log: x_path.cpp,v $
00030   Revision 1.1.1.1  2002/12/07 19:02:06  kolrabi
00031   initial release
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     // note: only for non closed paths
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 

Generated on Wed Dec 18 15:48:48 2002 for omicron engine by doxygen1.2.18