AnimatLab  2
Test
OsgPyramid.cpp
1 // OsgLine.cpp: implementation of the OsgLine class.
2 //
4 
5 #include "StdAfx.h"
6 #include "OsgMovableItem.h"
7 #include "OsgBody.h"
8 #include "OsgRigidBody.h"
9 #include "OsgJoint.h"
10 #include "OsgStructure.h"
11 #include "OsgSimulator.h"
12 #include "OsgUserData.h"
13 #include "OsgUserDataVisitor.h"
14 #include "OsgDragger.h"
15 #include "OsgPyramid.h"
16 
17 
18 namespace OsgAnimatSim
19 {
20  namespace Visualization
21  {
22 
24 // Construction/Destruction
26 
27 OsgPyramid::OsgPyramid()
28 {
29 }
30 
31 OsgPyramid::OsgPyramid(osg::Vec3d vPoint, osg::Vec3d vBase[3])
32 {
33  m_vPoint = vPoint;
34  for(int iIdx=0; iIdx<3; iIdx++)
35  m_vBase[iIdx] = vBase[iIdx];
36 }
37 
38 OsgPyramid::OsgPyramid(osg::Vec3d vPoint, osg::Vec3d vBase1, osg::Vec3d vBase2, osg::Vec3d vBase3)
39 {
40  m_vPoint = vPoint;
41  m_vBase[0] = vBase1;
42  m_vBase[1] = vBase2;
43  m_vBase[2] = vBase3;
44 }
45 
46 OsgPyramid::~OsgPyramid()
47 {
48 
49 try
50 {
51 }
52 catch(...)
53 {Std_TraceMsg(0, "Caught Error in desctructor of OsgPyramid\r\n", "", -1, false, true);}
54 }
55 
56 float OsgPyramid::Height()
57 {
58  // Construct a vector from the Pyramid base to the top point
59  float dx = m_vPoint[0] - m_vBase[0][0];
60  float dy = m_vPoint[1] - m_vBase[0][1];
61  float dz = m_vPoint[2] - m_vBase[0][2];
62  osg::Vec3d vt(dx, dy, dz);
63 
64  // Calculate the perpendicular
65  // distance from the base to the top point.
66  // The distance d is the projection of vt in the normal direction.
67  // Because a right-hand coordinate system is assumed, the value of d
68  // may be negative, so the absolute value is returned.
69  osg::Vec3 normal = (m_vBase[1]-m_vBase[0])^(m_vBase[2]-m_vBase[1]);
70  normal.normalize();
71 
72  float d = normal *vt;
73 
74  float result = 0.0;
75  if( d < 0.0 )
76  result = fabs(d);
77  else
78  result = d;
79  return result;
80 }
81 
82 float OsgPyramid::BaseArea()
83 {
84  float fltA = (m_vBase[0] - m_vBase[1]).length();
85  float fltB = (m_vBase[1] - m_vBase[2]).length();
86  float fltC = (m_vBase[2] - m_vBase[0]).length();
87 
88  float fltS = (fltA + fltB + fltC)/2.0;
89 
90  float fltVal = fltS * (fltS - fltA) * (fltS - fltB) * (fltS - fltC);
91  float fltArea = sqrt(fltVal);
92 
93  return fltArea;
94 }
95 
96 float OsgPyramid::Volume()
97 {
98  // from the base to the top point
99  float d = Height();
100 
101  // Calculate the area of the base
102  float baseArea = BaseArea();
103 
104  // Calculate the volume of the polygon's pyramid
105  float fltVolume = (d * baseArea) / 3.0;
106  return fltVolume;
107 }
108 
109 
110  } // Visualization
111 } //OsgAnimatSim
Classes for implementing the cm-labs vortex physics engine for AnimatLab.
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.
Declares the vortex structure class.