00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
#include "BalloonMesh.h"
00013
#include <OpenCAL/PointMass.h>
00014
#include <OpenCAL/Spring.h>
00015
#include <OpenCAL/System.h>
00016
#include <OpenCAL/Vertex.h>
00017
#include <OpenCAL/Triangle.h>
00018
#include <OpenCAL/PressureForce.h>
00019
using namespace OpenCAL;
00020
00021
#include <OpenCAL/Vector3.h>
00022
#include <OpenCAL/Material.h>
00023
using namespace Utils;
00024
00025
using namespace std;
00026
00027
00028
00029
00030
00031
00032 BalloonMesh::BalloonMesh(
System *parent,
const string &filename,
const Vector3 &position)
00033 :
MassSpringSystem(parent), m_filename(filename)
00034 {
00035 constructMesh(position);
00036
new PressureForce(
this);
00037 }
00038
00039 BalloonMesh::~BalloonMesh()
00040 {
00041 }
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
void BalloonMesh::constructMesh(
const Vector3 &position)
00076 {
00077
const float stiffness = 0.1f;
00078
const float damping = 0.005f;
00079
const float mass = 0.1f;
00080
float massEach = 1.0f;
00081
00082
unsigned int i = 0;
00083
PointMass *point;
00084
Vertex *vertex;
00085
Triangle *triangle;
00086
00087
00088
00089 m_surface.
loadFromFile(m_filename);
00090
if(m_surface.
numVertices() > 0)
00091 massEach= mass / m_surface.
numVertices();
00092
00093
00094
00095
00096
00097
00098
for(i = 0; i < m_surface.
numVertices(); ++i)
00099 {
00100 vertex = m_surface.
getVertex(i);
00101 point =
new PointMass(
this, massEach);
00102 point->
setPosition(vertex->
getPosition() + position);
00103 vertex->
linkPosition(point->
getPositionP());
00104 m_connections[vertex] = point;
00105 }
00106
00107
00108
00109
00110
00111
00112
for(i = 0; i < m_surface.
numTriangles(); ++i)
00113 {
00114 triangle = m_surface.
getTriangle(i);
00115 addSprings(triangle, stiffness, damping);
00116 }
00117
00118 m_springConnections.clear();
00119 }
00120
00121
void BalloonMesh::addSprings(
Triangle *triangle,
float stiffness,
float damping)
00122 {
00123
Vertex *v1 = triangle->
getVertex(0);
00124
Vertex *v2 = triangle->
getVertex(1);
00125
Vertex *v3 = triangle->
getVertex(2);
00126
Spring *spring;
00127
00128
if(!isConnected(v1, v2))
00129 {
00130 spring =
new Spring(
this, m_connections[v1], m_connections[v2]);
00131 spring->
setStiffness(stiffness);
00132 spring->
setDamping(damping);
00133 m_springConnections[v1] = v2;
00134 }
00135
00136
if(!isConnected(v2, v3))
00137 {
00138 spring =
new Spring(
this, m_connections[v2], m_connections[v3]);
00139 spring->
setStiffness(stiffness);
00140 spring->
setDamping(damping);
00141 m_springConnections[v2] = v3;
00142 }
00143
00144
if(!isConnected(v3, v1))
00145 {
00146 spring =
new Spring(
this, m_connections[v3], m_connections[v1]);
00147 spring->
setStiffness(stiffness);
00148 spring->
setDamping(damping);
00149 m_springConnections[v3] = v1;
00150 }
00151 }
00152
00153
bool BalloonMesh::isConnected(
Vertex *v1,
Vertex *v2)
00154 {
00155 SpringMap::iterator it;
00156
00157 it = m_springConnections.find(v1);
00158
if(it != m_springConnections.end() && it->second == v2)
00159
return true;
00160
00161 it = m_springConnections.find(v2);
00162
if(it != m_springConnections.end() && it->second == v1)
00163
return true;
00164
00165
return false;
00166 }