Code Search for Developers
 
 
  

kdatalist.cpp from Spatial Knowledge Experiments at Krugle


Show kdatalist.cpp syntax highlighted

/*
    Copyright (C) 2003, 2004 by Luca Cappa 
    Written by Luca Cappa groton@users.sourceforge.net
  
    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public
    License as published by the Free Software Foundation; either
    version 2 of the License, or (at your option) any later version.
  
    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Library General Public License for more details.
  
    You should have received a copy of the GNU Library General Public
    License along with this library; if not, write to the Free
    Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include "cssysdef.h"

#include "csutil/sysfunc.h"



#include "csutil/cscolor.h"
#include "csutil/cmdhelp.h"
#include "csutil/cspmeter.h"
#include "csutil/csstring.h"
#include "csutil/scfstr.h"
#include "csutil/xmltiny.h"
#include "csutil/array.h"

#include "cstool/csview.h"
#include "cstool/initapp.h"
#include "cstool/collider.h"

#include "iutil/vfs.h"
#include "iutil/eventq.h"
#include "iutil/event.h"
#include "iutil/objreg.h"
#include "iutil/csinput.h"
#include "iutil/virtclk.h"
#include "iutil/plugin.h"
#include "iutil/string.h"


#include "iengine/sector.h"
#include "iengine/engine.h"
#include "iengine/camera.h"
#include "iengine/light.h"
#include "iengine/texture.h"
#include "iengine/mesh.h"
#include "iengine/movable.h"
#include "iengine/material.h"

#include "imesh/thing.h"
#include "imesh/object.h"
#include "imesh/sprite3d.h"

#include "ivideo/graph3d.h"
#include "ivideo/graph2d.h"
#include "ivideo/natwin.h"
#include "ivideo/txtmgr.h"
#include "ivideo/texture.h"
#include "ivideo/material.h"
#include "ivideo/fontserv.h"

#include "igraphic/imageio.h"

#include "imap/loader.h"

#include "ivaria/reporter.h"
#include "ivaria/stdrep.h"
#include "ivaria/conout.h"
#include "ivaria/reporter.h"
#include "ivaria/stdrep.h"
#include "ivaria/collider.h"

#include "csgeom/quaterni.h"
#include "csgeom/transfrm.h"
#include "csgeom/math3d_d.h"
#include "csgeom/math3d.h"

#include "igeom/polymesh.h"
#include "igeom/objmodel.h"

#include "imap/loader.h"


#include "iaws/aws.h"
#include "iaws/awscnvs.h"


#include "korientation.h"
#include "kblock.h"
#include "kmap.h"
#include "kblockid.h"
#include "kflags.h"
#include "kappstate.h"
#include "ske.h"
#include "kdata.h"
#include "kutil.h"
#include "kquaternion.h"

#include "kdatalist.h"

KDataList::KDataList () 
{
  KDataList::KDataList ("no_name");
}

KDataList::KDataList (const char* p_name)
{
  if (p_name != NULL)
    m_name.AttachNew (new scfString (p_name));
  else
    m_name.AttachNew (new scfString ("null_name"));
}

KDataList::~KDataList()
{
  RemoveAll ();
}





void KDataList::SetName (const char* p_name)
{
  m_name.AttachNew (new scfString (p_name));
}
char* KDataList::GetName ()
{
  return m_name->GetData ();
}



int KDataList::GetCount ()
{
  return m_vector.Length ();
}
 
KData* KDataList::Get (int p_n)
{
  return m_vector.Get (p_n);
}
 
int KDataList::Add (KData* p_obj)
{
  return m_vector.Push (p_obj);
}

int KDataList::GetLastSampleTimestamp ()
{
  return Get (GetCount () - 1)->GetTimestamp ();
}

int KDataList::GetLastSampleMeters ()
{
  return Get (GetCount () - 1)->GetMeters ();
}
 
bool KDataList::Remove (KData* p_obj)
{
  if (m_vector.DeleteIndex (m_vector.Find(p_obj)))
    return true;
  else
    return false;
}

bool KDataList::Remove (int p_n)
{
  KData* l_data = this->Get (p_n);
  if (m_vector.DeleteIndex (p_n))
    return true;
  else
    return false;
}
 
void KDataList::RemoveAll ()
{
  m_vector.DeleteAll ();
}
 
int KDataList::Find (KData* p_data)
{
  return m_vector.Find(p_data);
}

KData* KDataList::FindByName (const char *p_name)
{
  KData* l_data = NULL;
  csString l_string(p_name);
  int i=0;
  for(i = 0;i < GetCount (); i++)
  {
    l_data = Get(i);
    iString* l_string2 = l_data->GetName ();
    if (l_string.Compare (*l_string2))
      return l_data;
  }//for
  return NULL;
}


bool KDataList::WriteXML (iDocumentNode* p_parent)
{
  csRef<iDocumentNode> l_kdataListNode = 
    p_parent->CreateNodeBefore (CS_NODE_ELEMENT, NULL);
  l_kdataListNode->SetValue ("data_list");
  l_kdataListNode->SetAttribute ("name", GetName());

  int l_count = GetCount ();
  int i;
  for (i = 0; i < l_count; i++)
  {
    KData* l_data = Get (i);//get the data.

    //
    //create and fill the node for the data.
    csRef<iDocumentNode> l_dataNode = l_kdataListNode->CreateNodeBefore 
      (CS_NODE_ELEMENT, NULL);
    l_dataNode->SetValue ("data");
    if (l_data->GetName () != NULL)
      l_dataNode->SetAttribute ("name", l_data->GetName ()->GetData ());    
    else
      l_dataNode->SetAttribute ("name", "");
    l_dataNode->SetAttributeAsFloat ("xpos", l_data->GetPosition().x);
    l_dataNode->SetAttributeAsFloat ("ypos", l_data->GetPosition().y);
    l_dataNode->SetAttributeAsFloat ("zpos", l_data->GetPosition().z);
    
    l_dataNode->SetAttributeAsFloat ("xvel", l_data->GetVelocity().x);
    l_dataNode->SetAttributeAsFloat ("yvel", l_data->GetVelocity().y);
    l_dataNode->SetAttributeAsFloat ("zvel", l_data->GetVelocity().z);
    
    l_dataNode->SetAttributeAsFloat ("bodyQX", l_data->m_bodyQ.x);
    l_dataNode->SetAttributeAsFloat ("bodyQY", l_data->m_bodyQ.y);
    l_dataNode->SetAttributeAsFloat ("bodyQZ", l_data->m_bodyQ.z);
    l_dataNode->SetAttributeAsFloat ("bodyQR", l_data->m_bodyQ.r);
    
    l_dataNode->SetAttributeAsFloat ("headQX", l_data->m_headQ.x);
    l_dataNode->SetAttributeAsFloat ("headQY", l_data->m_headQ.y);
    l_dataNode->SetAttributeAsFloat ("headQZ", l_data->m_headQ.z);
    l_dataNode->SetAttributeAsFloat ("headQR", l_data->m_headQ.r);


    l_dataNode->SetAttributeAsFloat ("meter", l_data->GetMeters());

    l_dataNode->SetAttributeAsFloat ("timestamp", l_data->GetTimestamp());
  }//for

  return true;
}

//
//Load the object from XML file.
bool KDataList::ReadXML (iDocumentNode* p_parent)
{
  //Destroy current content.
  RemoveAll ();

  //
  //Assure that the tag is right.
  const char* l_value = p_parent->GetValue ();
  if (strcmp (l_value, "data_list") != 0)
    return false;

  //
  //Set the name.
  SetName (p_parent->GetAttributeValue ("name"));
  
  //
  //Load each KData individually.
  csRef<iDocumentNodeIterator> l_dI = p_parent->GetNodes ();
  while (l_dI->HasNext ())
  {
    csRef<iDocumentNode> l_dN = l_dI->Next ();

    if (strcmp (l_dN->GetValue (), "data"))
      continue;//skip over non <data> tag
  
    const char* l_name = l_dN->GetAttributeValue ("name");

    float l_posX = l_dN->GetAttributeValueAsFloat ("xpos");
    float l_posY = l_dN->GetAttributeValueAsFloat ("ypos");
    float l_posZ = l_dN->GetAttributeValueAsFloat ("zpos");
    csVector3 l_position (l_posX, l_posY, l_posZ);

    float l_velX = l_dN->GetAttributeValueAsFloat ("xvel");
    float l_velY = l_dN->GetAttributeValueAsFloat ("yvel");
    float l_velZ = l_dN->GetAttributeValueAsFloat ("zvel");
    csVector3 l_vel (l_velX, l_velY, l_velZ);

    KQuaternion l_bQ;
    l_bQ.x = l_dN->GetAttributeValueAsFloat ("bodyQX");
    l_bQ.y = l_dN->GetAttributeValueAsFloat ("bodyQY");
    l_bQ.z = l_dN->GetAttributeValueAsFloat ("bodyQZ");
    l_bQ.r = l_dN->GetAttributeValueAsFloat ("bodyQR");
    
    KQuaternion l_hQ;
    l_hQ.x = l_dN->GetAttributeValueAsFloat ("headQX");
    l_hQ.y = l_dN->GetAttributeValueAsFloat ("headQY");
    l_hQ.z = l_dN->GetAttributeValueAsFloat ("headQZ");
    l_hQ.r = l_dN->GetAttributeValueAsFloat ("headQR");


    float l_meters = l_dN->GetAttributeValueAsFloat ("meter");

    int l_tS = l_dN->GetAttributeValueAsFloat ("timestamp");

    KData* l_data = new KData (l_position, /*//@@l_bodyAngle, l_headX,
      l_headY, l_headZ,*/ l_bQ, l_hQ,
      l_vel, l_meters, l_tS, l_name);

    Add (l_data);
  }//while

  return true;
}


void KDataList::GetAtTime (int p_msecs, csVector3& p_pos, 
  KQuaternion& p_hQ, KQuaternion& p_bQ, csVector3& p_vel)
{

  if (p_msecs < 0)
  {
    p_pos = Get (0)->GetPosition ();
    p_hQ = Get (0)->GetHeadAngles ();
    p_bQ = Get (0)->GetBodyAngle ();
    p_vel = Get (0)->GetVelocity ();
    return;
  }//if
  else if (p_msecs >= Get(GetCount () - 1)->GetTimestamp ())
  {
    p_pos = Get (GetCount () - 1)->GetPosition ();
    p_hQ = Get (GetCount () - 1)->GetHeadAngles ();
    p_bQ = Get (GetCount () - 1)->GetBodyAngle ();
    p_vel = Get (GetCount () - 1)->GetVelocity ();
    return;
  }

  //
  ///Binary search for the right KData object.
  KData* l_data1 = 0;
  KData* l_data2 = 0;
    
  int l_start = 0, l_end = GetCount () - 1;
  bool l_notFound = true;
  while (true)
  {
    int l_index = (l_start + l_end ) / 2;
    l_data1 = Get (l_index);
    l_data2 = Get (l_index + 1);
    
    if (l_data1->GetTimestamp () > p_msecs)
      l_end -= (l_end - l_start) / 2;
    else if (l_data2->GetTimestamp () > p_msecs)
      break;
    else
      l_start += (l_end - l_start) / 2;
  }//while

  //
  //Linear interpolation between (l_index) and (l_index+1)
  int l_t1 = l_data1->GetTimestamp ();
  int l_t2 = l_data2->GetTimestamp ();
  
  CS_ASSERT (p_msecs >= 0);
  float l_alpha;
  if (p_msecs != 0)
    l_alpha = ((float)(p_msecs - l_t1)) / ((float)(l_t2 - l_t1));
  else
    l_alpha = 0.0f;


  csVector3 l_pos1 = l_data1->GetPosition ();
  csVector3 l_pos2 = l_data2->GetPosition ();
  p_pos = l_pos1 + (l_pos2 - l_pos1) * l_alpha;

  KQuaternion l_bQ1 = l_data1->GetBodyAngle ();
  KQuaternion l_bQ2 = l_data2->GetBodyAngle ();  
  p_bQ = l_bQ1.Slerp (l_bQ2, l_alpha);

  KQuaternion l_hQ1 = l_data1->GetHeadAngles ();
  KQuaternion l_hQ2 = l_data2->GetHeadAngles ();
  p_hQ = l_hQ1.Slerp (l_hQ2, l_alpha);

  csVector3 l_vel1 = l_data1->GetVelocity ();
  csVector3 l_vel2 = l_data2->GetVelocity ();
  p_vel = l_vel1 + (l_vel2 - l_vel1) * l_alpha;
}




See more files for this project here

Spatial Knowledge Experiments

A simulation of 3D virtual worlds for psychological experiments

Project homepage: http://sourceforge.net/projects/ske
Programming language(s): C,C++,Perl
License: other

  isense/
  joystick/
  Jamfile
  KImageCardinalDirection.h
  SKE.cpp
  SKE.h
  eulerangles.c
  eulerangles.h
  ikdraggable.cpp
  ikdraggable.h
  ikdraghandler.h
  ikmission.h
  ikxmlreader.h
  ikxmlwriter.h
  kappstate.cpp
  kappstate.h
  kbasedraggable.cpp
  kbasedraggable.h
  kbasedraghandler.cpp
  kbasedraghandler.h
  kblock.cpp
  kblock.h
  kblockId.h
  kblockid.cpp
  kcamera.cpp
  kcamera.h
  kcollision.cpp
  kcommandprocessor.cpp
  kcommandprocessor.h
  kconstant.cpp
  kconstant.h
  kcursor3d.cpp
  kcursor3d.h
  kdata.cpp
  kdata.h
  kdatalist.cpp
  kdatalist.h
  kdragmanager.cpp
  kdragmanager.h
  keventhandlermanager.cpp
  keventhandlermanager.h
  kflags.cpp
  kflags.h
  kimagecardinaldirection.cpp
  kkeys.cpp
  kkeys.h
  kkeystate.cpp
  kkeystate.h
  klandmark.cpp
  klandmark.h
  klandmarklist.cpp
  klandmarklist.h
  klight.cpp
  klight.h
  klightlist.cpp
  klightlist.h
  kmap.cpp
  kmap.h
  kmission.cpp
  kmission.h
  kmissiontype.cpp
  kmissiontype.h
  kmode.cpp
  kmode.h
  kmousemanager.cpp
  kmousemanager.h
  kobject3d.cpp
  kobject3d.h
  korientation.cpp
  korientation.h
  kpath.cpp
  kpath.h
  kpathfollower.cpp
  kpathfollower.h
  kplayer.cpp
  kplayer.h
  kposition.cpp
  kposition.h
  kquaternion.cpp
  kquaternion.h
  ksaveddatamanager.cpp
  ksaveddatamanager.h
  ksegment3.h
  ksign.cpp
  ksign.h
  ksprite3d.cpp
  ksprite3d.h
  ksprite3dlist.cpp
  ksprite3dlist.h
  kterrain.cpp
  kterrain.h
  kthing.cpp
  kthing.h
  kutil.cpp
  kutil.h
  kwireframe.cpp
  kwireframe.h
  kxmlhelper.h
  movement1d.h
  movement3d.h
  quattypes.h