AnimatLab  2
Test
LineBase.cpp
Go to the documentation of this file.
1 
7 #include "StdAfx.h"
8 #include "IMovableItemCallback.h"
9 #include "ISimGUICallback.h"
10 #include "AnimatBase.h"
11 
12 #include <math.h>
13 #include "Node.h"
14 #include "IPhysicsMovableItem.h"
15 #include "IPhysicsBody.h"
16 #include "BoundingBox.h"
17 #include "MovableItem.h"
18 #include "BodyPart.h"
19 #include "Joint.h"
20 #include "ReceptiveField.h"
21 #include "ContactSensor.h"
22 #include "RigidBody.h"
23 #include "Sensor.h"
24 #include "Attachment.h"
25 #include "Structure.h"
26 #include "Organism.h"
27 #include "ActivatedItem.h"
28 #include "ActivatedItemMgr.h"
29 #include "DataChartMgr.h"
30 #include "ExternalStimuliMgr.h"
31 #include "KeyFrame.h"
32 #include "SimulationRecorder.h"
33 #include "OdorType.h"
34 #include "Odor.h"
35 #include "Light.h"
36 #include "LightManager.h"
37 #include "Simulator.h"
38 
39 #include "ExternalStimulus.h"
40 
41 #include "LineBase.h"
42 
43 namespace AnimatSim
44 {
45  namespace Environment
46  {
47  namespace Bodies
48  {
56 {
57  m_bEnabled = true;
58 
59  m_fltDensity = 0;
60  m_lpJointToParent = NULL;
61 
62  m_fltLength = 0;
63  m_fltPrevLength = 0;
64 }
65 
73 {
74  try
75  {
77  m_aryAttachmentPoints.Clear();
78  }
79  catch(...)
80  {Std_TraceMsg(0, "Caught Error in desctructor of LineBase\r\n", "", -1, false, true);}
81 }
82 
83 void LineBase::Enabled(bool bValue)
84 {
85  RigidBody::Enabled(bValue);
86 
87  //Get the current length of the muscle.
90 }
91 
100 float LineBase::Length() {return m_fltLength;}
101 
111 
112 bool LineBase::AllowMouseManipulation() {return false;}
113 
114 //Cannot set the position for anything derived from line base. Position and length are determined by the location of the attachments.
115 void LineBase::Position(CStdFPoint &oPoint, bool bUseScaling, bool bFireChangeEvent, bool bUpdateMatrix)
116 {}
117 
118 //Cannot set the position for anything derived from line base. Position and length are determined by the location of the attachments.
119 void LineBase::AbsolutePosition(CStdFPoint &oPoint)
120 {}
121 
122 //Cannot orient anything derived from line base. Position and length are determined by the location of the attachments.
123 void LineBase::OrientNewPart(float fltXPos, float fltYPos, float fltZPos, float fltXNorm, float fltYNorm, float fltZNorm)
124 {}
125 
134 CStdArray<Attachment *> *LineBase::AttachmentPoints() {return &m_aryAttachmentPoints;}
135 
147 void LineBase::AttachmentPoints(std::string strXml)
148 {
149  CStdXml oXml;
150  oXml.Deserialize(strXml);
151  oXml.FindElement("Root");
152  oXml.FindChildElement("Attachments");
153 
154  LoadAttachments(oXml);
156 
157  //Redraw the line.
158  Resize();
159 }
160 
170 {
171  float fltLength=0;
172 
173  if(!m_bEnabled)
174  return 0;
175 
176  int iCount = m_aryAttachmentPoints.GetSize();
177 
178  if(iCount<2)
179  return 0;
180 
181  Attachment *lpAttach1 = m_aryAttachmentPoints[0], *lpAttach2 = NULL;
182  for(int iIndex=1; iIndex<iCount; iIndex++)
183  {
184  lpAttach2 = m_aryAttachmentPoints[iIndex];
185  CStdFPoint oPos1 = lpAttach1->GetCurrentPosition();
186  CStdFPoint oPos2 = lpAttach2->GetCurrentPosition();
187 
188  fltLength += Std_CalculateDistance(oPos1, oPos2);
189  lpAttach1 = lpAttach2;
190  }
191 
192  return (fltLength * m_lpSim->DistanceUnits());
193 }
194 
195 void LineBase::AttachedPartMovedOrRotated(std::string strID)
196 {
197  //Redraw the line.
198  Resize();
199 }
200 
202 {
204 
205  //Get the current length of the muscle.
208 }
209 
211 {
215 }
216 
217 #pragma region DataAccesMethods
218 
219 float *LineBase::GetDataPointer(const std::string &strDataType)
220 {
221  std::string strType = Std_CheckString(strDataType);
222 
223  float *lpData = NULL;
224 
225  if(strType == "MUSCLELENGTH" || strType == "LENGTH")
226  lpData = &m_fltLength;
227  else if(strType == "ENABLE")
228  lpData = &m_fltEnabled;
229  else
230  lpData = RigidBody::GetDataPointer(strDataType);
231 
232  return lpData;
233 }
234 
235 bool LineBase::SetData(const std::string &strDataType, const std::string &strValue, bool bThrowError)
236 {
237  if(RigidBody::SetData(strDataType, strValue, false))
238  return true;
239 
240  if(strDataType == "ATTACHMENTPOINTS")
241  {
242  AttachmentPoints(strValue);
243  return true;
244  }
245 
246  if(strDataType == "ENABLED")
247  {
248  Enabled(Std_ToBool(strValue));
249  return true;
250  }
251 
252  if(strDataType == "ATTACHEDPARTMOVEDORROTATED")
253  {
254  AttachedPartMovedOrRotated(strValue);
255  return true;
256  }
257 
258  //If it was not one of those above then we have a problem.
259  if(bThrowError)
260  THROW_PARAM_ERROR(Al_Err_lInvalidDataType, Al_Err_strInvalidDataType, "Data Type", strDataType);
261 
262  return false;
263 }
264 
265 void LineBase::QueryProperties(CStdPtrArray<TypeProperty> &aryProperties)
266 {
267  RigidBody::QueryProperties(aryProperties);
268 
269  aryProperties.Add(new TypeProperty("Enable", AnimatPropertyType::Boolean, AnimatPropertyDirection::Get));
270  aryProperties.Add(new TypeProperty("MuscleLength", AnimatPropertyType::Float, AnimatPropertyDirection::Get));
271  aryProperties.Add(new TypeProperty("Length", AnimatPropertyType::Float, AnimatPropertyDirection::Get));
272 
273  aryProperties.Add(new TypeProperty("AttachmentPoints", AnimatPropertyType::Xml, AnimatPropertyDirection::Set));
274  aryProperties.Add(new TypeProperty("Enabled", AnimatPropertyType::Boolean, AnimatPropertyDirection::Both));
275  aryProperties.Add(new TypeProperty("AttachedPartMovedOrRotated", AnimatPropertyType::String, AnimatPropertyDirection::Set));
276 }
277 
278 #pragma endregion
279 
291 {
292  m_aryAttachmentPoints.Clear();
293 
294  std::string strID;
295  Attachment *lpAttachment=NULL;
296  int iCount = m_aryAttachmentPointIDs.GetSize();
297  for(int iIndex=0; iIndex<iCount; iIndex++)
298  {
299  strID = m_aryAttachmentPointIDs[iIndex];
300  lpAttachment = dynamic_cast<Attachment *>(m_lpStructure->FindRigidBody(strID));
301  m_aryAttachmentPoints.Add(lpAttachment);
302  }
303 
304  if(m_aryAttachmentPoints.GetSize() < 2)
305  Enabled(false);
306 
307  //Get the current length of the muscle.
310 }
311 
312 // There are no parts or joints to create for muscle attachment points.
314 {
315 }
316 
318 {
320 }
321 
322 void LineBase::Load(CStdXml &oXml)
323 {
324  if(!m_lpParent)
325  THROW_ERROR(Al_Err_lParentNotDefined, Al_Err_strParentNotDefined);
326 
327  AnimatBase::Load(oXml);
328 
329  oXml.IntoElem(); //Into RigidBody Element
330 
331  Enabled(oXml.GetChildBool("Enabled", m_bEnabled));
332  IsVisible(oXml.GetChildBool("IsVisible", m_bIsVisible));
333 
334  LoadAttachments(oXml);
335 
336  //Load the colors
337  m_vDiffuse.Load(oXml, "Diffuse", false);
338  m_vAmbient.Load(oXml, "Ambient", false);
339  m_vSpecular.Load(oXml, "Specular", false);
340  m_fltShininess = oXml.GetChildFloat("Shininess", m_fltShininess);
341 
342  oXml.OutOfElem(); //OutOf RigidBody Element
343 }
344 
354 {
355  m_aryAttachmentPointIDs.Clear();
356  if(oXml.FindChildElement("Attachments", false))
357  {
358  oXml.IntoElem();
359  int iCount = oXml.NumberOfChildren();
360  std::string strID;
361  for(int iIndex=0; iIndex<iCount; iIndex++)
362  {
363  oXml.FindChildByIndex(iIndex);
364  strID = oXml.GetChildString();
365  m_aryAttachmentPointIDs.Add(strID);
366  }
367  oXml.OutOfElem(); //OutOf NonSpikingSynapses Element
368  }
369 
370  if(m_aryAttachmentPointIDs.GetSize() < 2)
371  Enabled(false);
372 }
373 
374  } //Bodies
375  } //Environment
376 } //AnimatSim
virtual void Deserialize(std::string &strXml)
Deserializes a string into an xml document.
Definition: StdXml.cpp:162
virtual void AfterResetSimulation()
Called after a simulation reset for some objects.
Definition: LineBase.cpp:210
virtual float PrevLength()
Gets the previous length of the line.
Definition: LineBase.cpp:110
Declares the external stimulus base class.
Base class file for all Animat simulation objects.
virtual float CalculateLength()
Calculates the length of the line.
Definition: LineBase.cpp:169
Declares the simulation recorder class.
virtual bool FindChildElement(std::string strElementName, bool fThrowError=true)
Finds a child element by name.
Definition: StdXml.cpp:256
virtual void LoadAttachments(CStdXml &oXml)
Loads the attachment points list.
Definition: LineBase.cpp:353
Root namespace for the base simulation library for AnimatLab.
float m_fltPrevLength
Length of the line in the previous timestep.
Definition: LineBase.h:32
Declares the body part class.
CStdColor m_vAmbient
The ambient color to apply to this part. It is specified as red, green, blue, and alpha...
Definition: MovableItem.h:79
void Load(CStdXml &oXml, std::string strParamName, bool bThrowError=true)
Loads the color.
Definition: StdColor.cpp:540
virtual bool FindElement(std::string strElementName, bool fThrowError=true)
Finds an element with the specified name.
Definition: StdXml.cpp:179
Simulator * m_lpSim
The pointer to a Simulation.
Definition: AnimatBase.h:43
virtual float Length()
Gets the length of the line.
Definition: LineBase.cpp:100
float m_fltEnabled
This is used for reporting the enabled state in a GetDataPointer call.
Definition: Node.h:35
virtual CStdFPoint Position()
Gets the local position. (m_oPosition)
Definition: RigidBody.cpp:131
virtual bool IntoElem()
Goes into the next element where the cursor is located.
Definition: StdXml.cpp:42
Class that stores information about types for QueryProperty information.
Definition: TypeProperty.h:35
virtual void DistanceUnits(std::string strUnits)
Sets the distance units.
Definition: Simulator.cpp:1717
Declares the key frame class.
Declares the joint class.
Declares the organism class.
AnimatSim::Environment::Structure * m_lpStructure
The pointer to this items parent Structure. If this is not relevant for this object then this is NULL...
Definition: AnimatBase.h:46
virtual bool Enabled()
Tells whether this node is enabled.
Definition: Node.cpp:84
Declares a light object.
virtual void Resize()
Called when this object has been resized.
Definition: BodyPart.cpp:141
Declares the activated item class.
Declares a light manager object.
virtual bool IsVisible()
Query if this object is visible.
float m_fltDensity
Uniform density for the rigid body.
Definition: RigidBody.h:79
Declares the bounding box class.
float m_fltShininess
The shininess of the part. A value between 0 and 128.
Definition: MovableItem.h:88
virtual CStdFPoint GetCurrentPosition()
Gets the current position of this part.
A standard xml manipulation class.
Definition: StdXml.h:19
virtual void Resize()
Called when this object has been resized.
Definition: LineBase.cpp:201
virtual CStdFPoint AbsolutePosition()
Gets the absolute position of this body part.
Specifies a point on a rigid body where a muscle is to be attached.
Definition: Attachment.h:43
Declares the node class.
Declares the line base class.
CStdColor m_vDiffuse
The diffuse color to apply to this part. It is specified as red, green, blue, and alpha...
Definition: MovableItem.h:82
virtual std::string GetChildString(std::string strElementName)
Gets a string value from the element with the specified name.
Definition: StdXml.cpp:307
virtual bool AllowMouseManipulation()
Tells if a given part can be manipulated using the mouse and draggers.
Definition: LineBase.cpp:112
bool Std_ToBool(int iVal)
Converts a value toa bool.
virtual void Load(StdUtils::CStdXml &oXml)
Loads the item using an XML data packet.
Definition: AnimatBase.cpp:771
void Std_TraceMsg(const int iLevel, std::string strMessage, std::string strSourceFile, int iSourceLine, bool bLogToFile, bool bPrintHeader)
Traces a message to the debugger window.
virtual void CreateJoints()
Allows the rigid body to create its joints using the chosen physics engine.
Definition: LineBase.cpp:317
virtual bool GetChildBool(std::string strElementName)
Gets a bool value from the element with the specified name.
Definition: StdXml.cpp:699
CStdColor m_vSpecular
The specular color to apply to this part. It is specified as red, green, blue, and alpha...
Definition: MovableItem.h:85
CStdArray< Attachment * > m_aryAttachmentPoints
A pointer to the primary attachment part.
Definition: LineBase.h:38
bool m_bEnabled
Tells if this item is enabled or not. If it is not enabled then it is not run.
Definition: AnimatBase.h:40
virtual int NumberOfChildren()
Gets the number of children of the current element.
Definition: StdXml.cpp:202
virtual void InitializeAttachments()
Initializes the attachments.
Definition: LineBase.cpp:290
virtual bool OutOfElem()
Goes out of the element where the cursor is located.
Definition: StdXml.cpp:56
Declares the sensor class.
float m_fltLength
Current length of the line.
Definition: LineBase.h:29
Declares the data chart manager class.
Declares the rigid body class.
std::string Std_CheckString(std::string strVal)
Converts a string to upper case and trims it.
Declares the structure class.
Declares the odor type class.
virtual void CreateParts()
Allows the rigid body to create its parts using the chosen physics engine.
Definition: LineBase.cpp:313
Declares the odor class.
Declares the simulator class.
virtual bool FindChildByIndex(int iIndex, bool bThrowError=true)
Finds a child element by index.
Definition: StdXml.cpp:225
Declares the activated item manager class.
CStdArray< Attachment * > * AttachmentPoints()
Gets the attachment points.
Definition: LineBase.cpp:134
Declares the contact sensor class.
Declares the external stimuli manager class.
virtual RigidBody * FindRigidBody(std::string strBodyID, bool bThrowError=true)
Finds a rigid body with a specified ID within this structure.
Definition: Structure.cpp:489
virtual void OrientNewPart(float fltXPos, float fltYPos, float fltZPos, float fltXNorm, float fltYNorm, float fltZNorm)
Orients a new part based on where the parent was clicked and the normal of the surface face...
Definition: LineBase.cpp:123
CStdArray< std::string > m_aryAttachmentPointIDs
The ID's of the attachment points for this muscle. This is used during the load/initialization proces...
Definition: LineBase.h:35
Declares the attachment class.
Declares the receptive field class.
double Std_CalculateDistance(CStdIPoint &ptA, CStdIPoint &ptB)
Calculates the distance between two points.
virtual void AfterResetSimulation()
Called after a simulation reset for some objects.
Definition: RigidBody.cpp:1503
virtual float GetChildFloat(std::string strElementName)
Gets a float value from the element with the specified name.
Definition: StdXml.cpp:617