diff --git a/EulerIntegrator.cpp b/EulerIntegrator.cpp new file mode 100644 index 0000000..b962c02 --- /dev/null +++ b/EulerIntegrator.cpp @@ -0,0 +1,203 @@ +#include "EulerIntegrator.h" + +//##ModelId=45F4D79800AE +EulerIntegrator::EulerIntegrator(Object& object) : Integrator(object)// constructor +{ +} + +//##ModelId=45F4D79800B0 +EulerIntegrator::~EulerIntegrator() // destructor +{ +} + + +// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ +/*********************** + * Euler Integrator integrate all the particles on the object * + ***********************/ +//void EulerIntegrator::Derivatives(Particle[] points, int NUMP, float DT) +//void EulerIntegrator::integrate(Object object, int NUMP) + +//##ModelId=45F4D79800B2 +void EulerIntegrator::Derivatives(float deltaT, float k) +{ + + int NUMP = object->GetNumberOfParticles(); + +// cout<<"derivative,,,,,,,,,,,,,,,,,,object->GetNumberOfParticles======"<GetNumberOfParticles()<outer_points.size(); j++) + { + + temp_outer_points0[j]->mass = object->outer_points[j]->mass; + temp_outer_points0[j]->r = object->outer_points[j]->r; + temp_outer_points0[j]->v = object->outer_points[j]->v; + temp_outer_points0[j]->f = object->outer_points[j]->f; + temp_outer_points0[j]->dr = object->outer_points[j]->dr; + temp_outer_points0[j]->dv = object->outer_points[j]->dv; + temp_outer_points0[j]->OneOverMass = object->outer_points[j]->OneOverMass; + + temp_inner_points0[j]->mass = object->inner_points[j]->mass; + temp_inner_points0[j]->r = object->inner_points[j]->r; + temp_inner_points0[j]->v = object->inner_points[j]->v; + temp_inner_points0[j]->f = object->inner_points[j]->f; + temp_inner_points0[j]->dr = object->inner_points[j]->dr; + temp_inner_points0[j]->dv = object->inner_points[j]->dv; + temp_inner_points0[j]->OneOverMass = object->inner_points[j]->OneOverMass; + } + + + temp_outer_points1[i]->dv->x = k * (temp_outer_points0[i]->f->x/temp_outer_points0[i]->mass) * deltaT; + temp_outer_points1[i]->dv->y = k * (temp_outer_points0[i]->f->y/temp_outer_points0[i]->mass) * deltaT; + temp_outer_points1[i]->dv->z = k * (temp_outer_points0[i]->f->z/temp_outer_points0[i]->mass) * deltaT; + + temp_outer_points1[i]->dr->x = k * temp_outer_points0[i]->v->x * deltaT; + temp_outer_points1[i]->dr->y = k * temp_outer_points0[i]->v->y * deltaT; + temp_outer_points1[i]->dr->z = k * temp_outer_points0[i]->v->z * deltaT; + + + temp_inner_points1[i]->dv->x = k * (temp_inner_points0[i]->f->x/temp_inner_points0[i]->mass) * deltaT; + temp_inner_points1[i]->dv->y = k * (temp_inner_points0[i]->f->y/temp_inner_points0[i]->mass) * deltaT; + temp_inner_points1[i]->dv->z = k * (temp_inner_points0[i]->f->z/temp_inner_points0[i]->mass) * deltaT; + + temp_inner_points1[i]->dr->x = k * temp_inner_points0[i]->v->x * deltaT; + temp_inner_points1[i]->dr->y = k * temp_inner_points0[i]->v->y * deltaT; + temp_inner_points1[i]->dr->z = k * temp_inner_points0[i]->v->z * deltaT; + + + + +} + +//##ModelId=45F4D79800C2 +void EulerIntegrator::ynew(int i, float k, float deltaT) +{ + k1(i, k, deltaT); + + /*if(object->dim==1){ + if (i==0){ + object->outer_points[i]->dv->x = 0;//temp_outer_points1[i]->dv->x; + object->outer_points[i]->dv->y = 0;//temp_outer_points1[i]->dv->y; + object->outer_points[i]->dv->z = 0;//temp_outer_points1[i]->dv->z; + + object->outer_points[i]->dr->x = 0;//temp_outer_points1[i]->dr->x; // Change in position is velocity times the change in time + object->outer_points[i]->dr->y = 0;//temp_outer_points1[i]->dr->y; + object->outer_points[i]->dr->z = 0;//temp_outer_points1[i]->dr->z; + + object->outer_points[i]->v->x = 0;//temp_outer_points0[i]->v->x + temp_outer_points1[i]->dv->x; // Change in velocity is added to the velocity-> + object->outer_points[i]->v->y = 0;//temp_outer_points0[i]->v->y + temp_outer_points1[i]->dv->y; + object->outer_points[i]->v->z = 0;//temp_outer_points0[i]->v->z + temp_outer_points1[i]->dv->z; + + object->outer_points[i]->r->x = 0;//temp_outer_points0[i]->r->x + temp_outer_points1[i]->dr->x; // Change in position is velocity times the change in time + object->outer_points[i]->r->y = 0;//temp_outer_points0[i]->r->y + temp_outer_points1[i]->dr->y; + object->outer_points[i]->r->z = 0;//temp_outer_points0[i]->r->z + temp_outer_points1[i]->dr->z; + + + + object->inner_points[i]->dv->x = 0;//temp_inner_points1[i]->dv->x; // Change in position is velocity times the change in time + object->inner_points[i]->dv->y = 0;//temp_inner_points1[i]->dv->y; + object->inner_points[i]->dv->z = 0;//temp_inner_points1[i]->dv->z; + + object->inner_points[i]->dr->x = 0;//temp_inner_points1[i]->dr->x; // Change in position is velocity times the change in time + object->inner_points[i]->dr->y = 0;//temp_inner_points1[i]->dr->y; + object->inner_points[i]->dr->z = 0;//temp_inner_points1[i]->dr->z; + + + + object->inner_points[i]->v->x = 0;//temp_inner_points0[i]->v->x + temp_inner_points1[i]->dv->x; // Change in velocity is added to the velocity-> + object->inner_points[i]->v->y = 0;//temp_inner_points0[i]->v->y + temp_inner_points1[i]->dv->y; + object->inner_points[i]->v->z = 0;//temp_inner_points0[i]->v->z + temp_inner_points1[i]->dv->z; + + object->inner_points[i]->r->x = 0;//temp_inner_points0[i]->r->x + temp_inner_points1[i]->dr->x; // Change in position is velocity times the change in time + object->inner_points[i]->r->y = 0;//temp_inner_points0[i]->r->y + temp_inner_points1[i]->dr->y; + object->inner_points[i]->r->z = 0;//temp_inner_points0[i]->r->z + temp_inner_points1[i]->dr->z; + } + else{ + object->outer_points[i]->dv->x = temp_outer_points1[i]->dv->x; + object->outer_points[i]->dv->y = temp_outer_points1[i]->dv->y; + object->outer_points[i]->dv->z = temp_outer_points1[i]->dv->z; + + object->outer_points[i]->dr->x = temp_outer_points1[i]->dr->x; // Change in position is velocity times the change in time + object->outer_points[i]->dr->y = temp_outer_points1[i]->dr->y; + object->outer_points[i]->dr->z = temp_outer_points1[i]->dr->z; + + object->outer_points[i]->v->x = temp_outer_points0[i]->v->x + temp_outer_points1[i]->dv->x; // Change in velocity is added to the velocity-> + object->outer_points[i]->v->y = temp_outer_points0[i]->v->y + temp_outer_points1[i]->dv->y; + object->outer_points[i]->v->z = temp_outer_points0[i]->v->z + temp_outer_points1[i]->dv->z; + + object->outer_points[i]->r->x = temp_outer_points0[i]->r->x + temp_outer_points1[i]->dr->x; // Change in position is velocity times the change in time + object->outer_points[i]->r->y = temp_outer_points0[i]->r->y + temp_outer_points1[i]->dr->y; + object->outer_points[i]->r->z = temp_outer_points0[i]->r->z + temp_outer_points1[i]->dr->z; + + + + object->inner_points[i]->dv->x = temp_inner_points1[i]->dv->x; // Change in position is velocity times the change in time + object->inner_points[i]->dv->y = temp_inner_points1[i]->dv->y; + object->inner_points[i]->dv->z = temp_inner_points1[i]->dv->z; + + object->inner_points[i]->dr->x = temp_inner_points1[i]->dr->x; // Change in position is velocity times the change in time + object->inner_points[i]->dr->y = temp_inner_points1[i]->dr->y; + object->inner_points[i]->dr->z = temp_inner_points1[i]->dr->z; + + + + object->inner_points[i]->v->x = temp_inner_points0[i]->v->x + temp_inner_points1[i]->dv->x; // Change in velocity is added to the velocity-> + object->inner_points[i]->v->y = temp_inner_points0[i]->v->y + temp_inner_points1[i]->dv->y; + object->inner_points[i]->v->z = temp_inner_points0[i]->v->z + temp_inner_points1[i]->dv->z; + + object->inner_points[i]->r->x = temp_inner_points0[i]->r->x + temp_inner_points1[i]->dr->x; // Change in position is velocity times the change in time + object->inner_points[i]->r->y = temp_inner_points0[i]->r->y + temp_inner_points1[i]->dr->y; + object->inner_points[i]->r->z = temp_inner_points0[i]->r->z + temp_inner_points1[i]->dr->z; + } + } + + else{*/ + object->outer_points[i]->dv->x = temp_outer_points1[i]->dv->x; + object->outer_points[i]->dv->y = temp_outer_points1[i]->dv->y; + object->outer_points[i]->dv->z = temp_outer_points1[i]->dv->z; + + object->outer_points[i]->dr->x = temp_outer_points1[i]->dr->x; // Change in position is velocity times the change in time + object->outer_points[i]->dr->y = temp_outer_points1[i]->dr->y; + object->outer_points[i]->dr->z = temp_outer_points1[i]->dr->z; + + object->outer_points[i]->v->x = temp_outer_points0[i]->v->x + temp_outer_points1[i]->dv->x; // Change in velocity is added to the velocity-> + object->outer_points[i]->v->y = temp_outer_points0[i]->v->y + temp_outer_points1[i]->dv->y; + object->outer_points[i]->v->z = temp_outer_points0[i]->v->z + temp_outer_points1[i]->dv->z; + + object->outer_points[i]->r->x = temp_outer_points0[i]->r->x + temp_outer_points1[i]->dr->x; // Change in position is velocity times the change in time + object->outer_points[i]->r->y = temp_outer_points0[i]->r->y + temp_outer_points1[i]->dr->y; + object->outer_points[i]->r->z = temp_outer_points0[i]->r->z + temp_outer_points1[i]->dr->z; + + + + object->inner_points[i]->dv->x = temp_inner_points1[i]->dv->x; // Change in position is velocity times the change in time + object->inner_points[i]->dv->y = temp_inner_points1[i]->dv->y; + object->inner_points[i]->dv->z = temp_inner_points1[i]->dv->z; + + object->inner_points[i]->dr->x = temp_inner_points1[i]->dr->x; // Change in position is velocity times the change in time + object->inner_points[i]->dr->y = temp_inner_points1[i]->dr->y; + object->inner_points[i]->dr->z = temp_inner_points1[i]->dr->z; + + + + object->inner_points[i]->v->x = temp_inner_points0[i]->v->x + temp_inner_points1[i]->dv->x; // Change in velocity is added to the velocity-> + object->inner_points[i]->v->y = temp_inner_points0[i]->v->y + temp_inner_points1[i]->dv->y; + object->inner_points[i]->v->z = temp_inner_points0[i]->v->z + temp_inner_points1[i]->dv->z; + + object->inner_points[i]->r->x = temp_inner_points0[i]->r->x + temp_inner_points1[i]->dr->x; // Change in position is velocity times the change in time + object->inner_points[i]->r->y = temp_inner_points0[i]->r->y + temp_inner_points1[i]->dr->y; + object->inner_points[i]->r->z = temp_inner_points0[i]->r->z + temp_inner_points1[i]->dr->z; +// } +} + diff --git a/EulerIntegrator.h b/EulerIntegrator.h new file mode 100644 index 0000000..ee56082 --- /dev/null +++ b/EulerIntegrator.h @@ -0,0 +1,26 @@ +#ifndef EULERINTEGRATOR_H +#define EULERINTEGRATOR_H + +#include "Integrator.h" + + +class EulerIntegrator : public Integrator +{ +public: + + EulerIntegrator(Object&); // constructor + + virtual ~EulerIntegrator(); // destructor + + + virtual void Derivatives(float, float); // integrate all the particles on the object + +protected: + + void k1(int, float, float); // get the derivatives of velocity and position + + void ynew(int, float, float); // get the new velocity and position + +}; + +#endif /* EULERINTEGRATOR_H */ diff --git a/Face.cpp b/Face.cpp new file mode 100644 index 0000000..eb3a6c2 --- /dev/null +++ b/Face.cpp @@ -0,0 +1,29 @@ +#include "Face.h" +#include + +// Class triface function definition +// Calculate normal vector to a triangle and its field +//##ModelId=45F4D7980066 +void Face::CalNormalNField(void) +{ + // the normal can be calculated by vector cross multiply + +/* cout<<"fp1->r->x=="<r->x<<"==fp1->r->y=="<r->y<<"==fp1->r->z=="<r->z<r->x=="<r->x<<"==fp2->r->y=="<r->y<<"==fp2->r->z=="<r->z<r->x=="<r->x<<"==fp3->r->y=="<r->y<<"==fp3->r->z=="<r->z<r - *fp2->r) ^ (*fp1->r - *fp3->r); +// Vector N = (*fs1->sp1->r - *fs2->sp1->r) ^ (*fs1->sp1->r - *fs3->sp1->r); + + +// cout<<"N.x=="< &springs) : + fp1(Ap1), fp2(Ap2), fp3(Ap3) + { + normal = new Vector(0,0,0); // normal vecotr for face +// force = new Vector(0,0,0); + + fs1 = new Spring(Ap1, Ap2); // first spring points to particle p1&p2 + fs2 = new Spring(Ap2, Ap3); // second spring points to particle p2&p3 + fs3 = new Spring(Ap3, Ap1); // third spring points to particle p3&p1 + + + bool a = false, b = false, c = false; // the three new particles have not existed + + for(int o = 0; o < springs.size(); o++) // for all the spring in the requested spring vecotr + { + if // if the spring exist with particle p1&p2 + ( + springs[o]->sp1 == Ap1 + && springs[o]->sp2 == Ap2 + ) + { + delete fs1; // delete the builded spring fs1 + fs1 = springs[o]; // the new spring will point to the existed spring + a = true; // set the spring's particles have existed + } + + if // if the spring exist with particle p2&p3 + ( + springs[o]->sp1 == Ap2 + && springs[o]->sp2 == Ap3 + ) + { + delete fs2; // delete the builded spring fs2 + fs2 = springs[o]; // the new spring will point to the existed spring + b = true; // set the spring's particles have existed + } + + if // if the spring exist with particle p3&p1 + ( + springs[o]->sp1 == Ap3 + && springs[o]->sp2 == Ap1 + ) + { + delete fs3; // delete the builded spring fs3 + fs3 = springs[o]; // the new spring will point to the existed spring + c = true; // set the spring's particles have existed + } + } + + // The new points are added to the general collection of points + if(!a) springs.push_back(fs1); + if(!b) springs.push_back(fs2); + if(!c) springs.push_back(fs3); + } + +//////////////////////////////////////////////////////////////////////////////////////////////////// + //##ModelId=45F4D7980065 + ~Face() // face destructor + { +// delete fp1; +// delete fp2; +// delete fp3; + delete normal; +// delete force; + } +/////////////////////////////////////////////////////////////////////////////////////////////////// + //##ModelId=45F4D7980066 + void CalNormalNField(void); // Calculate normal vector to a triangle and its field +public: + + //##ModelId=45F4D7980070 + Particle *fp1; // first vertex + //##ModelId=45F4D7980075 + Particle *fp2; // second vertex + //##ModelId=45F4D798007F + Particle *fp3; // third vertex*/ + + //##ModelId=45F4D7980084 + Spring *fs1; // first spring on the face + //##ModelId=45F4D7980089 + Spring *fs2; // second spring on the face + //##ModelId=45F4D798008F + Spring *fs3; // third spring on the face +// float field; // normal field + //##ModelId=45F4D7980094 + Vector* normal; // triangle normal Vector +// Vector* force; // force on the triangle +}; + +#endif diff --git a/Integrator.cpp b/Integrator.cpp new file mode 100644 index 0000000..b9b0f62 --- /dev/null +++ b/Integrator.cpp @@ -0,0 +1,695 @@ +#include "Integrator.h" +#include "Object2D.h" +#include +#include + + +//////////////// integrator constructor/////////////////////////////////////////////////////// +//##ModelId=45F4D797039C +Integrator::Integrator(Object& objectToIntegrate) : dragExists(false) +{ + this->object = &objectToIntegrate; + for(int j=0; j container for integrator computation + temp_outer_points0.push_back(new Particle (*this->object->outer_points[j])); + temp_outer_points1.push_back(new Particle (*this->object->outer_points[j])); + temp_outer_points2.push_back(new Particle (*this->object->outer_points[j])); + temp_outer_points3.push_back(new Particle (*this->object->outer_points[j])); + temp_outer_points4.push_back(new Particle (*this->object->outer_points[j])); + + // temp inner vector container for integrator computation + temp_inner_points0.push_back(new Particle (*this->object->inner_points[j])); + temp_inner_points1.push_back(new Particle (*this->object->inner_points[j])); + temp_inner_points2.push_back(new Particle (*this->object->inner_points[j])); + temp_inner_points3.push_back(new Particle (*this->object->inner_points[j])); + temp_inner_points4.push_back(new Particle (*this->object->inner_points[j])); + } + dim = DIM1D; // default object is 1D +} + +//////////////// integrator destructor/////////////////////////////////////////////////////// + +//##ModelId=45F4D79703AB +Integrator::~Integrator() +{ + temp_outer_points0.clear(); // clear up the vector and free the memory + temp_outer_points1.clear(); + temp_outer_points2.clear(); + temp_outer_points3.clear(); + temp_outer_points4.clear(); + + temp_inner_points0.clear(); + temp_inner_points1.clear(); + temp_inner_points2.clear(); + temp_inner_points3.clear(); + temp_inner_points4.clear(); +} + +/////the calculation of the integration with/without external dragging force////////////////// +//##ModelId=45F4D79703AD +void Integrator::integrate(float deltaT, bool drag, float xDrag, float yDrag) +{ + dragExists = drag; // if the mouse drag exist + mDragX = xDrag; // the mouse position x axis + mDragY = yDrag; // the mouse position y axis + AccumulateForces(); // accumulate all forces + Derivatives(deltaT, 1.0); // compute the derivatives +} + +//////////////////////// accumulate all forces //////////////////////////////////////////////// +//##ModelId=45F4D79703DA +void Integrator::AccumulateForces() // accumulate forces acted on +{ + ExternalForces(); // accumulate gravity and dragging force + SpringForces(); + + switch(dim) // there is addition pressure force for 2D&3D + { + case DIM1D: + break; + + + case DIM2D: // Pressure forces are applicable on 2D + case DIM3D: // Pressure forces are applicable on 3D + PressureForces(); + break; + } +} + +////////////////////// accumulate gravity and dragging force//////////////////////////////////////// +//##ModelId=45F4D79703CA +void Integrator::ExternalForces() +{ + int i; // runing index + float outer_x1,outer_x2,outer_y1,outer_y2,outer_z1,outer_z2; // points p1, p2 on the outer layer + float outer_rd12; // distance between p1, p2 + float outer_Fx,outer_Fy,outer_Fz; // force vector for outer layer + float f; // accumulated force + + for(i = 0; i < object->GetNumberOfParticles(); i++) // contribute gravity force + { + switch(dim) + { + case DIM1D: + break; + + case DIM2D: + // for the inner layer + object->inner_points[i]->f->x = 0;//80*sin(90*i); // initial force on x axis + object->inner_points[i]->f->y = 2.5* MASS * GY; // set the gravity along y axix + object->inner_points[i]->f->z = 0; // set to 0 + break; + + case DIM3D: + // for the inner layer + object->inner_points[i]->f->x = 0;//80*sin(90*i); // initial force on x axis + object->inner_points[i]->f->y = MASS * GY; // set the gravity along y axix + object->inner_points[i]->f->z = 0; // set to 0 + break; + } + // for the outer layer + object->outer_points[i]->f->x = 0;//80* cos(25*i); // initial force on x axis + /* if (i==1) + object->outer_points[i]->f->y = 0;//MASS * GY; // set the gravity along y axix + else + */ object->outer_points[i]->f->y = MASS * GY; // set the gravity along y axix + object->outer_points[i]->f->z = 0; // set to 0 + + if(i == object->closest_i) // if is the nearest particle to mouse + { + if(dragExists) // if user clicked + { + outer_x1 = object->outer_points[ i ]->r->x; // get points X-coord + outer_y1 = object->outer_points[ i ]->r->y; // get points Y-coord + outer_z1 = object->outer_points[ i ]->r->z; // get points Z-coord + + outer_x2 = mDragX; // get Mouse X-coord + outer_y2 = mDragY; // get Mouse Y-coord + outer_z2 = 0; // set Mouse Z-coord + + outer_rd12 = // the distance nearest/mouse + sqrt((outer_x1-outer_x2) * (outer_x1-outer_x2) + +(outer_y1-outer_y2) * (outer_y1-outer_y2) + +(outer_z1-outer_z2) * (outer_z1-outer_z2) + ); + + f = (outer_rd12 - MOUSE_REST) * MOUSE_KS // the dragging force calculation + + ( + object->outer_points[i]->v->x * (outer_x1-outer_x2) + + object->outer_points[i]->v->y * (outer_y1-outer_y2) + + object->outer_points[i]->v->z * (outer_z1-outer_z2) + ) * MOUSE_KD / outer_rd12; + + // calculate spring force vector and its direction + outer_Fx = ((outer_x1 - outer_x2) / outer_rd12) * f; + outer_Fy = ((outer_y1 - outer_y2) / outer_rd12) * f; + outer_Fz = ((outer_z1 - outer_z2) / outer_rd12) * f; + + // contribute the dragging forces to the dragged point + object->outer_points[i]->f->x -= outer_Fx; + object->outer_points[i]->f->y -= outer_Fy; + object->outer_points[i]->f->z -= outer_Fz; + } + } + } +} + +/////// general spring calculation function ////////////////////////////////////////////////// +//##ModelId=45F4D79703CC +void Integrator::SpringForces() +{ + int i; // runing index + + // Three parts for computing the spring forces on all the points + for(i = 0; iGetNumberOfSprings(); i++) + { + + // Part #1 outer structual spring force contribution + // exist for 1D, 2D outer structual, and 3D outer structual + CalculateSpringForces(object->outer_springs, i); + + switch(dim) + { + case DIM1D: // intentionally empty, no more other type of springs + break; + + case DIM2D: // for 2D include the following additional springs + case DIM3D: // for 3D include the following additional springs + + { + // Part #2 inner structual spring force contribution + CalculateSpringForces + ( + ((Object2D*)object)->inner_springs, + i + ); + + // Part #3 radium spring contribution + CalculateSpringForces + ( + ((Object2D*)object)->radium_springs, + i, + false + ); + + // Part #4 shear left spring contribution + CalculateSpringForces + ( + ((Object2D*)object)->shear_springs_left, + i + ); + + // Part #5 shear right spring contribution + CalculateSpringForces + ( + ((Object2D*)object)->shear_springs_right, + i + ); + break; + } + } + } +} + + +//////////////////////////////////////////////////////////////////////////////////////////////////////////// +//##ModelId=45F4D7980004 +void Integrator::CalculateSpringForces(vectorsprings, int i, bool special) +{ + float vx12, vy12, vz12; + float Fx, Fy, Fz; + float f; + + float x1 = springs[i]->sp1->r->x; // tha head particle of the spring + float y1 = springs[i]->sp1->r->y; + float z1 = springs[i]->sp1->r->z; + + float x2 = springs[i]->sp2->r->x; // the tail particle of the spring + float y2 = springs[i]->sp2->r->y; + float z2 = springs[i]->sp2->r->z; + + // the length of the spring + float rd12 = (*springs[i]->sp1->r - *springs[i]->sp2->r).getLength(); + + if(rd12 == 0) + { + return; + } + + // Calculate normal vectors to springs + + if(special) ////?????????????????????????? + dim = DIM2D; + + switch(dim) + { + case DIM1D: + case DIM2D: + springs[i]->normal.x = -(y1 - y2) / rd12; // Normal X-vector + springs[i]->normal.y = +(x1 - x2) / rd12; // Normal Y-vector + springs[i]->normal.z = 0; // Normal Z-vector + break; + + case DIM3D: + springs[i]->normal.x = -(x1 - x2) / rd12; // Normal X-vector + springs[i]->normal.y = +(z1 - z2) / rd12; // Normal Y-vector + springs[i]->normal.z = +(y1 - y2) / rd12; // Normal Z-vector + break; + } + + // velocity vector of the two ends of the spring + vx12 = springs[i]->sp1->v->x - springs[i]->sp2->v->x; + vy12 = springs[i]->sp1->v->y - springs[i]->sp2->v->y; + vz12 = springs[i]->sp1->v->z - springs[i]->sp2->v->z; + + // hook spring force of its damping force + f = (rd12 - springs[i]->restLen) * KS + + (vx12 * (x1 - x2) + + vy12 * (y1 - y2) + + vz12 * (z1 - z2)) * KD / rd12; + + // spring vector on x, y, z axix + Fx = ((x1 - x2) / rd12) * f; + Fy = ((y1 - y2) / rd12) * f; + Fz = ((z1 - z2) / rd12) * f; + + // accumulate the force on the start partilce of the spring + springs[i]->sp1->f->x -= Fx; + springs[i]->sp1->f->y -= Fy; + springs[i]->sp1->f->z -= Fz; + // accumulate the force on the end partilce of the spring + springs[i]->sp2->f->x += Fx; + springs[i]->sp2->f->y += Fy; + springs[i]->sp2->f->z += Fz; +} + +///////////////////////////////////////////////////////////////////////////////////////////////////////////// + +//##ModelId=45F4D79703CE +void Integrator::PressureForces() +{ + int i; // runing index + + float inner_x1,inner_x2,inner_y1,inner_y2,inner_z1,inner_z2; // points p1, p2 on the inner layer + float outer_x1,outer_x2,outer_y1,outer_y2,outer_z1,outer_z2; // points p1, p2 on the outer layer + + float inner_rd12; // distance of innter p1, p2 + float outer_rd12; // distance of outer p1, p2 + + float inner_volume=0.0; // inner circle inner_volume + float outer_volume=0.0; // outer circle outer_volume + + float inner_p_accu; // inner pressure force accumulation + float outer_p_accu; // outer pressure force accumulation + + + // pressure force for the inner and outer circles + + for(i=0; iGetNumberOfSprings(); i++) // for every spring on the object + { + inner_x1 = object->inner_springs[i]->sp1->r->x; // inner spring start particle + inner_y1 = object->inner_springs[i]->sp1->r->y; + inner_z1 = object->inner_springs[i]->sp1->r->z; + + inner_x2 = object->inner_springs[i]->sp2->r->x; // inner spring end particle + inner_y2 = object->inner_springs[i]->sp2->r->y; + inner_z2 = object->inner_springs[i]->sp2->r->z; + + inner_rd12 = // need the inner spring length for volume + sqrt( + (inner_x1-inner_x2)*(inner_x1-inner_x2) + +(inner_y1-inner_y2)*(inner_y1-inner_y2) + +(inner_z1-inner_z2)*(inner_z1-inner_z2) + ); + + // based on the Gauss Theorem??????????????????????????????// calculation of inner circle volume + inner_volume += 40.5 * + fabs(inner_x1 - inner_x2) * + ( + fabs(object->inner_springs[i]->normal.x) + +fabs(object->inner_springs[i]->normal.y) + +fabs(object->inner_springs[i]->normal.z) + ) * + (inner_rd12); + + outer_x1 = object->outer_springs[i]->sp1->r->x; // outer spring start particle + outer_y1 = object->outer_springs[i]->sp1->r->y; + outer_z1 = object->outer_springs[i]->sp1->r->z; + + outer_x2 = object->outer_springs[i]->sp2->r->x; // outer spring end particle + outer_y2 = object->outer_springs[i]->sp2->r->y; + outer_z2 = object->outer_springs[i]->sp2->r->z; + + outer_rd12 = // need the outer spring length for volume + sqrt( + (outer_x1-outer_x2)*(outer_x1-outer_x2) + +(outer_y1-outer_y2)*(outer_y1-outer_y2) + +(outer_z1-outer_z2)*(outer_z1-outer_z2) + ); + + outer_volume += 40.5 * // calculation of outer layer volume + fabs(outer_x1 - outer_x2) * +// fabs(outer_y1 - outer_y2) * +// fabs(outer_z1 - outer_z2) * + ( + fabs(object->outer_springs[i]->normal.x) + +fabs(object->outer_springs[i]->normal.y) + +fabs(object->outer_springs[i]->normal.z) + ) * + (outer_rd12); + } + + // calculation inner and outer layer pressure force + for(i=0; iGetNumberOfSprings(); i++) + { + inner_x1 = object->inner_springs[i]->sp1->r->x; + inner_y1 = object->inner_springs[i]->sp1->r->y; + inner_z1 = object->inner_springs[i]->sp1->r->z; + + inner_x2 = object->inner_springs[i]->sp2->r->x; + inner_y2 = object->inner_springs[i]->sp2->r->y; + inner_z2 = object->inner_springs[i]->sp2->r->z; + + inner_rd12=sqrt((inner_x1-inner_x2)*(inner_x1-inner_x2) + + (inner_y1-inner_y2)*(inner_y1-inner_y2) + + (inner_z1-inner_z2)*(inner_z1-inner_z2) + ); + + inner_p_accu = inner_rd12 * ((Object2D*)object)->getPressure() * (1.0f / inner_volume); + + // contribute inner pressure to the start particle on this spring + object->inner_springs[i]->sp1->f->x += object->inner_springs[i]->normal.x*inner_p_accu; + object->inner_springs[i]->sp1->f->y += object->inner_springs[i]->normal.y*inner_p_accu; + object->inner_springs[i]->sp1->f->z += object->inner_springs[i]->normal.z*inner_p_accu; + + // contribute inner pressure to the end particle on this spring + object->inner_springs[i]->sp2->f->x += object->inner_springs[i]->normal.x*inner_p_accu; + object->inner_springs[i]->sp2->f->y += object->inner_springs[i]->normal.y*inner_p_accu; + object->inner_springs[i]->sp2->f->z += object->inner_springs[i]->normal.z*inner_p_accu; + + //////////////////////////////////////////// + outer_x1 = object->outer_springs[i]->sp1->r->x; + outer_y1 = object->outer_springs[i]->sp1->r->y; + outer_z1 = object->outer_springs[i]->sp1->r->z; + + outer_x2 = object->outer_springs[i]->sp2->r->x; + outer_y2 = object->outer_springs[i]->sp2->r->y; + outer_z2 = object->outer_springs[i]->sp2->r->z; + + outer_rd12=sqrt((outer_x1-outer_x2)*(outer_x1-outer_x2) + + (outer_y1-outer_y2)*(outer_y1-outer_y2) + + (outer_z1-outer_z2)*(outer_z1-outer_z2) + ); + + + outer_p_accu = outer_rd12 * ((Object2D*)object)->getPressure() * (1.0f / outer_volume); + + // contribute outer pressure to the start particle on this spring + object->outer_springs[i]->sp1->f->x += object->outer_springs[i]->normal.x * outer_p_accu; + object->outer_springs[i]->sp1->f->y += object->outer_springs[i]->normal.y * outer_p_accu; + object->outer_springs[i]->sp1->f->z += object->outer_springs[i]->normal.z * outer_p_accu; + + // contribute outer pressure to the end particle on this spring + object->outer_springs[i]->sp2->f->x += object->outer_springs[i]->normal.x * outer_p_accu; + object->outer_springs[i]->sp2->f->y += object->outer_springs[i]->normal.y * outer_p_accu; + object->outer_springs[i]->sp2->f->z += object->outer_springs[i]->normal.z * outer_p_accu; + } +} +//*/ + +/* +//////////////////////////////another unused pressure function based on the triangle face normal//////////// +void Integrator::PressureForces() +{ + int i; // runing index + + float inner_x1,inner_x2,inner_y1,inner_y2,inner_z1,inner_z2; // points inner_p1, inner_p2 on the inner circle + float outer_x1,outer_x2,outer_y1,outer_y2,outer_z1,outer_z2; // points outer_p1, outer_p2 on the outer circle + + float inner_rd12; // length of inner_p1, inner_p2 + float outer_rd12; // length of outer_p1, outer_p2 + + float inner_volume=0.0; // inner circle inner_volume + float outer_volume=0.0; // outer circle outer_volume + + float inner_p_accu=0;; // pressure force accumulation + float outer_p_accu=0;; // pressure force accumulation + + for(i=0;iinner_faces.size();i++) + { + object->inner_faces[i]->CalNormalNField(); + object->outer_faces[i]->CalNormalNField(); + } + + // pressure force for the inner circle + + for(i=0; iouter_faces.size(); i++) // calculation of inner circle volume + { + inner_volume += 0.5 * (object->inner_faces[i]->fp1->r->x + + object->inner_faces[i]->fp2->r->x + + object->inner_faces[i]->fp3->r->x)* + (object->inner_faces[i]->normal->x) * + (object->inner_faces[i]->normal->getLength()); + + outer_volume += 0.5 * (object->outer_faces[i]->fp1->r->x + + object->outer_faces[i]->fp2->r->x + + object->outer_faces[i]->fp3->r->x)* + (object->outer_faces[i]->normal->x) * + (object->outer_faces[i]->normal->getLength()); + } + + + for(i = 0; i < object->outer_faces.size(); i++) // calculation inner pressure force + { + inner_p_accu=(object->inner_faces[i]->normal->x) * + (object->inner_faces[i]->normal->getLength())* + ((Object2D*)object)->getPressure()*(1.0f/inner_volume); + + object->inner_faces[i]->fp1->f->x += 0.5 * inner_p_accu; + object->inner_faces[i]->fp1->f->y += 0.5 * inner_p_accu; + object->inner_faces[i]->fp1->f->z += 0.5 * inner_p_accu; + + object->inner_faces[i]->fp2->f->x += 0.5 * inner_p_accu; + object->inner_faces[i]->fp2->f->y += 0.5 * inner_p_accu; + object->inner_faces[i]->fp2->f->z += 0.5 * inner_p_accu; + + object->inner_faces[i]->fp3->f->x += 0.5 * inner_p_accu; + object->inner_faces[i]->fp3->f->y += 0.5 * inner_p_accu; + object->inner_faces[i]->fp3->f->z += 0.5 * inner_p_accu; + + //////////////////////////////////////////// + + outer_p_accu=(object->outer_faces[i]->normal->x) * + (object->outer_faces[i]->normal->getLength()) * + ((Object2D*)object)->getPressure() * (1.0f/outer_volume); + + object->outer_faces[i]->fp1->f->x += 0.5 * outer_p_accu; + object->outer_faces[i]->fp1->f->y += 0.5 * outer_p_accu; + object->outer_faces[i]->fp1->f->z += 0.5 * outer_p_accu; + + object->outer_faces[i]->fp2->f->x += 0.5 * outer_p_accu; + object->outer_faces[i]->fp2->f->y += 0.5 * outer_p_accu; + object->outer_faces[i]->fp2->f->z += 0.5 * outer_p_accu; + + object->outer_faces[i]->fp3->f->x += 0.5 * outer_p_accu; + object->outer_faces[i]->fp3->f->y += 0.5 * outer_p_accu; + object->outer_faces[i]->fp3->f->z += 0.5 * outer_p_accu; + } +} + + +//*/ + + + + +///////// calculate the collision detection ////////////////////////////////////////////////// +//##ModelId=45F4D7980001 +void Integrator::CollisionDetection(int i) +{ + bool changed = false; + float decay = 0.5; + + +/////////////////////check the outer layer particle if collide with the wall//////////////// + // Check #1 - X boundaries left + if((object->outer_points[i]->r->x + object->outer_points[i]->dr->x) < -LIMIT) + { + object->outer_points[i]->dr->x = -LIMIT - object->outer_points[i]->r->x; + changed = true; + } // Check #1 - X boundaries right + else if((object->outer_points[i]->r->x + object->outer_points[i]->dr->x) > LIMIT) + { + object->outer_points[i]->dr->x = LIMIT - object->outer_points[i]->r->x; + changed = true; + } + + if(changed) + { + object->outer_points[i]->v->x = -decay * object->outer_points[i]->v->x; + object->outer_points[i]->v->y = 0.5 * object->outer_points[i]->v->y; + object->outer_points[i]->v->z = 0.5 * object->outer_points[i]->v->z; + + /* if(object->dim == 1){ + if(i==1){ + object->outer_points[i]->r->x = 0;//object->outer_points[i]->r->x + object->outer_points[i]->dr->x; + } + + } + else{ + object->outer_points[i]->r->x = object->outer_points[i]->r->x + object->outer_points[i]->dr->x; + }*/ + object->outer_points[i]->r->x = object->outer_points[i]->r->x + object->outer_points[i]->dr->x; + changed = false; + } + + + // Check #2 - Y boundaries left + if((object->outer_points[i]->r->y + object->outer_points[i]->dr->y) < -LIMIT) + { + object->outer_points[i]->dr->y = -LIMIT - object->outer_points[i]->r->y; + changed = true; + } // Check #2 - Y boundaries right + else if((object->outer_points[i]->r->y + object->outer_points[i]->dr->y) > LIMIT) + { + object->outer_points[i]->dr->y = LIMIT - object->outer_points[i]->r->y; + changed = true; + } + + if(changed) + { + object->outer_points[i]->v->x = 0.5 * object->outer_points[i]->v->x; + object->outer_points[i]->v->y = - decay * object->outer_points[i]->v->y; + object->outer_points[i]->v->z = 0.5 * object->outer_points[i]->v->z; + + object->outer_points[i]->r->y = object->outer_points[i]->r->y + object->outer_points[i]->dr->y; + changed = false; + } + + + // Check #3 - Z boundary left + if((object->outer_points[i]->r->z + object->outer_points[i]->dr->z) < -LIMIT ) + { + object->outer_points[i]->dr->z = -LIMIT - object->outer_points[i]->r->z; + changed = true; + } // Check #3 - Z boundary right + else if((object->outer_points[i]->r->z + object->outer_points[i]->dr->z) > LIMIT ) + { + object->outer_points[i]->dr->z = LIMIT - object->outer_points[i]->r->z; + changed = true; + } + + + if(changed) + { + object->outer_points[i]->v->x = 0.5 * object->outer_points[i]->v->x; + object->outer_points[i]->v->y = 0.5 * object->outer_points[i]->v->y; + object->outer_points[i]->v->z = - decay * object->outer_points[i]->v->z; + + object->outer_points[i]->r->z = object->outer_points[i]->r->z + object->outer_points[i]->dr->z; + changed = false; + } + + + + // Check #4 - Boundary limitations + if (object->outer_points[i]->r->x < -LIMIT) object->outer_points[i]->r->x = -LIMIT; + if (object->outer_points[i]->r->y < -LIMIT) object->outer_points[i]->r->y = -LIMIT; + if (object->outer_points[i]->r->z < -LIMIT) object->outer_points[i]->r->z = -LIMIT; + + if (object->outer_points[i]->r->x > LIMIT) object->outer_points[i]->r->x = LIMIT; + if (object->outer_points[i]->r->y > LIMIT) object->outer_points[i]->r->y = LIMIT; + if (object->outer_points[i]->r->z > LIMIT) object->outer_points[i]->r->z = LIMIT; + + + +///////////////////////////////////////////////////////////////////////////////////////////////////////// + +/////////////////////check the inner layer particle if collide with the wall//////////////// + + // Check #1 - X boundaries + if((object->inner_points[i]->r->x + object->inner_points[i]->dr->x) < -LIMIT) + { + object->inner_points[i]->dr->x = -LIMIT - object->inner_points[i]->r->x; + changed = true; + } + else if((object->inner_points[i]->r->x + object->inner_points[i]->dr->x) > LIMIT) + { + object->inner_points[i]->dr->x = LIMIT - object->inner_points[i]->r->x; + changed = true; + } + + if(changed) + { + object->inner_points[i]->v->x = -decay * object->inner_points[i]->v->x; + object->inner_points[i]->v->y = 0.5 * object->inner_points[i]->v->y; + object->inner_points[i]->v->z = 0.5 * object->inner_points[i]->v->z; + + object->inner_points[i]->r->x = object->inner_points[i]->r->x + object->inner_points[i]->dr->x; + changed = false; + + } + + + // Check #2 - Y boundaries + if((object->inner_points[i]->r->y + object->inner_points[i]->dr->y) < -LIMIT) + { + object->inner_points[i]->dr->y = -LIMIT - object->inner_points[i]->r->y; + changed = true; + } + else if((object->inner_points[i]->r->y + object->inner_points[i]->dr->y) > LIMIT) + { + object->inner_points[i]->dr->y = LIMIT - object->inner_points[i]->r->y; + changed = true; + } + + if(changed) + { + object->inner_points[i]->v->x = 0.5 * object->inner_points[i]->v->x; + object->inner_points[i]->v->y = - decay * object->inner_points[i]->v->y; + object->inner_points[i]->v->z = 0.5 * object->inner_points[i]->v->z; + + object->inner_points[i]->r->y = object->inner_points[i]->r->y + object->inner_points[i]->dr->y; + changed = false; + } + + + // Check #3 - Z boundary + + if((object->inner_points[i]->r->z + object->inner_points[i]->dr->z) < -LIMIT ) + { + object->inner_points[i]->dr->z = -LIMIT - object->inner_points[i]->r->z; + changed = true; + } + else if((object->inner_points[i]->r->z + object->inner_points[i]->dr->z) > LIMIT ) + { + object->inner_points[i]->dr->z = LIMIT - object->inner_points[i]->r->z; + changed = true; + } + + + if(changed) + { + object->inner_points[i]->v->x = 0.5 * object->inner_points[i]->v->x; + object->inner_points[i]->v->y = 0.5 * object->inner_points[i]->v->y; + object->inner_points[i]->v->z = - decay * object->inner_points[i]->v->z; + + object->inner_points[i]->r->z = object->inner_points[i]->r->z + object->inner_points[i]->dr->z; + changed = false; + } + + + + // Check #4 - Boundary limitations + if (object->inner_points[i]->r->x < -LIMIT) object->inner_points[i]->r->x = -LIMIT; + if (object->inner_points[i]->r->y < -LIMIT) object->inner_points[i]->r->y = -LIMIT; + if (object->inner_points[i]->r->z < -LIMIT) object->inner_points[i]->r->z = -LIMIT; + + if (object->inner_points[i]->r->x > LIMIT) object->inner_points[i]->r->x = LIMIT; + if (object->inner_points[i]->r->y > LIMIT) object->inner_points[i]->r->y = LIMIT; + if (object->inner_points[i]->r->z > LIMIT) object->inner_points[i]->r->z = LIMIT; + + +} + diff --git a/Integrator.h b/Integrator.h new file mode 100644 index 0000000..05100f6 --- /dev/null +++ b/Integrator.h @@ -0,0 +1,83 @@ +#ifndef INTEGRATOR_H +#define INTEGRATOR_H + +#include "Object.h" + +class Object; + + +class Integrator +{ +protected: + + Object* object; + + + vector temp_inner_points0; // temp inner vector container for integrator computation + + vector temp_inner_points1; + + vector temp_inner_points2; + + vector temp_inner_points3; + + vector temp_inner_points4; + + + vector temp_outer_points0; // temp outer vector container for integrator computation + + vector temp_outer_points1; + + vector temp_outer_points2; + + vector temp_outer_points3; + + vector temp_outer_points4; + + + dimensionality dim; + +public: + + bool dragExists; //if there is user interaction + + float mDragX; // mouse position x axis + + float mDragY; // mouse position y axis + +public: + + Integrator(Object&); // integrator constructor with the computed object + + virtual ~Integrator(); // integrator destructor + + // the calculation of the integration with/without external dragging force + + virtual void integrate(float, bool drag = false, float xDrag = 0, float yDrag = 0); + + + dimensionality getDimension() {return dim;} // get the object type, 1D, 2D, or 3D + + void setDimension(dimensionality dim) {this->dim = dim;} // set the object type, 1D, 2D, or 3D + +protected: + + virtual void ExternalForces(); // accumulate gravity and dragging force + + virtual void SpringForces(); // accumulate all type of springs forces + + virtual void PressureForces(); // accumulate pressure + + virtual void AccumulateForces(); // accumulate all forces acted on object + + virtual void Derivatives(float, float) = 0; // Differential computation + + virtual void CollisionDetection(int); // calculate the collision detection + +private: + // general spring calculation function + + void CalculateSpringForces(vectorsprings, int i, bool special = false); +}; + +#endif /* INTEGRATOR_H */ diff --git a/MidpointIntegrator.cpp b/MidpointIntegrator.cpp new file mode 100644 index 0000000..0f079c0 --- /dev/null +++ b/MidpointIntegrator.cpp @@ -0,0 +1,96 @@ +#include "MidpointIntegrator.h" +#include "Object2D.h" + +//##ModelId=45F4D79702B3 +MidpointIntegrator::MidpointIntegrator(Object& object) : EulerIntegrator(object) +{ +} + +//##ModelId=45F4D79702B5 +MidpointIntegrator::~MidpointIntegrator() +{ +} + +//##ModelId=45F4D79702B6 +void MidpointIntegrator::k2(int i, float k, float deltaT) +{ + k1(i, k, deltaT); + +// memcpy(temp_inner_points2, temp_inner_points1, sizeof(Particle) * MAX_POINTS_SPRINGS); +// memcpy(temp_outer_points2, temp_outer_points1, sizeof(Particle) * MAX_POINTS_SPRINGS); + for(int j=0; jouter_points.size(); j++) + { + + temp_outer_points1[j]->mass = temp_outer_points0[j]->mass; + temp_outer_points1[j]->r = temp_outer_points0[j]->r; + temp_outer_points1[j]->v = temp_outer_points0[j]->v; + temp_outer_points1[j]->f = temp_outer_points0[j]->f; + temp_outer_points1[j]->dr = temp_outer_points0[j]->dr; + temp_outer_points1[j]->dv = temp_outer_points0[j]->dv; +// temp_outer_points1[j]->OneOverMass = temp_outer_points0[j]->OneOverMass; + + temp_inner_points1[j]->mass = temp_inner_points0[j]->mass; + temp_inner_points1[j]->r = temp_inner_points0[j]->r; + temp_inner_points1[j]->v = temp_inner_points0[j]->v; + temp_inner_points1[j]->f = temp_inner_points0[j]->f; + temp_inner_points1[j]->dr = temp_inner_points0[j]->dr; + temp_inner_points1[j]->dv = temp_inner_points0[j]->dv; +// temp_inner_points1[j]->OneOverMass = temp_inner_points0[j]->OneOverMass; + } + + + temp_inner_points2[i]->dv->x = temp_inner_points0[i]->dv->x + k * 0.5 * (temp_inner_points1[i]->f->x/temp_inner_points1[i]->mass) * deltaT; + temp_inner_points2[i]->dv->y = temp_inner_points0[i]->dv->y + k * 0.5 * (temp_inner_points1[i]->f->y/temp_inner_points1[i]->mass) * deltaT; + temp_inner_points2[i]->dv->z = temp_inner_points0[i]->dv->z + k * 0.5 * (temp_inner_points1[i]->f->z/temp_inner_points1[i]->mass) * deltaT; + + temp_inner_points2[i]->dr->x = temp_inner_points0[i]->dr->x + k * 0.5 * temp_inner_points1[i]->dv->x * deltaT; + temp_inner_points2[i]->dr->y = temp_inner_points0[i]->dr->y + k * 0.5 * temp_inner_points1[i]->dv->y * deltaT; + temp_inner_points2[i]->dr->z = temp_inner_points0[i]->dr->z + k * 0.5 * temp_inner_points1[i]->dv->z * deltaT; + + temp_outer_points2[i]->dv->x = temp_outer_points0[i]->dv->x + k * 0.5 * (temp_outer_points1[i]->f->x/temp_outer_points1[i]->mass) * deltaT; + temp_outer_points2[i]->dv->y = temp_outer_points0[i]->dv->y + k * 0.5 * (temp_outer_points1[i]->f->y/temp_outer_points1[i]->mass) * deltaT; + temp_outer_points2[i]->dv->z = temp_outer_points0[i]->dv->z + k * 0.5 * (temp_outer_points1[i]->f->z/temp_outer_points1[i]->mass) * deltaT; + + temp_outer_points2[i]->dr->x = temp_outer_points0[i]->dr->x + k * 0.5 * temp_outer_points1[i]->dv->x * deltaT; + temp_outer_points2[i]->dr->y = temp_outer_points0[i]->dr->y + k * 0.5 * temp_outer_points1[i]->dv->y * deltaT; + temp_outer_points2[i]->dr->z = temp_outer_points0[i]->dr->z + k * 0.5 * temp_outer_points1[i]->dv->z * deltaT; +} + +//##ModelId=45F4D79702C4 +void MidpointIntegrator::ynew(int i, float k, float deltaT) +{ + k2(i, k, deltaT); + + object->inner_points[i]->dv->x = temp_inner_points2[i]->dv->x; + object->inner_points[i]->dv->y = temp_inner_points2[i]->dv->y; + object->inner_points[i]->dv->z = temp_inner_points2[i]->dv->z; + + object->inner_points[i]->dr->x = temp_inner_points2[i]->dr->x; // Change in position is velocity times the change in time + object->inner_points[i]->dr->y = temp_inner_points2[i]->dr->y; + object->inner_points[i]->dr->z = temp_inner_points2[i]->dr->z; + + object->inner_points[i]->v->x = temp_inner_points0[i]->v->x + temp_inner_points2[i]->v->x; // Change in velocity is added to the velocity. + object->inner_points[i]->v->y = temp_inner_points0[i]->v->y + temp_inner_points2[i]->v->y; + object->inner_points[i]->v->z = temp_inner_points0[i]->v->z + temp_inner_points2[i]->v->z; + + object->inner_points[i]->r->x = temp_inner_points0[i]->r->x + temp_inner_points2[i]->r->x; // Change in position is velocity times the change in time + object->inner_points[i]->r->y = temp_inner_points0[i]->r->y + temp_inner_points2[i]->r->y; + object->inner_points[i]->r->z = temp_inner_points0[i]->r->z + temp_inner_points2[i]->r->z; + + + object->outer_points[i]->dv->x = temp_outer_points2[i]->dv->x; + object->outer_points[i]->dv->y = temp_outer_points2[i]->dv->y; + object->outer_points[i]->dv->z = temp_outer_points2[i]->dv->z; + + object->outer_points[i]->dr->x = temp_outer_points2[i]->dr->x; // Change in position is velocity times the change in time + object->outer_points[i]->dr->y = temp_outer_points2[i]->dr->y; + object->outer_points[i]->dr->z = temp_outer_points2[i]->dr->z; + + object->outer_points[i]->v->x = temp_outer_points0[i]->v->x + temp_outer_points2[i]->v->x; // Change in velocity is added to the velocity. + object->outer_points[i]->v->y = temp_outer_points0[i]->v->y + temp_outer_points2[i]->v->y; + object->outer_points[i]->v->z = temp_outer_points0[i]->v->z + temp_outer_points2[i]->v->z; + + object->outer_points[i]->r->x = temp_outer_points0[i]->r->x + temp_outer_points2[i]->r->x; // Change in position is velocity times the change in time + object->outer_points[i]->r->y = temp_outer_points0[i]->r->y + temp_outer_points2[i]->r->y; + object->outer_points[i]->r->z = temp_outer_points0[i]->r->z + temp_outer_points2[i]->r->z; +} diff --git a/MidpointIntegrator.h b/MidpointIntegrator.h new file mode 100644 index 0000000..7e7d0f4 --- /dev/null +++ b/MidpointIntegrator.h @@ -0,0 +1,23 @@ +#ifndef MIDPOINTINTEGRATOR_H +#define MIDPOINTINTEGRATOR_H + +#include "EulerIntegrator.h" + + +class MidpointIntegrator : public EulerIntegrator +{ +public: + + MidpointIntegrator(Object&); // constructor + + ~MidpointIntegrator(); // destructor + +protected: + + + void k2(int, float, float);// 2nd integrate to get the derivatives of velocity and position + + void ynew(int, float, float);// get the new velocity and position +}; + +#endif /* MIDPOINTINTEGRATOR_H */ diff --git a/Object.cpp b/Object.cpp new file mode 100644 index 0000000..0ef338d --- /dev/null +++ b/Object.cpp @@ -0,0 +1,121 @@ +#include + +#include "Object.h" +#include "EulerIntegrator.h" +#include "MidpointIntegrator.h" +#include "RungeKutta4Integrator.h" + +//##ModelId=45F4D7970263 +Object::Object() +{ + numParticles = 2; //default particle number on the object + numSprings = 1; //default spring number on the object + integrator = NULL; //default integrator + integratorType = EULER; //default integrator tpye is Euler + dim = DIM1D; //default object is 1D + closest_i=0; // the point which is closest to mouse position +} + +//##ModelId=45F4D7970264 +Object::~Object() +{ + + delete integrator; +} + +//========================================================================== + +//##ModelId=45F4D7970293 +void Object::SetParticles() //model the particles on the object +{ + + assert(false); +} + +//##ModelId=45F4D7970297 +void Object::Add_Structural_Spring(int index, int head, int tail) +{ + assert(false); + +} + +//############################################################################## + +//##ModelId=45F4D7970295 +void Object::SetObject() //model the object +{ + + SetParticles(); //model the particles on the object + +} + +//############################################################################# + +//##ModelId=45F4D7970268 +void Object::setIntegratorType(integrator_type type) //set the integrator tpye as user selects from the GUI +{ + + this->integratorType = type; +} + +//############################################################################# +//update the forces, velocity, and position of the particle on the object + +//##ModelId=45F4D7970274 +void Object::Update(float deltaT, bool drag, float xDrag, float yDrag) +{ + + if(integrator == NULL) + { + + switch(integratorType) + { + case EULER: + integrator = new EulerIntegrator(*this); + break; + + case MIDPOINT: + integrator = new MidpointIntegrator(*this); + break; + + case RK4: + integrator = new RungeKutta4Integrator(*this); + break; + + default: + assert(false); + return; + } + + integrator->setDimension(dim); + } + + integrator->integrate(deltaT, drag, xDrag, yDrag); +} + + +/////////////////////////////////////////////////////////// + +//##ModelId=45F4D7970286 +void Object::FindClosestPoint(void) +{ + float dmin = 0; + float mousepointd = 0; + int i; + + // find closest point + dmin = sqrt(pow(outer_points[closest_i]->r->x - integrator->mDragX,2) + pow(outer_points[closest_i]->r->y - integrator->mDragY,2)); + + for(i=0; ir->x - integrator->mDragX,2) + + pow(outer_points[i]->r->y - integrator->mDragY,2)); + if(mousepointd < dmin) + { + dmin = mousepointd; + closest_i = i; + } + } + + //cout<<" closest_i=="<< closest_i< +using namespace std; + +class Integrator; + +//##ModelId=45F4D79701E6 +class Object +{ +protected: + //##ModelId=45F4D79701E7 + int numParticles; // number of particles of the object + //##ModelId=45F4D79701F5 + int numSprings; // number of springs of the object + //##ModelId=45F4D79701F7 + Integrator* integrator; // integrator object + //##ModelId=45F4D7970206 + integrator_type integratorType; // different type of integrator, Euler, Midpoint, RK4 + +public: + //##ModelId=45F4D797020B + vector inner_springs; // the springs on the inner layer of the object + //##ModelId=45F4D7970216 + vector outer_springs; // the springs on the outer layer of the object + //##ModelId=45F4D7970227 + vector inner_points; // the particles on the inner layer of the object + //##ModelId=45F4D7970235 + vector outer_points; // the particles on the outer layer of the object + //##ModelId=45F4D7970246 + vector inner_faces; // the surfaces on the inner layer of the object + //##ModelId=45F4D797024B + vector outer_faces; // the surfaces on the outer layer of the object + //##ModelId=45F4D7970254 + dimensionality dim; // dimension type to specify the object is 1D, 2D, or 3D + + //##ModelId=45F4D7970258 + int closest_i ; // the closest point on the outer layer to mouse position + +public: + //##ModelId=45F4D7970263 + Object(); // object constructor + //##ModelId=45F4D7970264 + virtual ~Object(); // object destructor + + //##ModelId=45F4D7970266 + int GetNumberOfParticles() { return numParticles; } // get the number of particles of the object + //##ModelId=45F4D7970267 + int GetNumberOfSprings() { return numSprings; } // get the number of springs of the object + + //##ModelId=45F4D7970268 + void setIntegratorType(integrator_type type); // choose the integrator + //##ModelId=45F4D7970273 + Integrator* getIntegrator() {return integrator;} // get the integrator type + + //##ModelId=45F4D7970274 + virtual void Update(float, bool = false, float = 0, float = 0); //update the object about forces, velocity, position + + //##ModelId=45F4D7970284 + virtual void Draw(int, glm::mat4) = 0; // display the object + //##ModelId=45F4D7970286 + virtual void FindClosestPoint(void) ; // find the closet particle on th outer layer to mouse position + +protected: + //##ModelId=45F4D7970293 + virtual void SetParticles(); // model the particles on the object + //##ModelId=45F4D7970295 + virtual void SetObject(); // model the object + //##ModelId=45F4D7970297 + virtual void Add_Structural_Spring(int, int, int); // construct the springs to model the object +}; + +#endif /* OBJECT_H */ diff --git a/Object1D.cpp b/Object1D.cpp new file mode 100644 index 0000000..582c634 --- /dev/null +++ b/Object1D.cpp @@ -0,0 +1,185 @@ +#include "Object1D.h" +#include +//##ModelId=45F4D79701B9 +Object1D::Object1D() +{ + SetObject(); + dim = DIM1D; + // added + createVertexArray(); +} + + +//##ModelId=45F4D79701BA +Object1D::~Object1D() +{ +} + +void Object1D::createVertexArray(){ + glGenVertexArrays(1, &VAO); + glBindVertexArray(VAO); + + glGenBuffers(1, &VBO); + glBindBuffer(GL_ARRAY_BUFFER, VBO); + glBufferData(GL_ARRAY_BUFFER, sizeof(vertexArrayData), vertexArrayData, GL_DYNAMIC_DRAW); + + // set attribute + glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(glm::vec3), (void*)0); + glEnableVertexAttribArray(0); + + glBindBuffer(GL_ARRAY_BUFFER, 0); + glBindVertexArray(0); +} + +void Object1D::updateVertexArray(){ + // update the vertex array and upload the data to buffer + glBindBuffer(GL_ARRAY_BUFFER, VBO); + // the actual object + vertexArrayData[0] = glm::vec3(outer_springs[0]->sp1->r->x,outer_springs[0]->sp1->r->y, 0.0f); + vertexArrayData[1] = glm::vec3(outer_springs[0]->sp2->r->x,outer_springs[0]->sp2->r->y, 0.0f); + + // integrator point + if(integrator != nullptr){ + vertexArrayData[2] = glm::vec3(integrator->mDragX, integrator->mDragY, 0.0f); + vertexArrayData[3] = glm::vec3(outer_points[closest_i]->r->x,outer_points[closest_i]->r->y, 0.0f); + } + + glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(vertexArrayData), vertexArrayData); +// glBufferData(GL_ARRAY_BUFFER, sizeof(vertexArrayData), (void*)vertexArrayData, GL_DYNAMIC_DRAW); + +} + +//##ModelId=45F4D79701C7 +void Object1D::Draw(int shaderID, glm::mat4 worldMatrix) +{ + static bool findOnce = false; + + + // draw object1D itself (one line and two points) + + // update the buffer data + updateVertexArray(); + glBindVertexArray(VAO); +// glBindBuffer(GL_ARRAY_BUFFER, VBO); + // set world matrix + glUniformMatrix4fv(glGetUniformLocation(shaderID, "worldMatrix"), 1, GL_FALSE, &worldMatrix[0][0]); + // get color location + GLuint ColorLocation = glGetUniformLocation(shaderID, "colorChoice"); + glDisable(GL_CULL_FACE); + // set color and draw + // draw line + glEnableClientState(GL_VERTEX_ARRAY); + glUniform4f(ColorLocation, 1.0f, 1.0f, 1.0f, 1.0f); + glDrawArrays(GL_LINES, 0, 2); + // draw points + glPointSize(10.0f); + glDrawArrays(GL_POINTS, 0, 2); + + + if(integrator != nullptr) + { + if(integrator->dragExists) + { + if(findOnce == true) + { + FindClosestPoint(); + findOnce = false; + } + glUniform4f(ColorLocation, 1.0f, 1.0f, 0.0f, 1.0f); + glDrawArrays(GL_LINES, 2, 2); + } + else + { + FindClosestPoint(); + findOnce = true; + } + + } + + + + glBindVertexArray(0); + glBindBuffer(GL_ARRAY_BUFFER, 0); +// glEnable(GL_CULL_FACE); + +} + +//==================================================================== + +//##ModelId=45F4D79701C9 +void Object1D::Add_Structural_Spring(int index, int head, int tail) +{ + + outer_springs.push_back( new Spring(outer_points[head],outer_points[tail])); + inner_springs.push_back( new Spring()); + + /* outer_springs[index].sp1 = &outer_points[head]; + outer_springs[index].sp2 = &outer_points[tail]; + + inner_springs[index].sp1 = new Particle(); + inner_springs[index].sp2 = new Particle(); + + outer_springs[index].setRestLen(); + */ + inner_springs[index]->setRestLen(); + outer_springs[index]->setRestLen(); +} + + +//================================================================ +//##ModelId=45F4D79701CD +void Object1D::SetObject() +{ + + + int PosX = 0; + int PosY = 0; + int i; +// cout<<"numParticles---1D------"<x += PosX ; // outer X coordiation + outer_points[i].r->y += PosY; // outer Y coordiation + outer_points[i].mass = MASS; + */ + PosY += 2; + } + + + for(i=0; i<1 ;i++) // NUMP-1 springs from 1st to the NUMP for outer & inner + { + Add_Structural_Spring(i, i, (i+1) % numParticles); + } +} + + +//======================================================= +/* +void Object1D::FindClosestPoint(void) +{ + float dmin = 0; + float mousepointd = 0; + int i; + + // find closest point + dmin = sqrt(pow(outer_points[closest_i]->r->x - integrator->mDragX,2) + pow(outer_points[closest_i]->r->y - integrator->mDragY,2)); + + for(i=0; ir->x - integrator->mDragX,2) + + pow(outer_points[i]->r->y - integrator->mDragY,2)); + if(mousepointd < dmin) + { + dmin = mousepointd; + closest_i = i; + } + } + + cout<<" closest_i=="<< closest_i< tempVertices; + // push in the outer circle lines data + for(int i = 0; i < outer_faces.size(); ++i){ + tempVertices.push_back(glm::vec3(outer_faces[i]->fp1->r->x, outer_faces[i]->fp1->r->y, outer_faces[i]->fp1->r->z)); + tempVertices.push_back(glm::vec3((outer_faces[i]->normal->x + outer_faces[i]->fp1->r->x), + (outer_faces[i]->normal->y + outer_faces[i]->fp1->r->y), + (outer_faces[i]->normal->z + outer_faces[i]->fp1->r->z))); + } + + // push in the outer polygon data + for(int i = 0; i < outer_points.size(); i++) + { + tempVertices.push_back(glm::vec3(outer_points[i]->r->x, outer_points[i]->r->y, 0.0f)); + } + + // push in inner polygon data + for(int i = 0; i < inner_points.size(); ++i) + { + tempVertices.push_back(glm::vec3(inner_points[i]->r->x, inner_points[i]->r->y, 0.0f)); + } + + // push in outer spring data + for(int i = 0; i < outer_springs.size(); ++i){ + tempVertices.push_back(glm::vec3(outer_springs[i]->sp1->r->x,outer_springs[i]->sp1->r->y, 0.0f)); + tempVertices.push_back(glm::vec3(outer_springs[i]->sp2->r->x,outer_springs[i]->sp2->r->y, 0.0f)); + } + + // push in inner spring data + for(int i = 0; i < inner_springs.size(); ++i){ + tempVertices.push_back(glm::vec3(inner_springs[i]->sp1->r->x,inner_springs[i]->sp1->r->y, 0.0f)); + tempVertices.push_back(glm::vec3(inner_springs[i]->sp2->r->x,inner_springs[i]->sp2->r->y, 0.0f)); + } + + // push in radium spring data + for(int i = 0; i < radium_springs.size(); ++i){ + tempVertices.push_back(glm::vec3(radium_springs[i]->sp1->r->x,radium_springs[i]->sp1->r->y, 0.0f)); + tempVertices.push_back(glm::vec3(radium_springs[i]->sp2->r->x,radium_springs[i]->sp2->r->y, 0.0f)); + } + + // push in shear spring left data + for(int i = 0; i < shear_springs_left.size(); ++i){ + tempVertices.push_back(glm::vec3(shear_springs_left[i]->sp1->r->x,shear_springs_left[i]->sp1->r->y, 0.0f)); + tempVertices.push_back(glm::vec3(shear_springs_left[i]->sp2->r->x,shear_springs_left[i]->sp2->r->y, 0.0f)); + } + + // push in shear spring right data + for(int i = 0; i < shear_springs_right.size(); ++i){ + tempVertices.push_back(glm::vec3(shear_springs_right[i]->sp1->r->x,shear_springs_right[i]->sp1->r->y, 0.0f)); + tempVertices.push_back(glm::vec3(shear_springs_right[i]->sp2->r->x,shear_springs_right[i]->sp2->r->y, 0.0f)); + } + + // push in integrator line data + if(integrator != nullptr){ + tempVertices.push_back(glm::vec3(integrator->mDragX, integrator->mDragY, 0.0f)); + tempVertices.push_back(glm::vec3(outer_points[closest_i]->r->x, outer_points[closest_i]->r->y, 0.0f)); + } + + + // copy the vector to vertexArrayData + for(unsigned int i = 0; i < total; ++i) + { + // this if statement is to make sure the vertexArrayData's size is unchanged and every vertex is initialized + // (in case integrator is null) + if(i < tempVertices.size()) + vertexArrayData[i] = tempVertices.at(i); + else + vertexArrayData[i] = glm::vec3(0); + } + + // update the buffer data + glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(*vertexArrayData) * total, (void*)vertexArrayData); +} + +int Object2D::getOffset(drawing_instances instance){ + int offset = 0; + + switch(instance){ + case outer_line: + offset = 0; + break; + case outer_polygon: + offset = (int) outer_faces.size() * 2; + break; + case inner_polygon: + offset = getOffset(outer_polygon) + getCount(outer_polygon); + break; + case outer_spring: + offset = getOffset(inner_polygon) + getCount(inner_polygon); + break; + case inner_spring: + offset = getOffset(outer_spring) + getCount(outer_spring); + break; + case radium_spring: + offset = getOffset(inner_spring) + getCount(inner_spring); + break; + case shear_left: + offset = getOffset(radium_spring) + getCount(radium_spring); + break; + case shear_right: + offset = getOffset(shear_left) + getCount(shear_left); + break; + case integrator_line: + offset = getOffset(shear_right) + getCount(shear_right); + break; + default: + offset = 0; + break; + + } + return offset; +} + +int Object2D::getCount(drawing_instances instance){ + int vertexCount = 0; + + switch(instance){ + case outer_line: + vertexCount = (int) outer_faces.size()*2; + break; + case outer_polygon: + vertexCount = (int) outer_points.size(); + break; + case inner_polygon: + vertexCount = (int) inner_points.size(); + break; + case outer_spring: + vertexCount = (int) outer_springs.size() * 2; + break; + case inner_spring: + vertexCount = (int) inner_springs.size() * 2; + break; + case radium_spring: + vertexCount = (int) radium_springs.size() * 2; + break; + case shear_left: + vertexCount = (int) shear_springs_left.size() * 2; + break; + case shear_right: + vertexCount = (int) shear_springs_right.size() * 2; + break; + case integrator_line: + vertexCount = 2; + break; + default: + vertexCount = 0; + break; + + } + return vertexCount; +} + +//##ModelId=45F4D797017A +void Object2D::Draw(int shaderID, glm::mat4 worldMatrix) +{ + static bool findOnce = false; + + // =============== basic settings for ogl3 drawing + updateVertexArray(); + glBindVertexArray(VAO); +// glBindBuffer(GL_ARRAY_BUFFER, VBO); + // set world matrix + glUniformMatrix4fv(glGetUniformLocation(shaderID, "worldMatrix"), 1, GL_FALSE, &worldMatrix[0][0]); + // get color location + GLuint ColorLocation = glGetUniformLocation(shaderID, "colorChoice"); + glEnableClientState(GL_VERTEX_ARRAY); + glPointSize(10); + // ==================================================== + + // drawing outer lines + glUniform4f(ColorLocation, 0.5f, 0.2f, 0.5f, 0.7f); + glDrawArrays(GL_LINES, getOffset(outer_line), getCount(outer_line)); + + // drawing outer polygon + glDrawArrays(GL_TRIANGLE_FAN, getOffset(outer_polygon), getCount(outer_polygon)); + + // drawing inner polygon + glUniform4f(ColorLocation, 0.0f, 1.0f, 0.0f, 0.5f); + glDrawArrays(GL_TRIANGLE_FAN, getOffset(inner_polygon), getCount(inner_polygon)); + + // drawing outer springs + // (line) + glUniform4f(ColorLocation, 0.2f, 0.2f, 1.0f, 1.0f); + glDrawArrays(GL_LINES, getOffset(outer_spring), getCount(outer_spring)); + // (point) - need to be drawn using a loop to draw only one point, also the first point need to be red, other yellow + for(int i = 0; i < outer_springs.size(); ++i) + { + if(i == 0) + glUniform4f(ColorLocation, 1.0f, 0.0f, 0.0f, 1.0f); + else + glUniform4f(ColorLocation, 1.0f, 1.0f, 0.0f, 1.0f); + + glDrawArrays(GL_POINTS, getOffset(outer_spring) + i*2, 1); + } + + // drawing inner springs + // (line) + glUniform4f(ColorLocation, 0.2f, 0.2f, 1.0f, 1.0f); + glDrawArrays(GL_LINES, getOffset(inner_spring), getCount(inner_spring)); + // (point) + glUniform4f(ColorLocation, 1.0f, 0.0f, 0.0f, 1.0f); + glDrawArrays(GL_POINTS, getOffset(inner_spring), getCount(inner_spring)); + + // drawing radium springs + glUniform4f(ColorLocation, 0.0f, 1.0f, 0.0f, 1.0f); + glDrawArrays(GL_LINES, getOffset(radium_spring), getCount(radium_spring)); + + // drawing shear spring left + glUniform4f(ColorLocation, 1.0f, 0.0f, 0.0f, 1.0f); + glDrawArrays(GL_LINES, getOffset(shear_left), getCount(shear_left)); + + // drawing shear spring right + glUniform4f(ColorLocation, 0.0f, 0.0f, 1.0f, 1.0f); + glDrawArrays(GL_LINES, getOffset(shear_right), getCount(shear_right)); + + + + if(integrator != nullptr){ + if(integrator->dragExists) + { + if(findOnce == true) + { + FindClosestPoint(); + findOnce = false; + } + // draw integrator + glUniform4f(ColorLocation, 1.0f, 1.0f, 0.0f, 1.0f); + glDrawArrays(GL_LINES, getOffset(integrator_line), getCount(integrator_line)); + } + else + { + FindClosestPoint(); + findOnce = true; + } + } + + +// glEnable(GL_DEPTH_TEST); +// glEnable(GL_CULL_FACE); + + glBindVertexArray(0); +// glBindBuffer(GL_ARRAY_BUFFER, 0); +} + +/* Function of adding a new spring on the inner rings*/ + +//##ModelId=45F4D797018A +void Object2D::Add_Structural_Spring(int index, int h, int t) +{ + + inner_springs.push_back( new Spring(inner_points[h],inner_points[t])); + outer_springs.push_back( new Spring(outer_points[h],outer_points[t])); + + inner_springs[index]->setRestLen(); //set the inner spring's length + outer_springs[index]->setRestLen(); //set the outer spring's length +} + + +//##ModelId=45F4D7970198 +void Object2D::Add_Radium_Spring(int index) +{ + + radium_springs.push_back( new Spring(inner_points[index],outer_points[index])); + radium_springs[index]->setRestLen(); //set the radium spring's length +} + + +//##ModelId=45F4D797019A +void Object2D::Add_Shear_Spring(int index, int h, int t ) +{ + + + shear_springs_left.push_back(new Spring(inner_points[h],outer_points[t])); + shear_springs_right.push_back(new Spring(inner_points[t],outer_points[h])); + + + shear_springs_left[index]->setRestLen(); //set the shear left spring's length + shear_springs_right[index]->setRestLen(); //set the shear right spring's length + +} + +/* Create 2 2D-rings (points + springs) */ +//##ModelId=45F4D797017F +void Object2D::SetObject(void) +{ + float tix, tiy, tiz, tox,toy, toz; + + tix=tiy=tiz=tox=toy=toz=0; + outer_points.clear(); + inner_points.clear(); + outer_springs.clear(); + inner_springs.clear(); + outer_faces.clear(); + inner_faces.clear(); + int i; + for(i=0; iCalNormalNField(); + + outer_faces[i]->CalNormalNField(); +// cout<<"outer_faces[i]->normal->x==="<normal->x<<"=== outer_faces[i]->normal->y==="<normal->y<<"===outer_faces[i]->normal->z===="<normal->z<fp1->r->x=="<fp1->r->x<<"==outer_faces[0]->fp1->r->y=="<fp1->r->y<<"==outer_faces[0]->fp1->r->z=="<fp1->r->z<fp2->r->x=="<fp2->r->x<<"==outer_faces[0]->fp2->r->y=="<fp2->r->y<<"==outer_faces[0]->fp2->r->z=="<fp2->r->z<fp3->r->x=="<fp3->r->x<<"==outer_faces[0]->fp3->r->y=="<fp3->r->y<<"==outer_faces[0]->fp3->r->z=="<fp3->r->z<CalNormalNField(); + cout<<"outer_faces[0]->normal->x==="<normal->x<<"=== outer_faces[0]->normal->y==="<normal->y<<"===outer_faces[0]->normal->z===="<normal->z< radium_springs; // for radium springs between two circles + //##ModelId=45F4D7970160 + vector shear_springs_left; // for shear spring toward left + //##ModelId=45F4D797016A + vector shear_springs_right; // for right spring toward right + +public: + + //##ModelId=45F4D797016E + Object2D(); // constructor object2D + //##ModelId=45F4D7970178 + virtual ~Object2D(); // destructor object2D + + //##ModelId=45F4D797017A + virtual void Draw(int shaderID, glm::mat4 worldMatrix); // display the object 2D + //##ModelId=45F4D797017C + inline void setPressure(float pre){pressure = pre;} // Set Pressure + //##ModelId=45F4D797017E + inline float getPressure() {return(pressure);} // Get Pressure + +private: + //##ModelId=45F4D797017F + virtual void SetObject(void); // model the object2D + //##ModelId=45F4D797018A + void Add_Structural_Spring(int index, int h, int t); // model the inner or outer cirle with structural springs + //##ModelId=45F4D7970198 + void Add_Radium_Spring(int index); // add the radium springs with inner point i and outer point i + //##ModelId=45F4D797019A + void Add_Shear_Spring(int index, int h, int t ); // add the left shear springs with inner point i and outer point i+1 //add the right shear springs with inner point i+1 and outer point i + //##ModelId=45F4D79701A7 + virtual void Add_Faces(int i, int i1, int imid); // add the triangle surface to object2D + + // for OGL3 drawing + void createVertexArray(); + glm::vec3* vertexArrayData; + void updateVertexArray(); + unsigned int total; + GLuint VAO; + GLuint VBO; + + // other helper functions to get the offset of drawing instances + enum drawing_instances{outer_line, outer_polygon, inner_polygon, outer_spring, inner_spring, radium_spring, shear_left, shear_right, integrator_line}; + + int getOffset(drawing_instances instance); + int getCount(drawing_instances instance); + + +}; + +#endif /* OBJECT2D_H */ diff --git a/Object3D.cpp b/Object3D.cpp new file mode 100644 index 0000000..143923e --- /dev/null +++ b/Object3D.cpp @@ -0,0 +1,1083 @@ +#include "Object3D.h" + +#include + +//##ModelId=45F4D79700EE +Object3D::Object3D() +{ //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +/* pa = new Particle(new Vector(0,0,0), MASS); + pb = new Particle(new Vector(0,0,0), MASS); + pc = new Particle(new Vector(0,0,0), MASS); +*/ + //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + + + // It is *IMPORTANT* to realize that most of the time + // the number of particles and springs are not the same + // in 3D (unlike 2D) so these have to be updated to the + // actual number of particles and springs when the geometry + // is initially constructed or altered + numParticles = NUMPOINTS; + numSprings = NUMSPRINGS; + + iterations = 1; + SetObject(); + + dim = DIM3D; +} + + + +//##ModelId=45F4D79700FB +Object3D::~Object3D() +{ +/* delete pa; + delete pb; + delete pc; +*/ +} + +void Object3D::createVertexArray(){ + total = 0; + // blue outer points + total += outer_points.size(); // for drawing outer points + total += inner_points.size(); // for drawing inner points + + // TODO: can add (outer_spring), (inner_spring), (radium_spring), (shear_left), (shear_right) these springs here. + // but also need to change (drawing type enum, update data function, and drawing function to correctly draw these) + + total += inner_faces.size() * 3;// for drawing inner faces + total += outer_faces.size() * 3;// for drawing outer faces + + // TODO: can add inner face normals here + + total += outer_faces.size() * 12; // for drawing outer faces normals + total += 2; // for drawing integrator line + + // dynamically allocate the vertexArrayData size by counting how many vertercies in total + vertexArrayData = new glm::vec3[total]; + + // generate VAO and VBO, upload the data, reserve memory in buffer + glGenVertexArrays(1, &VAO); + glBindVertexArray(VAO); + + glGenBuffers(1, &VBO); + glBindBuffer(GL_ARRAY_BUFFER, VBO); + glBufferData(GL_ARRAY_BUFFER, sizeof(*vertexArrayData) * total, (void*)vertexArrayData, GL_DYNAMIC_DRAW); + + // set attribute + glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(glm::vec3), (void*)0); + glEnableVertexAttribArray(0); + + glBindBuffer(GL_ARRAY_BUFFER, 0); + glBindVertexArray(0); +// cout<<"3D: " << VAO << " " << VBO << endl; +} + +void Object3D::updateVertexArray(){ + + glBindBuffer(GL_ARRAY_BUFFER, VBO); + vector tempVertices; + + // push in data for outer points + for(int i = 0; i < outer_points.size(); ++i){ + tempVertices.push_back(glm::vec3(outer_points[i]->r->x, outer_points[i]->r->y, outer_points[i]->r->z)); + } + + // push in data for inner points + for(int i = 0; i < inner_points.size(); ++i){ + tempVertices.push_back(glm::vec3(inner_points[i]->r->x, inner_points[i]->r->y, inner_points[i]->r->z)); + } + + // push in data for inner faces + for(int i = 0; i < inner_faces.size(); ++i){ + tempVertices.push_back(glm::vec3(inner_faces[i]->fp1->r->x, inner_faces[i]->fp1->r->y, inner_faces[i]->fp1->r->z)); + tempVertices.push_back(glm::vec3(inner_faces[i]->fp2->r->x, inner_faces[i]->fp2->r->y, inner_faces[i]->fp2->r->z)); + tempVertices.push_back(glm::vec3(inner_faces[i]->fp3->r->x, inner_faces[i]->fp3->r->y, inner_faces[i]->fp3->r->z)); + } + + // push in data for outer faces + for(int i = 0; i < outer_faces.size(); ++i){ + tempVertices.push_back(glm::vec3(outer_faces[i]->fp1->r->x, outer_faces[i]->fp1->r->y, outer_faces[i]->fp1->r->z)); + tempVertices.push_back(glm::vec3(outer_faces[i]->fp2->r->x, outer_faces[i]->fp2->r->y, outer_faces[i]->fp2->r->z)); + tempVertices.push_back(glm::vec3(outer_faces[i]->fp3->r->x, outer_faces[i]->fp3->r->y, outer_faces[i]->fp3->r->z)); + } + + // push in data for outer face normals + for(int i = 0; i < outer_faces.size(); ++i){ + tempVertices.push_back(glm::vec3(outer_faces[i]->fp1->r->x, outer_faces[i]->fp1->r->y, outer_faces[i]->fp1->r->z)); + tempVertices.push_back(glm::vec3(outer_faces[i]->fp2->r->x, outer_faces[i]->fp2->r->y, outer_faces[i]->fp2->r->z)); + + tempVertices.push_back(glm::vec3(outer_faces[i]->fp2->r->x, outer_faces[i]->fp2->r->y, outer_faces[i]->fp2->r->z)); + tempVertices.push_back(glm::vec3(outer_faces[i]->fp3->r->x, outer_faces[i]->fp3->r->y, outer_faces[i]->fp3->r->z)); + + tempVertices.push_back(glm::vec3(outer_faces[i]->fp3->r->x, outer_faces[i]->fp3->r->y, outer_faces[i]->fp3->r->z)); + tempVertices.push_back(glm::vec3(outer_faces[i]->fp1->r->x, outer_faces[i]->fp1->r->y, outer_faces[i]->fp1->r->z)); + + tempVertices.push_back(glm::vec3(inner_faces[i]->fp1->r->x, inner_faces[i]->fp1->r->y, inner_faces[i]->fp1->r->z)); + tempVertices.push_back(glm::vec3(inner_faces[i]->fp2->r->x, inner_faces[i]->fp2->r->y, inner_faces[i]->fp2->r->z)); + + tempVertices.push_back(glm::vec3(inner_faces[i]->fp2->r->x, inner_faces[i]->fp2->r->y, inner_faces[i]->fp2->r->z)); + tempVertices.push_back(glm::vec3(inner_faces[i]->fp3->r->x, inner_faces[i]->fp3->r->y, inner_faces[i]->fp3->r->z)); + + tempVertices.push_back(glm::vec3(inner_faces[i]->fp3->r->x, inner_faces[i]->fp3->r->y, inner_faces[i]->fp3->r->z)); + tempVertices.push_back(glm::vec3(inner_faces[i]->fp1->r->x, inner_faces[i]->fp1->r->y, inner_faces[i]->fp1->r->z)); + } + + // push in integrator line data + if(integrator != nullptr){ + tempVertices.push_back(glm::vec3(integrator->mDragX, integrator->mDragY, 0.0f)); + tempVertices.push_back(glm::vec3(outer_points[closest_i]->r->x,outer_points[closest_i]->r->y,outer_points[closest_i]->r->z)); + } + + // copy the vector to vertexArrayData + for(unsigned int i = 0; i < total; ++i) + { + // this if statement is to make sure the vertexArrayData's size is unchanged and every vertex is initialized + // (in case integrator is null) + if(i < tempVertices.size()) + vertexArrayData[i] = tempVertices.at(i); + else + vertexArrayData[i] = glm::vec3(0); + } + + // update the buffer data + glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(*vertexArrayData) * total, (void*)vertexArrayData); + +} + +int Object3D::getOffset(drawingInstances instance){ + int offset = 0; + + switch(instance){ + case outer_inner_points: + offset = 0; + break; + case outer_face: + offset = getCount(outer_inner_points); + break; + case inner_face: + offset = getOffset(outer_face) + getCount(outer_face); + break; + case outer_face_normal: + offset = getOffset(inner_face) + getCount(inner_face); + break; + case integrator_line: + offset = getOffset(outer_face_normal) + getCount(outer_face_normal); + break; + default: + offset = 0; + break; + } + return offset; +} + +int Object3D::getCount(drawingInstances instance){ + int vertexCount = 0; + + switch(instance){ + case outer_inner_points: + vertexCount = (int) outer_points.size() + (int)inner_points.size(); + break; + case outer_face: + vertexCount = (int) outer_faces.size() * 3; + break; + case inner_face: + vertexCount = (int) inner_faces.size() * 3; + break; + case outer_face_normal: + vertexCount = (int) outer_faces.size() * 12; + break; + case integrator_line: + vertexCount = 2; + break; + default: + vertexCount = 0; + break; + + } + return vertexCount; +} + +//##ModelId=45F4D797010B +void Object3D::Add_Structural_Spring(int index, int h, int t) +{ + // making sure nobody actuall calls it +/* assert(false); + + inner_springs.push_back( new Spring(inner_points[h],inner_points[t])); + outer_springs.push_back( new Spring(outer_points[h],outer_points[t])); + +*/ + +/* inner_springs.push_back( new Spring()); + outer_springs.push_back( new Spring()); +*/ +/* inner_springs[index]->setRestLen(); //set the inner spring's length + outer_springs[index]->setRestLen(); //set the outer spring's length +*/ +} + + +//////////////////////////////////////////////////// + + + +//##ModelId=45F4D797010F +void Object3D::Add_Radium_Spring(int index) +{ + radium_springs.push_back( new Spring(inner_springs[index]->sp1,outer_springs[index]->sp1, 5*KS, 2*KD)); + radium_springs[index]->setRestLen(); //set the radium spring's length +} + + +//##ModelId=45F4D797011B +void Object3D::Add_Shear_Spring(int index ) +{ + shear_springs_left.push_back(new Spring(inner_springs[index]->sp1,outer_springs[index]->sp2, 5*KS, 2*KD)); + shear_springs_right.push_back(new Spring(inner_springs[index]->sp2, outer_springs[index]->sp1,5*KS, 2*KD)); + + shear_springs_left[index]->setRestLen(); //set the shear left spring's length + shear_springs_right[index]->setRestLen(); //set the shear right spring's length +} + + + + + +//============================================================================== +//##ModelId=45F4D79700FF +void Object3D::Iteration() +{ + // Spring triplets counter for older springs particle replacements +// static int j = 0; + + for (int it=0; itr->x=(outer_faces[i]->fp1->r->x + outer_faces[i]->fp2->r->x)/2; + pa->r->y=(outer_faces[i]->fp1->r->y + outer_faces[i]->fp2->r->y)/2; + pa->r->z=(outer_faces[i]->fp1->r->z + outer_faces[i]->fp2->r->z)/2; + + pb->r->x=(outer_faces[i]->fp2->r->x + outer_faces[i]->fp3->r->x)/2; + pb->r->y=(outer_faces[i]->fp2->r->y + outer_faces[i]->fp3->r->y)/2; + pb->r->z=(outer_faces[i]->fp2->r->z + outer_faces[i]->fp3->r->z)/2; + + pc->r->x=(outer_faces[i]->fp1->r->x + outer_faces[i]->fp3->r->x)/2; + pc->r->y=(outer_faces[i]->fp1->r->y + outer_faces[i]->fp3->r->y)/2; + pc->r->z=(outer_faces[i]->fp1->r->z + outer_faces[i]->fp3->r->z)/2; + // + + /* cout<<"before normalization: "<x="<r->x<<"; pa->y="<r->y<<"; pa->z="<r->z<x="<r->x<<"; pb->y="<r->y<<"; pb->z="<r->z<x="<r->x<<"; pc->y="<r->y<<"; pc->z="<r->z<r->Normalize(); + pb->r->Normalize(); + pc->r->Normalize(); + + /* cout<<"after normalization: "<x="<r->x<<"; pa->y="<r->y<<"; pa->z="<r->z<x="<r->x<<"; pb->y="<r->y<<"; pb->z="<r->z<x="<r->x<<"; pc->y="<r->y<<"; pc->z="<r->z<r->x == pa->r->x + && outer_points[o]->r->y == pa->r->y + && outer_points[o]->r->z == pa->r->z + ) + { + delete pa; + pa = outer_points[o]; + a = true; + } + + if + ( + outer_points[o]->r->x == pb->r->x + && outer_points[o]->r->y == pb->r->y + && outer_points[o]->r->z == pb->r->z + ) + { + delete pb; + pb = outer_points[o]; + b = true; + } + + if + ( + outer_points[o]->r->x == pc->r->x + && outer_points[o]->r->y == pc->r->y + && outer_points[o]->r->z == pc->r->z + ) + { + delete pc; + pc = outer_points[o]; + c = true; + } + } + + // The new points are added to the general collection of points + if(!a) outer_points.push_back(pa); + if(!b) outer_points.push_back(pb); + if(!c) outer_points.push_back(pc); + + + // Create three new faces; these represent the three outer triangles + outer_faces.push_back(new Face(outer_faces[i]->fp1, pa, pc, outer_springs)); // Top + outer_faces.push_back(new Face(pa, outer_faces[i]->fp2, pb, outer_springs)); // Bottom-left +// outer_faces.push_back(new Face(pb, outer_faces[i]->fp3, pc, outer_springs)); // Bottom-right + outer_faces.push_back(new Face(pc, pb, outer_faces[i]->fp3, outer_springs)); // Bottom-right + + + // Replace the original face's points with its new one in the middle + + outer_faces[i]->fp1 = pa; + outer_faces[i]->fp2 = pb; + outer_faces[i]->fp3 = pc; + + + outer_faces[i]->fs1->sp1 = pa; + outer_faces[i]->fs1->sp2 = pb; + + outer_faces[i]->fs2->sp1 = pb; + outer_faces[i]->fs2->sp2 = pc; + + outer_faces[i]->fs3->sp1 = pc; + outer_faces[i]->fs3->sp2 = pa; + } + + initialFacesCount = (int)inner_faces.size(); + + for(i=0;ir->x=(inner_faces[i]->fp1->r->x + inner_faces[i]->fp2->r->x)/2; + pa->r->y=(inner_faces[i]->fp1->r->y + inner_faces[i]->fp2->r->y)/2; + pa->r->z=(inner_faces[i]->fp1->r->z + inner_faces[i]->fp2->r->z)/2; + + pb->r->x=(inner_faces[i]->fp2->r->x + inner_faces[i]->fp3->r->x)/2; + pb->r->y=(inner_faces[i]->fp2->r->y + inner_faces[i]->fp3->r->y)/2; + pb->r->z=(inner_faces[i]->fp2->r->z + inner_faces[i]->fp3->r->z)/2; + + pc->r->x=(inner_faces[i]->fp1->r->x + inner_faces[i]->fp3->r->x)/2; + pc->r->y=(inner_faces[i]->fp1->r->y + inner_faces[i]->fp3->r->y)/2; + pc->r->z=(inner_faces[i]->fp1->r->z + inner_faces[i]->fp3->r->z)/2; + // + + /* cout<<"inner iteration before normalization: "<x="<r->x<<"; pa->y="<r->y<<"; pa->z="<r->z<x="<r->x<<"; pb->y="<r->y<<"; pb->z="<r->z<x="<r->x<<"; pc->y="<r->y<<"; pc->z="<r->z<r->Normalize(); + pb->r->Normalize(); + pc->r->Normalize(); + +/* cout<<"inner iteration after normalization: "<x="<r->x<<"; pa->y="<r->y<<"; pa->z="<r->z<x="<r->x<<"; pb->y="<r->y<<"; pb->z="<r->z<x="<r->x<<"; pc->y="<r->y<<"; pc->z="<r->z<r->x == pa->r->x + && inner_points[o]->r->y == pa->r->y + && inner_points[o]->r->z == pa->r->z + ) + { + delete pa; + pa = inner_points[o]; + a = true; + } + + if + ( + inner_points[o]->r->x == pb->r->x + && inner_points[o]->r->y == pb->r->y + && inner_points[o]->r->z == pb->r->z + ) + { + delete pb; + pb = inner_points[o]; + b = true; + } + + if + ( + inner_points[o]->r->x == pc->r->x + && inner_points[o]->r->y == pc->r->y + && inner_points[o]->r->z == pc->r->z + ) + { + delete pc; + pc = inner_points[o]; + c = true; + } + } + + // The new points are added to the general collection of points + if(!a) inner_points.push_back(pa); + if(!b) inner_points.push_back(pb); + if(!c) inner_points.push_back(pc); + + + // Create three new faces; these represent the three inner triangles + inner_faces.push_back(new Face(inner_faces[i]->fp1, pa, pc, inner_springs)); // Top + inner_faces.push_back(new Face(pa, inner_faces[i]->fp2, pb, inner_springs)); // Bottom-left +// inner_faces.push_back(new Face(pb, inner_faces[i]->fp3, pc, inner_springs)); // Bottom-right + inner_faces.push_back(new Face(pc, pb, inner_faces[i]->fp3, inner_springs)); // Bottom-right + + + // Replace the original face's points with its new one in the middle + + inner_faces[i]->fp1 = pa; + inner_faces[i]->fp2 = pb; + inner_faces[i]->fp3 = pc; + + + inner_faces[i]->fs1->sp1 = pa; + inner_faces[i]->fs1->sp2 = pb; + + inner_faces[i]->fs2->sp1 = pb; + inner_faces[i]->fs2->sp2 = pc; + + inner_faces[i]->fs3->sp1 = pc; + inner_faces[i]->fs3->sp2 = pa; + } + + + + + + } + + + int i; +for(i=0; ir->y<<";"<r->z<fp1->r->y<<";"<fp1->r->z<fp2->r->y<<";"<fp2->r->z<fp3->r->y<<";"<fp3->r->z<-90 && theta<90) + { + tix = 1*RING_RADIUS*cos(theta)*cos(phi+dphi); // outer point X coordiation + tiy = 1*RING_RADIUS*cos(theta)*sin(phi+dphi); // outer point Y coordiation + tiz = 1*RING_RADIUS*sin(theta); // outer point Y coordiation + + tox = 1*RING_RADIUS*cos(theta)*cos(phi+dphi); + toy = 1*RING_RADIUS*cos(theta)*sin(phi+dphi); // outer point Y coordiation + toz = 1*RING_RADIUS*sin(theta); // outer point Y coordiation + + outer_points.push_back( new Particle(new Vector(tox, toy, toz), MASS)); + + inner_points.push_back( new Particle(new Vector(tix, tiy, tiz), MASS)); + } + + // cout<<"tox=="<numParticles = (int) this->outer_points.size(); + this->numSprings = (int) this->outer_springs.size(); + +// cout<<"this->numParticles ==="<numParticles<<"-----this->numSprings===="<numSprings<<"-------this->numFaces===="<outer_faces.size()<CalNormalNField(); + outer_faces[i]->CalNormalNField(); +// cout<<"outer_faces[i]->normal->x==="<normal->x<<"=== outer_faces[i]->normal->y==="<normal->y<<"===outer_faces[i]->normal->z===="<normal->z<numParticles = (int) outer_points.size(); + + + a = 1 / sqrt(2.0); + + for (i=0;ir->x *= a; + outer_points[i]->r->y *= a; + // outer_points[i]->r->z *= a; + + inner_points[i]->r->x *= a; + inner_points[i]->r->y *= a; + // inner_points[i]->r->z *= a; + } + + + outer_faces.push_back(new Face(outer_points[0], outer_points[3], outer_points[4], outer_springs)); + outer_faces.push_back(new Face(outer_points[0], outer_points[4], outer_points[5], outer_springs)); + outer_faces.push_back(new Face(outer_points[0], outer_points[5], outer_points[2], outer_springs)); + outer_faces.push_back(new Face(outer_points[0], outer_points[2], outer_points[3], outer_springs)); + outer_faces.push_back(new Face(outer_points[1], outer_points[4], outer_points[3], outer_springs)); + outer_faces.push_back(new Face(outer_points[1], outer_points[5], outer_points[4], outer_springs)); + outer_faces.push_back(new Face(outer_points[1], outer_points[2], outer_points[5], outer_springs)); + outer_faces.push_back(new Face(outer_points[1], outer_points[3], outer_points[2], outer_springs)); + + + inner_faces.push_back(new Face(inner_points[0], inner_points[3], inner_points[4], inner_springs)); + inner_faces.push_back(new Face(inner_points[0], inner_points[4], inner_points[5], inner_springs)); + inner_faces.push_back(new Face(inner_points[0], inner_points[5], inner_points[2], inner_springs)); + inner_faces.push_back(new Face(inner_points[0], inner_points[2], inner_points[3], inner_springs)); + inner_faces.push_back(new Face(inner_points[1], inner_points[4], inner_points[3], inner_springs)); + inner_faces.push_back(new Face(inner_points[1], inner_points[5], inner_points[4], inner_springs)); + inner_faces.push_back(new Face(inner_points[1], inner_points[2], inner_points[5], inner_springs)); + inner_faces.push_back(new Face(inner_points[1], inner_points[3], inner_points[2], inner_springs)); + + + this->numSprings = (int) this->outer_springs.size(); + +} + + +//##ModelId=45F4D797011E +void Object3D::Pyramid3() +{ + + double a; + + int i; + + + outer_points.push_back( new Particle(new Vector(-2 * RING_RADIUS, 0, 2 * RING_RADIUS), MASS)); + outer_points.push_back( new Particle(new Vector(-2 * RING_RADIUS, 0, -2 * RING_RADIUS), MASS)); + outer_points.push_back( new Particle(new Vector( 2 * RING_RADIUS, 0, 0), MASS)); + outer_points.push_back( new Particle(new Vector( 0, 2 * RING_RADIUS,0), MASS)); + + // XXX: must match above + inner_points.push_back( new Particle(new Vector(0,0,RING_RADIUS), MASS)); + inner_points.push_back( new Particle(new Vector(0,0,-RING_RADIUS), MASS)); + inner_points.push_back( new Particle(new Vector(-RING_RADIUS,-RING_RADIUS,0), MASS)); + inner_points.push_back( new Particle(new Vector(RING_RADIUS,-RING_RADIUS,0), MASS)); + + + this->numParticles = (int)outer_points.size(); + + + a = 1 / sqrt(2.0); + + + + for (i=0;ir->x *= a; + outer_points[i]->r->y *= a; + outer_points[i]->r->z *= a; + + + inner_points[i]->r->x *= a; + inner_points[i]->r->y *= a; + inner_points[i]->r->z *= a; + } + + + // Bottom face + outer_faces.push_back(new Face(outer_points[0], outer_points[1], outer_points[2], outer_springs)); + + // Left face + outer_faces.push_back(new Face(outer_points[0], outer_points[1], outer_points[3], outer_springs)); + + + // Front face + outer_faces.push_back(new Face(outer_points[0], outer_points[2], outer_points[3], outer_springs)); + + // Back face + outer_faces.push_back(new Face(outer_points[1], outer_points[2], outer_points[3], outer_springs)); + + + //----------------------------- + +///* + + inner_faces.push_back(new Face(inner_points[0], inner_points[1], inner_points[2], inner_springs)); + + // Left face + inner_faces.push_back(new Face(inner_points[0], inner_points[1], inner_points[3], inner_springs)); + + // Front face + inner_faces.push_back(new Face(inner_points[0], inner_points[2], inner_points[3], inner_springs)); + + // Back face + inner_faces.push_back(new Face(inner_points[1], inner_points[2], inner_points[3], inner_springs)); + +//*/ + + + + this->numSprings = (int) this->outer_springs.size(); +} + + +//================================================================================ + +//##ModelId=45F4D79700FD +void Object3D::Draw(int shaderID, glm::mat4 worldMatrix) +{ + + + static bool findOnce = false; +// glDisable(GL_DEPTH_TEST); +// glDisable(GL_CULL_FACE); + +// int i; + //=============================================================================== +// glPushMatrix(); +// glColor4f(0,0,1,1); // Blue color point distribution +// glBegin(GL_POINTS); // Draw points which built the Object3D +// glPointSize(10); +// for(i=0; ir->x, outer_points[i]->r->y, outer_points[i]->r->z); +// } +// +// glEnd(); +// +// glColor4f(0,0,1,1); +// +// glBegin(GL_POINTS); // Draw inner points which built the Object3D +// glPointSize(10); +// for(i=0; ir->x, inner_points[i]->r->y, inner_points[i]->r->z); +// } +// glEnd(); +// +// glPopMatrix(); +//=================================================================================== + + // =============== basic settings for ogl3 drawing + updateVertexArray(); + glBindVertexArray(VAO); + // set world matrix + glUniformMatrix4fv(glGetUniformLocation(shaderID, "worldMatrix"), 1, GL_FALSE, &glm::mat4(1.0f)[0][0]); + // get color location + GLuint ColorLocation = glGetUniformLocation(shaderID, "colorChoice"); + glEnableClientState(GL_VERTEX_ARRAY); + glPointSize(10); + // ==================================================== + + // drawing outer and inner points (blue) + glUniform4f(ColorLocation, 0.0f, 0.0f, 1.0f, 1.0f); + glDrawArrays(GL_POINTS, getOffset(outer_inner_points), getCount(outer_inner_points)); + + // drawing inner faces + glUniform4f(ColorLocation, 1.0f, 0.0f, 1.0f, 0.6f); + glDrawArrays(GL_TRIANGLES, getOffset(inner_face), getCount(inner_face)); + + // drawing outer faces + glUniform4f(ColorLocation, 1.0f, 1.0f, 1.0f, 0.6f); + glDrawArrays(GL_TRIANGLES, getOffset(outer_face), getCount(outer_face)); + + // drawing outer faces normals + glUniform4f(ColorLocation, 0.0f, 0.0f, 1.0f, 1.0f); + glDrawArrays(GL_LINES, getOffset(outer_face_normal), getCount(outer_face_normal)); + + +// cout<<"Object3D spring size is : "<sp1->r->x, outer_springs[i]->sp1->r->y, outer_springs[i]->sp1->r->z); + glVertex3f(outer_springs[i]->sp2->r->x, outer_springs[i]->sp2->r->y, outer_springs[i]->sp2->r->z); + + + } + glEnd(); + glPopMatrix(); + //*/ +///========================================draw inner structural springs========================================== + +/* glPushMatrix(); + glColor4f(1,0,0,1); + glBegin(GL_LINES); + for(i = 0; isp1->r->x, inner_springs[i]->sp1->r->y, inner_springs[i]->sp1->r->z); + glVertex3f(inner_springs[i]->sp2->r->x, inner_springs[i]->sp2->r->y, inner_springs[i]->sp2->r->z); + } + glEnd(); + glPopMatrix(); +*/ +///========================================draw radium springs========================================== +/* glPushMatrix(); + glBegin(GL_LINES); // the draw of radium lines from inner to outer + glLineWidth(10); + for(i=0 ; isp1->r->x, radium_springs[i]->sp1->r->y, radium_springs[i]->sp1->r->z); + glVertex3f(radium_springs[i]->sp2->r->x, radium_springs[i]->sp2->r->y, radium_springs[i]->sp2->r->z); + } + glEnd(); + glPopMatrix();*/ + +///========================================draw shear left springs========================================== +/* glPushMatrix(); + glBegin(GL_LINES); // the left shear lines from inner to outer + for(i=0 ; isp1->r->x,shear_springs_left[i]->sp1->r->y, shear_springs_left[i]->sp1->r->z); + glVertex3f(shear_springs_left[i]->sp2->r->x,shear_springs_left[i]->sp2->r->y, shear_springs_left[i]->sp2->r->z); + } + glEnd(); + glPopMatrix();*/ + +///========================================draw shear right springs========================================== + +/* glPushMatrix(); + glBegin(GL_LINES); // the right shear lines from inner to outer + for(i=0 ; isp1->r->x,shear_springs_right[i]->sp1->r->y, shear_springs_right[i]->sp1->r->z); + glVertex3f(shear_springs_right[i]->sp2->r->x,shear_springs_right[i]->sp2->r->y, shear_springs_right[i]->sp2->r->z); + } + glEnd(); + glPopMatrix();*/ + + + +//======================================================================================= +/////----------------------------------------- Draw inner faces ---------------------------------------------- +// cout<<"inner_faces.size()???????????????"<normal->x,inner_faces[i]->normal->y,inner_faces[i]->normal->z); +// glVertex3f(inner_faces[i]->fp1->r->x, inner_faces[i]->fp1->r->y, inner_faces[i]->fp1->r->z); +// glVertex3f(inner_faces[i]->fp2->r->x, inner_faces[i]->fp2->r->y, inner_faces[i]->fp2->r->z); +// glVertex3f(inner_faces[i]->fp3->r->x, inner_faces[i]->fp3->r->y, inner_faces[i]->fp3->r->z); +// +// } +// glEnd(); +/////----------------------------------------- Draw inner faces normals---------------------------------------------- + +/* glBegin(GL_LINES); + for(i=0 ; ifp1->r->x, inner_faces[i]->fp1->r->y, inner_faces[i]->fp1->r->z); + + glVertex3f((inner_faces[i]->normal->x + inner_faces[i]->fp1->r->x), + (inner_faces[i]->normal->y + inner_faces[i]->fp1->r->y), + (inner_faces[i]->normal->z + inner_faces[i]->fp1->r->z)); + } + glEnd();*/ + +// glPopMatrix(); + + +/////----------------------------------------- Draw outer faces ---------------------------------------------- + +// glPushMatrix(); +// glBegin(GL_TRIANGLES); +// for(i=0; inormal->x,outer_faces[i]->normal->y,outer_faces[i]->normal->z); +// glVertex3f(outer_faces[i]->fp1->r->x, outer_faces[i]->fp1->r->y, outer_faces[i]->fp1->r->z); +// glVertex3f(outer_faces[i]->fp2->r->x, outer_faces[i]->fp2->r->y, outer_faces[i]->fp2->r->z); +// glVertex3f(outer_faces[i]->fp3->r->x, outer_faces[i]->fp3->r->y, outer_faces[i]->fp3->r->z); +// +// } +// glEnd(); +/////----------------------------------------- Draw outer faces normals---------------------------------------------- + + +// glColor3f(0,0,1); +// glBegin(GL_LINES); +// for(i=0 ; ifp1->r->x, outer_faces[i]->fp1->r->y, outer_faces[i]->fp1->r->z); +// glVertex3f(outer_faces[i]->fp2->r->x, outer_faces[i]->fp2->r->y, outer_faces[i]->fp2->r->z); +// +// glVertex3f(outer_faces[i]->fp2->r->x, outer_faces[i]->fp2->r->y, outer_faces[i]->fp2->r->z); +// glVertex3f(outer_faces[i]->fp3->r->x, outer_faces[i]->fp3->r->y, outer_faces[i]->fp3->r->z); +// +// glVertex3f(outer_faces[i]->fp3->r->x, outer_faces[i]->fp3->r->y, outer_faces[i]->fp3->r->z); +// glVertex3f(outer_faces[i]->fp1->r->x, outer_faces[i]->fp1->r->y, outer_faces[i]->fp1->r->z); +// +//// glVertex3f((outer_faces[i]->normal->x + outer_faces[i]->fp1->r->x), +//// (outer_faces[i]->normal->y + outer_faces[i]->fp1->r->y), +//// (outer_faces[i]->normal->z + outer_faces[i]->fp1->r->z)); +// +// +// glVertex3f(inner_faces[i]->fp1->r->x, inner_faces[i]->fp1->r->y, inner_faces[i]->fp1->r->z); +// glVertex3f(inner_faces[i]->fp2->r->x, inner_faces[i]->fp2->r->y, inner_faces[i]->fp2->r->z); +// +// glVertex3f(inner_faces[i]->fp2->r->x, inner_faces[i]->fp2->r->y, inner_faces[i]->fp2->r->z); +// glVertex3f(inner_faces[i]->fp3->r->x, inner_faces[i]->fp3->r->y, inner_faces[i]->fp3->r->z); +// +// glVertex3f(inner_faces[i]->fp3->r->x, inner_faces[i]->fp3->r->y, inner_faces[i]->fp3->r->z); +// glVertex3f(inner_faces[i]->fp1->r->x, inner_faces[i]->fp1->r->y, inner_faces[i]->fp1->r->z); +// } +// glEnd(); +// +// glPopMatrix(); +//--------------------------------------------------------------------------------------- + if(integrator != nullptr){ + if(integrator->dragExists) + { + if(findOnce == true) + { + FindClosestPoint(); + findOnce = false; + } + // drawing integrator line + glUniform4f(ColorLocation, 1.0f, 1.0f, 0.0f, 1.0f); + glDrawArrays(GL_LINES, getOffset(integrator_line), getCount(integrator_line)); + + } + else + { + FindClosestPoint(); + findOnce = true; + } + } +// glEnable(GL_DEPTH_TEST); +// glEnable(GL_CULL_FACE); +} + diff --git a/Object3D.h b/Object3D.h new file mode 100644 index 0000000..94fe043 --- /dev/null +++ b/Object3D.h @@ -0,0 +1,88 @@ +#ifndef OBJECT3D_H +#define OBJECT3D_H + + +#include "Particle.h" +#include "Spring.h" + +#include "Object2D.h" + + +//##ModelId=45F4D79700EC +class Object3D : public Object2D +{ +public: + + //##ModelId=45F4D79700EE + Object3D(); // object3D constructor +// Object3D(float Mass, float Ks, float Kd, float Press); +// Object3D (float Mass, float Ks, float Kd); + //##ModelId=45F4D79700FB + virtual ~Object3D(); // object3D destructor + + /* + void Mouse(int, int, int, int); + void Keyboard(unsigned char, int, int); + void SpecialKeys(int, int, int); + void Rotated(); + */ + +/* + void ExternalForces(); // gravity force + void PressureForces(); // pressure + void SpringForces(); // spring forces + void AccumulateForces(); // accumulate forces acted on + void Derivatives(float); // Differential computation +*/ + +public: + + //##ModelId=45F4D79700FD + virtual void Draw(int, glm::mat4 worldMatrix); + //##ModelId=45F4D79700FF + void Iteration(); + //##ModelId=45F4D7970100 + void nonunitsphere(); + //void GetClosestParticle(); // the Particle on the Object3D, which is cloest to the mouse pos + + //void GetParticles (); + //void GetFace(); + +private: + //##ModelId=45F4D7970101 + virtual void SetObject(void); // model the object3D + //##ModelId=45F4D797010B + void Add_Structural_Spring(int index, int h, int t); // model the inner or outer cirle with structural springs + //##ModelId=45F4D797010F + void Add_Radium_Spring(int index); // add the radium springs with inner point i and outer point i + //##ModelId=45F4D797011B + void Add_Shear_Spring(int index); // add the left&right shear springs with inner point i and outer point i+1 + + //##ModelId=45F4D797011D + void Tetrahedron(); // draw the single shape + //##ModelId=45F4D797011E + void Pyramid3(); + //##ModelId=45F4D797011F + void Pyramid4(); + + Particle *pa, *pb, *pc; // the three points subdivide each edge on the triangle face + //##ModelId=45F4D797012A + int iterations; // the number of iteration for subdivision + + // for OGL3 drawing + void createVertexArray(); + glm::vec3* vertexArrayData; + void updateVertexArray(); + unsigned int total; + GLuint VAO; + GLuint VBO; + + // other helper functions to get the offset of drawing instances + enum drawingInstances{outer_inner_points, outer_face, inner_face, outer_face_normal, integrator_line}; + + int getOffset(drawingInstances instance); + int getCount(drawingInstances instance); + +}; + +#endif diff --git a/Particle.cpp b/Particle.cpp new file mode 100644 index 0000000..679c16c --- /dev/null +++ b/Particle.cpp @@ -0,0 +1,52 @@ +#include "Particle.h" + + +// class Particle number functions + +//##ModelId=45F4D7970080 +void Particle::ClearForce(void) // reset forces at a Particle +{ + + if(f == NULL) + { + f = new Vector(0,0,0); // A force vector with all coordinates equal zero + } + else + { + f->x = 0; + f->y = 0; + f->z = 0; + } +} + + +ostream& operator<<(ostream& os, const Particle& c) // output operator +{ + + //os<x; + os<y; + os<z; + + os<x; // Particle space position + os<y; + os<z; + + os<x; // Particle space velocity + os<y; + os<z; + + os<x; // small change of position in derivatives + os<y; + os<z; + + os<x; // small change of velocity in derivatives + os<y; + os<z; + + return os; // Always return stream object. +} diff --git a/Particle.h b/Particle.h new file mode 100644 index 0000000..d041920 --- /dev/null +++ b/Particle.h @@ -0,0 +1,103 @@ +#ifndef PARTICLE_H +#define PARTICLE_H + +#include "Vector.h" + +// Class Particle declaration +//##ModelId=45F4D797006F +class Particle +{ + public: + + //##ModelId=45F4D7970070 + Particle(Vector* R, float Mass) : mass(Mass) // particle constructor with variables position and mass + { + r = R; // initialize Particle space position + // OneOverMass=1.0/mass; + dr = new Vector(0,0,0); // initialize position in derivatives + dv = new Vector(0,0,0); // initialize velocity in derivatives + f = new Vector(0,0,0); // initialize Particle space force + v = new Vector(0,0,0); // initialize Particle space velocity + } + + //##ModelId=45F4D7970073 + Particle() // particle constructor with no variables + { + r = new Vector(0,0,0); // initialize Particle space position + dr = new Vector(0,0,0); // initialize position in derivatives + dv = new Vector(0,0,0); // initialize velocity in derivatives + f = new Vector(0,0,0); // initialize Particle space force + v = new Vector(0,0,0); // initialize Particle space velocity + mass = MASS; + } + + //##ModelId=45F4D7970074 + Particle(const Particle& p) // particle constructor with variables position + { + mass = p.mass; + + r = new Vector(0,0,0); // initialize Particle space position + r->x = p.r->x; + r->y = p.r->y; + r->z = p.r->z; + + dr = new Vector(0,0,0); // initialize position in derivatives + dr->x = p.dr->x; + dr->y = p.dr->y; + dr->z = p.dr->z; + + dv = new Vector(0,0,0); // initialize velocity in derivatives + dv->x = p.dv->x; + dv->y = p.dv->y; + dv->z = p.dv->z; + + f = new Vector(0,0,0); // initialize Particle space force + f->x = p.f->x; + f->y = p.f->y; + f->z = p.f->z; + + v = new Vector(0,0,0); // initialize Particle space velocity + v->x = p.v->x; + v->y = p.v->y; + v->z = p.v->z; + } + + + //##ModelId=45F4D797007F + ~Particle() // particle destructor + { + /* + delete norm; + delete dr; + delete dv; + delete f; + delete v; + */ + } + + //##ModelId=45F4D7970080 + void ClearForce(void); // reset forces at a Particle +// void AccumuForce(Vector const &); + //##ModelId=45F4D79700B2 + friend ostream& operator<<(ostream& os, const Particle& c); //output operator + + +public: + + //##ModelId=45F4D7970082 + float mass; // space Particle with mass + //##ModelId=45F4D797008E + float OneOverMass; // for computation + //##ModelId=45F4D7970090 + Vector* r; // Particle space position + //##ModelId=45F4D7970095 + Vector* f; // Particle space force + //##ModelId=45F4D797009F + Vector* v; // Particle space velocity + //##ModelId=45F4D79700A4 + Vector* dr; // small change of position in derivatives + //##ModelId=45F4D79700AE + Vector* dv; // small change of velocity in derivatives +}; + +#endif diff --git a/README.md b/README.md index e95ef9e..78f7262 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,18 @@ -# SoftbodySimulationSystem +# SoftbodySimulationSystem OGL3 Softbody Simulation System -- a framework and for softbody objects simulation and comparison of algorithm implementations with sample code instances. +Using OpenGL 3 ## References * [Miao Song](https://dblp.uni-trier.de/pers/hd/s/Song_0001:Miao), Serguei A. Mokhov, and Peter Grogono. 2009. Designing an interactive OpenGL slide-based presentation of the softbody simulation system for teaching and learning of computer graphics techniques. In Proceedings of the 2nd Canadian Conference on Computer Science and Software Engineering (C3S2E '09), Bipin C. Desai and Carson K. Leung (Eds.). ACM, New York, NY, USA, 131-136. DOI=[10.1145/1557626.1557647](http://dx.doi.org/10.1145/1557626.1557647) * Miao Song and Peter Grogono. 2009. Application of advanced rendering and animation techniques for 3D games to softbody modeling and animation. In Proceedings of the 2nd Canadian Conference on Computer Science and Software Engineering (C3S2E '09), Bipin C. Desai and Carson K. Leung (Eds.). ACM, New York, NY, USA, 89-100. DOI=[10.1145/1557626.1557640](http://dx.doi.org/10.1145/1557626.1557640) * Song, Miao. "Dynamic deformation of uniform elastic two-layer objects.", Master's thesis, arXiv preprint [arXiv:0907.4364](https://arxiv.org/abs/0907.4364) (2009). + + +# Important updates and notes +1. Removed Simulation.h and Simulation.cpp from source code, but added a single main.cpp +2. main.cpp can render a demo scene for softbody simulation, including a viewspace, and 3 softbody entities. +3. Added resources/shaders/core.fs and resources/shaders/core.vs +4. Added shader.h to compile and link the above mentioned shaders. +5. To compile the project, libglfw.3.3.dylib and libGLEW.2.1.0.dylib are needed +6. If you have questions, please reach out through jiarui.li@mail.concordia.ca diff --git a/RungeKutta4Integrator.cpp b/RungeKutta4Integrator.cpp new file mode 100644 index 0000000..fb91b95 --- /dev/null +++ b/RungeKutta4Integrator.cpp @@ -0,0 +1,167 @@ +#include "RungeKutta4Integrator.h" + +//##ModelId=45F4D7970041 +RungeKutta4Integrator::RungeKutta4Integrator(Object& object) : MidpointIntegrator(object) +{ +} + +//##ModelId=45F4D7970043 +RungeKutta4Integrator::~RungeKutta4Integrator() +{ +} + + +//##ModelId=45F4D7970044 +void RungeKutta4Integrator::k4(int i, float k, float deltaT) +{ + k3(i, k, deltaT); + +// memcpy(temp_inner_points4, temp_inner_points3, sizeof(Particle) * MAX_POINTS_SPRINGS); +// memcpy(temp_outer_points4, temp_outer_points3, sizeof(Particle) * MAX_POINTS_SPRINGS); + + + for(int j=1; jouter_points.size(); j++) + { + + temp_outer_points4[j]->mass = temp_outer_points3[j]->mass; + temp_outer_points4[j]->r = temp_outer_points3[j]->r; + temp_outer_points4[j]->v = temp_outer_points3[j]->v; + temp_outer_points4[j]->f = temp_outer_points3[j]->f; + temp_outer_points4[j]->dr = temp_outer_points3[j]->dr; + temp_outer_points4[j]->dv = temp_outer_points3[j]->dv; + temp_outer_points4[j]->OneOverMass = temp_outer_points3[j]->OneOverMass; + + temp_inner_points4[j]->mass = temp_inner_points3[j]->mass; + temp_inner_points4[j]->r = temp_inner_points3[j]->r; + temp_inner_points4[j]->v = temp_inner_points3[j]->v; + temp_inner_points4[j]->f = temp_inner_points3[j]->f; + temp_inner_points4[j]->dr = temp_inner_points3[j]->dr; + temp_inner_points4[j]->dv = temp_inner_points3[j]->dv; + temp_inner_points4[j]->OneOverMass = temp_inner_points3[j]->OneOverMass; + } + +// k1(i, 0.5 * k, deltaT); + + temp_inner_points4[i]->dv->x = temp_inner_points0[i]->dv->x + k * (temp_inner_points3[i]->f->x/temp_inner_points3[i]->mass) * deltaT; + temp_inner_points4[i]->dv->y = temp_inner_points0[i]->dv->y + k * (temp_inner_points3[i]->f->y/temp_inner_points3[i]->mass) * deltaT; + temp_inner_points4[i]->dv->z = temp_inner_points0[i]->dv->z + k * (temp_inner_points3[i]->f->z/temp_inner_points3[i]->mass) * deltaT; + + temp_inner_points4[i]->dr->x = temp_inner_points0[i]->dr->x + k * temp_inner_points3[i]->dv->x * deltaT; + temp_inner_points4[i]->dr->y = temp_inner_points0[i]->dr->y + k * temp_inner_points3[i]->dv->y * deltaT; + temp_inner_points4[i]->dr->z = temp_inner_points0[i]->dr->z + k * temp_inner_points3[i]->dv->z * deltaT; + + temp_outer_points4[i]->dv->x = temp_outer_points0[i]->dv->x + k * (temp_outer_points3[i]->f->x/temp_outer_points3[i]->mass) * deltaT; + temp_outer_points4[i]->dv->y = temp_outer_points0[i]->dv->y + k * (temp_outer_points3[i]->f->y/temp_outer_points3[i]->mass) * deltaT; + temp_outer_points4[i]->dv->z = temp_outer_points0[i]->dv->z + k * (temp_outer_points3[i]->f->z/temp_outer_points3[i]->mass) * deltaT; + + temp_outer_points4[i]->dr->x = temp_outer_points0[i]->dr->x + k * temp_outer_points3[i]->dv->x * deltaT; + temp_outer_points4[i]->dr->y = temp_outer_points0[i]->dr->y + k * temp_outer_points3[i]->dv->y * deltaT; + temp_outer_points4[i]->dr->z = temp_outer_points0[i]->dr->z + k * temp_outer_points3[i]->dv->z * deltaT; + +} + +//##ModelId=45F4D7970050 +void RungeKutta4Integrator::k3(int i, float k, float deltaT) +{ + k2(i, k, deltaT); + +// memcpy(temp_inner_points3, temp_inner_points2, sizeof(Particle) * MAX_POINTS_SPRINGS); +// memcpy(temp_outer_points3, temp_outer_points2, sizeof(Particle) * MAX_POINTS_SPRINGS); + + + for(int j=1; jouter_points.size(); j++) + { + + temp_outer_points3[j]->mass = temp_outer_points2[j]->mass; + temp_outer_points3[j]->r = temp_outer_points2[j]->r; + temp_outer_points3[j]->v = temp_outer_points2[j]->v; + temp_outer_points3[j]->f = temp_outer_points2[j]->f; + temp_outer_points3[j]->dr = temp_outer_points2[j]->dr; + temp_outer_points3[j]->dv = temp_outer_points2[j]->dv; + temp_outer_points3[j]->OneOverMass = temp_outer_points2[j]->OneOverMass; + + temp_inner_points3[j]->mass = temp_inner_points2[j]->mass; + temp_inner_points3[j]->r = temp_inner_points2[j]->r; + temp_inner_points3[j]->v = temp_inner_points2[j]->v; + temp_inner_points3[j]->f = temp_inner_points2[j]->f; + temp_inner_points3[j]->dr = temp_inner_points2[j]->dr; + temp_inner_points3[j]->dv = temp_inner_points2[j]->dv; + temp_inner_points3[j]->OneOverMass = temp_inner_points2[j]->OneOverMass; + } + +// k1(i, 0.5 * k, deltaT); + + temp_inner_points3[i]->dv->x = temp_inner_points0[i]->dv->x + k * 0.5 * (temp_inner_points2[i]->f->x/temp_inner_points2[i]->mass) * deltaT; + temp_inner_points3[i]->dv->y = temp_inner_points0[i]->dv->y + k * 0.5 * (temp_inner_points2[i]->f->y/temp_inner_points2[i]->mass) * deltaT; + temp_inner_points3[i]->dr->x = temp_inner_points0[i]->dr->x + k * 0.5 * temp_inner_points2[i]->dv->x * deltaT; + temp_inner_points3[i]->dr->y = temp_inner_points0[i]->dr->y + k * 0.5 * temp_inner_points2[i]->dv->y * deltaT; + + temp_outer_points3[i]->dv->x = temp_outer_points0[i]->dv->x + k * 0.5 * (temp_outer_points2[i]->f->x/temp_outer_points2[i]->mass) * deltaT; + temp_outer_points3[i]->dv->y = temp_outer_points0[i]->dv->y + k * 0.5 * (temp_outer_points2[i]->f->y/temp_outer_points2[i]->mass) * deltaT; + temp_outer_points3[i]->dr->x = temp_outer_points0[i]->dr->x + k * 0.5 * temp_outer_points2[i]->dv->x * deltaT; + temp_outer_points3[i]->dr->y = temp_outer_points0[i]->dr->y + k * 0.5 * temp_outer_points2[i]->dv->y * deltaT; +} + +/* +void RungeKutta4Integrator::k2(int i, float k, float deltaT) +{ + k1(i, k, deltaT); + + memcpy(temp_inner_points2, temp_inner_points1, sizeof(Particle) * MAX_POINTS_SPRINGS); + memcpy(temp_outer_points2, temp_outer_points1, sizeof(Particle) * MAX_POINTS_SPRINGS); + + +// k1(i, 0.5 * k, deltaT); + + temp_inner_points2[i].v->x = temp_inner_points0[i].v->x + k * 0.5 * (temp_inner_points1[i].f->x/temp_inner_points1[i].mass) * deltaT; + temp_inner_points2[i].v->y = temp_inner_points0[i].v->y + k * 0.5 * (temp_inner_points1[i].f->y/temp_inner_points1[i].mass) * deltaT; + temp_inner_points2[i].r->x = temp_inner_points0[i].r->x + k * 0.5 * temp_inner_points1[i].v->x * deltaT; + temp_inner_points2[i].r->y = temp_inner_points0[i].r->y + k * 0.5 * temp_inner_points1[i].v->y * deltaT; + + temp_outer_points2[i].v->x = temp_outer_points0[i].v->x + k * 0.5 * (temp_outer_points1[i].f->x/temp_outer_points1[i].mass) * deltaT; + temp_outer_points2[i].v->y = temp_outer_points0[i].v->y + k * 0.5 * (temp_outer_points1[i].f->y/temp_outer_points1[i].mass) * deltaT; + temp_outer_points2[i].r->x = temp_outer_points0[i].r->x + k * 0.5 * temp_outer_points1[i].v->x * deltaT; + temp_outer_points2[i].r->y = temp_outer_points0[i].r->y + k * 0.5 * temp_outer_points1[i].v->y * deltaT; +} +*/ + +//##ModelId=45F4D7970054 +void RungeKutta4Integrator::ynew(int i, float k, float deltaT) +{ + k4(i, k, deltaT); + + object->inner_points[i]->dv->x = temp_inner_points4[i]->dv->x; + object->inner_points[i]->dv->y = temp_inner_points4[i]->dv->y; + object->inner_points[i]->dv->z = temp_inner_points4[i]->dv->z; + + object->inner_points[i]->dr->x = temp_inner_points4[i]->dr->x; // Change in position is velocity times the change in time + object->inner_points[i]->dr->y = temp_inner_points4[i]->dr->y; + object->inner_points[i]->dr->z = temp_inner_points4[i]->dr->z; + + + object->inner_points[i]->v->x = temp_inner_points0[i]->v->x + 1/6*temp_inner_points1[i]->v->x + 1/3*temp_inner_points2[i]->v->x + 1/3*temp_inner_points3[i]->v->x + 1/6*temp_inner_points4[i]->v->x; // Change in velocity is added to the velocity. + object->inner_points[i]->v->y = temp_inner_points0[i]->v->y + 1/6*temp_inner_points1[i]->v->y + 1/3*temp_inner_points2[i]->v->y + 1/3*temp_inner_points3[i]->v->y + 1/6*temp_inner_points4[i]->v->y; // Change in velocity is added to the velocity. + object->inner_points[i]->v->z = temp_inner_points0[i]->v->z + 1/6*temp_inner_points1[i]->v->z + 1/3*temp_inner_points2[i]->v->z + 1/3*temp_inner_points3[i]->v->z + 1/6*temp_inner_points4[i]->v->z; // Change in velocity is added to the velocity. + + object->inner_points[i]->r->x = temp_inner_points0[i]->r->x + 1/6*temp_inner_points1[i]->r->x + 1/3*temp_inner_points2[i]->r->x + 1/3*temp_inner_points3[i]->r->x + 1/6*temp_inner_points4[i]->r->x; // Change in velocity is added to the velocity. // Change in position is velocity times the change in time + object->inner_points[i]->r->y = temp_inner_points0[i]->r->y + 1/6*temp_inner_points1[i]->r->y + 1/3*temp_inner_points2[i]->r->y + 1/3*temp_inner_points3[i]->r->y + 1/6*temp_inner_points4[i]->r->y; // Change in velocity is added to the velocity. // Change in position is velocity times the change in time + object->inner_points[i]->r->z = temp_inner_points0[i]->r->z + 1/6*temp_inner_points1[i]->r->z + 1/3*temp_inner_points2[i]->r->z + 1/3*temp_inner_points3[i]->r->z + 1/6*temp_inner_points4[i]->r->z; // Change in velocity is added to the velocity. // Change in position is velocity times the change in time + + + object->outer_points[i]->dv->x = temp_outer_points4[i]->dv->x; + object->outer_points[i]->dv->y = temp_outer_points4[i]->dv->y; + object->outer_points[i]->dv->z = temp_outer_points4[i]->dv->z; + + object->outer_points[i]->dr->x = temp_outer_points4[i]->dr->x; // Change in position is velocity times the change in time + object->outer_points[i]->dr->y = temp_outer_points4[i]->dr->y; + object->outer_points[i]->dr->z = temp_outer_points4[i]->dr->z; + + object->outer_points[i]->v->x = temp_outer_points0[i]->v->x + 1/6*temp_outer_points1[i]->v->x + 1/3*temp_outer_points2[i]->v->x + 1/3*temp_outer_points3[i]->v->x + 1/6*temp_outer_points4[i]->v->x; // Change in velocity is added to the velocity. + object->outer_points[i]->v->y = temp_outer_points0[i]->v->y + 1/6*temp_outer_points1[i]->v->y + 1/3*temp_outer_points2[i]->v->y + 1/3*temp_outer_points3[i]->v->y + 1/6*temp_outer_points4[i]->v->y; // Change in velocity is added to the velocity. + object->outer_points[i]->v->z = temp_outer_points0[i]->v->z + 1/6*temp_outer_points1[i]->v->z + 1/3*temp_outer_points2[i]->v->z + 1/3*temp_outer_points3[i]->v->z + 1/6*temp_outer_points4[i]->v->z; // Change in velocity is added to the velocity. + + object->outer_points[i]->r->x = temp_outer_points0[i]->r->x + 1/6*temp_outer_points1[i]->r->x + 1/3*temp_outer_points2[i]->r->x + 1/3*temp_outer_points3[i]->r->x + 1/6*temp_outer_points4[i]->r->x; // Change in velocity is added to the velocity. // Change in position is velocity times the change in time + object->outer_points[i]->r->y = temp_outer_points0[i]->r->y + 1/6*temp_outer_points1[i]->r->y + 1/3*temp_outer_points2[i]->r->y + 1/3*temp_outer_points3[i]->r->y + 1/6*temp_outer_points4[i]->r->y; // Change in velocity is added to the velocity. // Change in position is velocity times the change in time + object->outer_points[i]->r->z = temp_outer_points0[i]->r->z + 1/6*temp_outer_points1[i]->r->z + 1/3*temp_outer_points2[i]->r->z + 1/3*temp_outer_points3[i]->r->z + 1/6*temp_outer_points4[i]->r->z; // Change in velocity is added to the velocity. // Change in position is velocity times the change in time + +} diff --git a/RungeKutta4Integrator.h b/RungeKutta4Integrator.h new file mode 100644 index 0000000..f8e1e32 --- /dev/null +++ b/RungeKutta4Integrator.h @@ -0,0 +1,25 @@ +#ifndef RUNGEKUTTA4INTEGRATOR_H +#define RUNGEKUTTA4INTEGRATOR_H + +#include "MidpointIntegrator.h" + + +class RungeKutta4Integrator : public MidpointIntegrator +{ +public: + + RungeKutta4Integrator(Object&);// constructor + + ~RungeKutta4Integrator();// destructor + + +protected: + + void k3(int, float, float);// 3rd integrate to get the derivatives of velocity and position + + void k4(int, float, float);// 4th integrate to get the derivatives of velocity and position + + void ynew(int, float, float);// get the new velocity and position +}; + +#endif /* RUNGEKUTTA4INTEGRATOR_H */ diff --git a/Spring.cpp b/Spring.cpp new file mode 100644 index 0000000..b03a254 --- /dev/null +++ b/Spring.cpp @@ -0,0 +1,52 @@ +#include "Spring.h" + + + +//##ModelId=45F4D79603CB +Spring::Spring(Particle *P1, Particle *P2,float Ks, float Kd): // spring constructor with spring head, tail, and Ks, Kd + sp1(P1), sp2(P2), ks(Ks), kd(Kd) +{ // *P1, *P2 Particles the srping two ends + // Spring_ks is the Hooke constant + // Spring_kd is the dampling constant + + if (restLen != 0) //????????????????????????????????????????? + { + restLen = (*sp1->r - *sp2->r).getLength(); // static length + } + changePercent = 10.0; + maxAllowedDist = (changePercent/1.0f)*restLen; + type = SPRING_STRUCTURAL; // default spring type is structural spring which + // construct the object shape +} + + +//##ModelId=45F4D79603DE +Spring::Spring() // spring constructor with no initial value +{ + sp1 = new Particle(); // head Particle of a Spring + sp2 = new Particle(); // tail Particle of a Spring + restLen = 0; // lenth when Fs = 0 (Hooke force) + changePercent = 0; // percentage of spring length change + maxAllowedDist = 0; // maximum distange allowed from the rest length + ks = 0; // Spring constant + kd = 0; // damping factor + type = SPRING_STRUCTURAL; // default spring type is structural spring which +} // construct the object shape + +//##ModelId=45F4D79603DF +Spring::~Spring() //spring destructor +{ +/* if(sp1 != NULL) + delete sp1; + + if(sp2 != NULL) + delete sp2; +*/ +} + +//##ModelId=45F4D79603E0 +//set the springs resting length +void Spring::setRestLen() +{ + restLen = (*sp1->r - *sp2->r).getLength(); // static length +} diff --git a/Spring.h b/Spring.h new file mode 100644 index 0000000..43ea0fc --- /dev/null +++ b/Spring.h @@ -0,0 +1,47 @@ +#ifndef SPRING_H +#define SPRING_H + + +#include "Particle.h" +// Class Spring is an element cell of 2D ball entire surface +// Linear hooke Spring is built between two space Particles + +//##ModelId=45F4D79603CA +class Spring +{ +public: + + //##ModelId=45F4D79603CB + Spring(Particle *P1, Particle *P2,float Ks=KS, float Kd=KD); // constructor with variables + //##ModelId=45F4D79603DE + Spring(); // constructor with no variable + //##ModelId=45F4D79603DF + ~Spring(); // destructor + + //##ModelId=45F4D79603E0 + void setRestLen(); + +public: +//protected: + //##ModelId=45F4D79603E2 + Particle *sp1; // head Particle of a Spring + //##ModelId=45F4D7970004 + Particle *sp2; // tail Particle of a Spring + //##ModelId=45F4D7970008 + float restLen; // length when spring force = 0 (Hooke force) + //##ModelId=45F4D7970009 + float changePercent; // percentage of spring length change + //##ModelId=45F4D7970011 + float maxAllowedDist; // maximum distaance allowed from the rest length + //##ModelId=45F4D7970012 + float ks; // Spring constant + //##ModelId=45F4D7970013 + float kd; // spring damping factor + //##ModelId=45F4D7970022 + Vector normal; // normal vector at the spring of a object + //##ModelId=45F4D7970027 + spring_type type; // different types of springs to construct the object, structural, radium +}; + + +#endif diff --git a/Vector.cpp b/Vector.cpp new file mode 100644 index 0000000..7929313 --- /dev/null +++ b/Vector.cpp @@ -0,0 +1,147 @@ +#include +#include +#include "Vector.h" + + +// Class Vector member functions + +Vector operator+ (const Vector &K, const Vector &N) // #1 fcn - Vector addition for two vectors +{ + cout<<"vecotr +"<x, spaceVertex[f]->y, spaceVertex[f]->z), + vec3(spaceVertex[f+1]->x, spaceVertex[f+1]->y, spaceVertex[f+1]->z), + vec3(spaceVertex[f+2]->x, spaceVertex[f+2]->y, spaceVertex[f+2]->z), + vec3(spaceVertex[f+3]->x, spaceVertex[f+3]->y, spaceVertex[f+3]->z), + + // ceiling + // [color = glColor4f(1, 0.5, 0.1, 0.5)] [normal = (0, -1, 0)] + vec3(spaceVertex[c]->x, spaceVertex[c]->y, spaceVertex[c]->z), + vec3(spaceVertex[c+1]->x, spaceVertex[c+1]->y, spaceVertex[c+1]->z), + vec3(spaceVertex[c+2]->x, spaceVertex[c+2]->y, spaceVertex[c+2]->z), + vec3(spaceVertex[c+3]->x, spaceVertex[c+3]->y, spaceVertex[c+3]->z), + + // back wall + // [color = glColor4f(0, 0, 1, 0.5)] [normal = (0,0,1)] + vec3(spaceVertex[b]->x, spaceVertex[b]->y, spaceVertex[b]->z), + vec3(spaceVertex[b+1]->x, spaceVertex[b+1]->y, spaceVertex[b+1]->z), + vec3(spaceVertex[b+5]->x, spaceVertex[b+5]->y, spaceVertex[b+5]->z), + vec3(spaceVertex[b+4]->x, spaceVertex[b+4]->y, spaceVertex[b+4]->z), + + // right wall + // [color = glColor4f(0.5, 0.9, 0.1, 0.5)] [normal = (-1, 0, 0)] + vec3(spaceVertex[r]->x, spaceVertex[r]->y, spaceVertex[r]->z), + vec3(spaceVertex[r+1]->x, spaceVertex[r+1]->y, spaceVertex[r+1]->z), + vec3(spaceVertex[r+5]->x, spaceVertex[r+5]->y, spaceVertex[r+5]->z), + vec3(spaceVertex[r+4]->x, spaceVertex[r+4]->y, spaceVertex[r+4]->z), + + // left wall + // [color = glColor4f(0.5, 0.9, 0.1, 0.5)] [normal = (1, 0, 0)] + vec3(spaceVertex[l]->x, spaceVertex[l]->y, spaceVertex[l]->z), + vec3(spaceVertex[l+1]->x, spaceVertex[l+1]->y, spaceVertex[l+1]->z), + vec3(spaceVertex[l+5]->x, spaceVertex[l+5]->y, spaceVertex[l+5]->z), + vec3(spaceVertex[l+4]->x, spaceVertex[l+4]->y, spaceVertex[l+4]->z) + }; + + glGenVertexArrays(1, &VAO); + glBindVertexArray(VAO); + + glGenBuffers(1, &VBO); + glBindBuffer(GL_ARRAY_BUFFER, VBO); + // since the box doesn't change, use static draw + glBufferData(GL_ARRAY_BUFFER, sizeof(vertexArray), vertexArray, GL_STATIC_DRAW); + + // set attribute + glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(vec3), (void*)0); + glEnableVertexAttribArray(0); + + glBindBuffer(GL_ARRAY_BUFFER, 0); + glBindVertexArray(0); +} + +//##ModelId=45F4D79602F5 +void ViewSpace::Draw(int shaderID) // Draw the view space +{ + glBindVertexArray(VAO); + + // set world matrix + glUniformMatrix4fv(glGetUniformLocation(shaderID, "worldMatrix"), 1, GL_FALSE, &mat4(1.0f)[0][0]); + // get color location + GLuint ColorLocation = glGetUniformLocation(shaderID, "colorChoice"); + glDisable(GL_CULL_FACE); + // set color and draw + // floor + glEnableClientState(GL_VERTEX_ARRAY); + glUniform4f(ColorLocation, 1, 0.5, 0.1, 0.5); + // TODO: I don't know why GL_QUAD is not working, but GL_TRIANGLE_FAN is working + glDrawArrays(GL_TRIANGLE_FAN, 0, 4); + // draw ceiling + glUniform4f(ColorLocation, 1, 0.5, 0.1, 0.5); + glDrawArrays(GL_TRIANGLE_FAN, 4, 4); + // draw backwall + glUniform4f(ColorLocation, 0, 0, 1, 0.5); + glDrawArrays(GL_TRIANGLE_FAN, 8, 4); + // draw right wall + glUniform4f(ColorLocation, 0.5, 0.9, 0.1, 0.5); + glDrawArrays(GL_TRIANGLE_FAN, 12, 4); + // draw left wall + glUniform4f(ColorLocation, 0.5, 0.9, 0.1, 0.5); + glDrawArrays(GL_TRIANGLE_FAN, 16, 4); + + glEnable(GL_CULL_FACE); + glBindVertexArray(0); +} diff --git a/ViewSpace.h b/ViewSpace.h new file mode 100644 index 0000000..4197ecb --- /dev/null +++ b/ViewSpace.h @@ -0,0 +1,49 @@ +#ifndef VIEWSPACE_H +#define VIEWSPACE_H + +#include "Vector.h" + +// A 3D space containing the balls + + +//##ModelId=45F4D79602F2 +class ViewSpace // constructor +{ +public: + //##ModelId=45F4D79602F3 + ViewSpace() + { + spaceVertex[0]= new Vector( LIMIT, -LIMIT, LIMIT); + spaceVertex[1]= new Vector( LIMIT, -LIMIT, -LIMIT); + spaceVertex[2]= new Vector(-LIMIT, -LIMIT, -LIMIT); + spaceVertex[3]= new Vector(-LIMIT, -LIMIT, LIMIT); + + spaceVertex[4]= new Vector( LIMIT, LIMIT, LIMIT); + spaceVertex[5]= new Vector( LIMIT, LIMIT, -LIMIT); + spaceVertex[6]= new Vector(-LIMIT, LIMIT, -LIMIT); + spaceVertex[7]= new Vector(-LIMIT, LIMIT, LIMIT); + createVertexArrayObject(); + } + + //##ModelId=45F4D79602F4 + ~ViewSpace() // destructor + { + for(int i = 0; i < 8; i++) + { + delete spaceVertex[i]; + } + } + + //##ModelId=45F4D79602F5 + void Draw(int shaderID); + void createVertexArrayObject(); + +public: + //##ModelId=45F4D7960300 + Vector* spaceVertex[8]; + GLuint VAO; + GLuint VBO; +}; + + +#endif diff --git a/global.h b/global.h new file mode 100644 index 0000000..cc0054b --- /dev/null +++ b/global.h @@ -0,0 +1,88 @@ +#ifndef _GLOBAL_H +#define _GLOBAL_H +#define GLEW_STATIC +#include +#include +#include "shader.h" +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace std; + +// for 3D demo +///* +#define DT 0.003f // Delta time in derivatives + +#define KS 800.0f // Hooke spring constant +#define KD 15.0f // Hooke spring damping constant + +#define RKS 700.0f // radium spring constant +#define RKD 50.0f // radium spring damping constant + +#define MOUSE_KS 850.0f // mouse spring constant +#define MOUSE_KD 25.0f // mouse damping constant +#define MOUSE_REST 2.0f // mouse rest lenth + + +#define PI 3.1416f // Circle = 2*PI*RADIUS +#define GY -10.0f // Gravity +#define WIND 10.0f // External force, such as wind force +#define PRESSURE 300.0f // Pressure constant + +#define LIMIT 5.0f // Collision detection floor and wall screen size +#define Width 1024 // Display window width +#define Height 768 // Display window Height + +#define SCRSIZE 10 // screen size factor for mouse dragging +#define MAX_POINTS_SPRINGS 40 //reserve temp space particle and spring +#define NUMPOINTS 10 //default number of particles on a object +#define NUMSPRINGS NUMPOINTS //default number of springs on a object +#define MASS 2.0f // point mass on the rings = 5.0 +#define RING_RADIUS 1.0f // default radius of the 2D and 3D object +//*/ +/* +#define DT 0.003f // Delta time in derivatives + +#define KS 600.0f // Hooke spring constant +#define KD 15.0f // Hooke spring damping constant + +#define RKS 600.0f // radium spring constant +#define RKD 5.0f // radium spring damping constant + +#define MOUSE_KS 150.0f // mouse spring constant +#define MOUSE_KD 25.0f // mouse damping constant +#define MOUSE_REST 2.0f // mouse rest lenth + + +#define PI 3.1416f // Circle = 2*PI*RADIUS +#define GY -10.0f // Gravity +#define WIND 10.0f // External force, such as wind force +#define PRESSURE 880.0f // Pressure constant + +#define LIMIT 5.0f // Collision detection floor and wall screen size +#define Width 500 // Display window width +#define Height 500 // Display window Height + +#define SCRSIZE 10 // screen size factor for mouse dragging +#define MAX_POINTS_SPRINGS 40 //reserve temp space particle and spring +#define NUMPOINTS 10 //default number of particles on a object +#define NUMSPRINGS NUMPOINTS //default number of springs on a object +#define MASS 0.2f // point mass on the rings = 5.0 +#define RING_RADIUS 0.5f // default radius of the 2D and 3D object + +*/ + +enum dimensionality {DIM1D, DIM2D, DIM3D}; //object type, 1D, 2D, and 3D + +enum integrator_type {EULER, MIDPOINT, RK4}; //integrator type, EULER, MIDPOINT, RK4 + +enum spring_type { SPRING_STRUCTURAL, SPRING_RADIUM, SPRING_SHEAR}; //spring type, structrual, radium, shear + + +#endif /* _GLOBAL_H */ diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..06a86f5 --- /dev/null +++ b/main.cpp @@ -0,0 +1,191 @@ + +#include "global.h" +#include "ViewSpace.h" +#include "Object1D.h" +#include "Object2D.h" +#include "Object3D.h" + +int main() +{ + // ****************** initialization process **************************************** + // initialize glfw + glfwInit(); + + // set the window version to 3.3 + glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); + glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); + glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); + // for mac only, for crossplatform purpose + glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); + + // prevent the window from resizing itself +// glfwWindowHint(GLFW_RESIZABLE, GL_FALSE); + + // create a window + GLFWwindow *window = glfwCreateWindow(Width, Height, "COMP371 PA3", nullptr, nullptr); + + // for mac to adjust the window size + int screenWidth, screenHeight; + glfwGetFramebufferSize(window, &screenWidth, &screenHeight); + + // check if the window has been created correctly + if (window == nullptr) + { + std::cerr << "Failed to create GLFW window" << std::endl; + glfwTerminate(); + return -1; + } + + glfwMakeContextCurrent(window); + + // initialize glew + glewExperimental = true; + if(GLEW_OK != glewInit()) + { + std::cerr << "Failed to create GLEW" << std::endl; + glfwTerminate(); + return -1; + } + + // Compile and link shaders here ... + Shader shader("resources/shaders/core.vs", "resources/shaders/core.fs"); + shader.use(); + // ******************** end of system initializatoin *************************************** + + + // *****************Variables declarations******************* + float lastFrameTime = glfwGetTime(); // time is in seconds + float movementSpeed = 1.0f; + float initialEyeX = 0.0f; + float initialEyeY = 0.0f; + float initialEyeZ = 7.0f; + glm::vec3 initialEye(initialEyeX, initialEyeY, initialEyeZ); + + int mousedown = 0; + float xMouse, yMouse; + // ******************* end of variable declaration*********** + + + // ******************* set initial camera position ***************** + glm::vec3 eye = initialEye; + glm::vec3 center(0.0f, 0.0f, 0.0f); + glm::vec3 cameraUp(0.0f, 1.0f, 0.0f); + glm::mat4 projectionMatrix = glm::perspective(glm::radians(70.0f), float(Width) / float(Height), 0.01f, 100.0f); + GLuint projectionMatrixLocation = glGetUniformLocation(shader.program, "projectionMatrix"); + glUniformMatrix4fv(projectionMatrixLocation, 1, GL_FALSE, &projectionMatrix[0][0]); + glm::mat4 viewMatrix = glm::lookAt(eye, // eye + center, // center + cameraUp); + GLuint viewMatrixLocation = glGetUniformLocation(shader.program, "viewMatrix"); + glUniformMatrix4fv(viewMatrixLocation, 1, GL_FALSE, &viewMatrix[0][0]); + + //**************** end of initial camera position ******************************************* + + + // enable backface culling and depth test +// glEnable(GL_CULL_FACE); +// glEnable(GL_DEPTH_TEST); + glEnable(GL_BLEND); // transparent + glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA); + + ViewSpace box; + Object1D object1D; + Object2D object2D; + Object3D object3D; + + // set integrator type + object1D.setIntegratorType(RK4); + object2D.setIntegratorType(RK4); + object3D.setIntegratorType(RK4); + // main game loop + while(!glfwWindowShouldClose(window)) + { + // detect inputs from keyboard + glfwPollEvents(); + + // set background color (r, g, b, alpha) + // can either do this inside the loop or outside the loop + glClearColor(0.0f, 0.0f, 0.0f, 1.0f); + + // Each frame, reset color of each pixel to glClearColor and clear depth buffer bit + glClear(GL_COLOR_BUFFER_BIT); + + // compute the rotational angle every loop. + float dt = glfwGetTime() - lastFrameTime; + lastFrameTime += dt; + + + + //============== updating =========== + // update mouse position + double x = Width / 2.0, y = Height / 2.0; + glfwGetCursorPos(window, &x, &y); + xMouse = (4 * ((float)x / (float)Width)) - 2; + yMouse = -((4 * ((float)y / (float)Height)) - 2); + // update object + object1D.Update(DT, mousedown != 0, xMouse, yMouse); + object2D.Update(DT, mousedown != 0, xMouse, yMouse); + object3D.Update(DT, mousedown != 0, xMouse, yMouse); + + // ============ drawing ======================= + glm::mat4 sbMatrix = glm::mat4(1.0f); // can possibly apply glm::translate(), glm::rotate(), and glm::scale() here + box.Draw(shader.program); + object1D.Draw(shader.program, sbMatrix); + object2D.Draw(shader.program, sbMatrix); + object3D.Draw(shader.program, sbMatrix); + + // End of frame, used 2 buffers for displaying, this is swapping the buffer + glfwSwapBuffers(window); + + // ************** handle inputs ************************* + + if(glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_LEFT) == GLFW_PRESS) + { + mousedown = 1; + } + else{ + mousedown = 0; + } + + + if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS) + { + glfwSetWindowShouldClose(window, true); + } + + + if (glfwGetKey(window, GLFW_KEY_UP) == GLFW_PRESS) + { + glm::vec3 direction(0.0f, 2.0f, 0.0f); + eye = eye + direction * movementSpeed * dt; + center = center + direction * movementSpeed * dt; + + } + if (glfwGetKey(window, GLFW_KEY_DOWN) == GLFW_PRESS) + { + glm::vec3 direction(0.0f, -2.0f, 0.0f); + eye = eye + direction * movementSpeed * dt; + center = center + direction * movementSpeed * dt; + } + + // update the view matrix + glm::mat4 viewMatrix = glm::lookAt(eye, // eye + center, // center + cameraUp); + GLuint viewMatrixLocation = glGetUniformLocation(shader.program, "viewMatrix"); + glUniformMatrix4fv(viewMatrixLocation, 1, GL_FALSE, &viewMatrix[0][0]); + + } + + + glfwTerminate(); + return 0; +} + + + + + + + + diff --git a/resources/shaders/core.fs b/resources/shaders/core.fs new file mode 100644 index 0000000..0d3becf --- /dev/null +++ b/resources/shaders/core.fs @@ -0,0 +1,7 @@ +#version 330 core +in vec4 vertexColor; +out vec4 FragColor; +void main() +{ + FragColor = vec4(vertexColor.r, vertexColor.g, vertexColor.b, vertexColor.a); +} diff --git a/resources/shaders/core.vs b/resources/shaders/core.vs new file mode 100644 index 0000000..2e94488 --- /dev/null +++ b/resources/shaders/core.vs @@ -0,0 +1,20 @@ +#version 330 core +// (For vertex position) attribute position; vector size and name +layout (location = 0) in vec3 aPos; +layout (location = 1) in vec3 aColor; + +// uniform matrix for transformation, scaling and rotating +uniform mat4 worldMatrix; +// uniform view matrix +uniform mat4 viewMatrix; +uniform mat4 projectionMatrix; +uniform vec4 colorChoice; +out vec4 vertexColor; + +void main() +{ + vertexColor = colorChoice; +// vertexColor = aColor; + gl_Position = projectionMatrix * viewMatrix * worldMatrix * vec4(aPos.x, aPos.y, aPos.z, 1.0); + +} diff --git a/shader.h b/shader.h new file mode 100644 index 0000000..e5c86f6 --- /dev/null +++ b/shader.h @@ -0,0 +1,103 @@ +#ifndef SHADER_H +#define SHADER_H + +#include +#include +#include +#include + +#include +using namespace std; + +class Shader{ +public: + int program; + Shader(const char* vertexPath, const GLchar *fragmentPath) + { + // 1. retrieve the vertex/fragment source code from filepath + string vertexCode; + string fragmentCode; + ifstream vShaderFile; + ifstream fShaderFile; + // ensures ifstream objects can throw exceptions + vShaderFile.exceptions(ifstream::badbit); + fShaderFile.exceptions(ifstream::badbit); + + try{ + // open files + vShaderFile.open(vertexPath); + fShaderFile.open(fragmentPath); + stringstream vShaderStream, fShaderStream; + // read file's buffer contents into streams + vShaderStream << vShaderFile.rdbuf(); + fShaderStream << fShaderFile.rdbuf(); + // close file handlers + vShaderFile.close(); + fShaderFile.close(); + // convert stream into string + vertexCode = vShaderStream.str(); + fragmentCode = fShaderStream.str(); + } + catch(ifstream::failure e) + { + cout << "ERROR:SHADER::FILE_NOT_SUCCESSFULLY_READ" << endl; + } + + // vertex shader + int vertexShader = glCreateShader(GL_VERTEX_SHADER); + const char* vertexShaderSource = vertexCode.c_str(); + glShaderSource(vertexShader, 1, &vertexShaderSource, NULL); + glCompileShader(vertexShader); + + // check for shader compile errors + int success; + char infoLog[512]; + glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &success); + if (!success) + { + glGetShaderInfoLog(vertexShader, 512, NULL, infoLog); + std::cerr << "ERROR::SHADER::VERTEX::COMPILATION_FAILED\n" << infoLog << std::endl; + } + + // fragment shader + int fragmentShader = glCreateShader(GL_FRAGMENT_SHADER); + const char* fragmentShaderSource = fragmentCode.c_str(); + glShaderSource(fragmentShader, 1, &fragmentShaderSource, NULL); + glCompileShader(fragmentShader); + + // check for shader compile errors + glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &success); + if (!success) + { + glGetShaderInfoLog(fragmentShader, 512, NULL, infoLog); + std::cerr << "ERROR::SHADER::FRAGMENT::COMPILATION_FAILED\n" << infoLog << std::endl; + } + + // link shaders + this->program = glCreateProgram(); + glAttachShader(this->program, vertexShader); + glAttachShader(this->program, fragmentShader); + glLinkProgram(this->program); + + // check for linking errors + glGetProgramiv(this->program, GL_LINK_STATUS, &success); + if (!success) { + glGetProgramInfoLog(this->program, 512, NULL, infoLog); + std::cerr << "ERROR::SHADER::PROGRAM::LINKING_FAILED\n" << infoLog << std::endl; + } + + + + glDeleteShader(vertexShader); + glDeleteShader(fragmentShader); + + } + + // use the current shader + void use() + { + glUseProgram(this->program); + } +}; + +#endif diff --git a/src/SoftBodySimulation/Makefile b/src/SoftBodySimulation/Makefile deleted file mode 100644 index 2080154..0000000 --- a/src/SoftBodySimulation/Makefile +++ /dev/null @@ -1,40 +0,0 @@ -CC = g++ -CPPFLAGS = -g # -Wall -Wmissing-declarations -Wmissing-prototypes -ansi -pedantic -LIBS = -L/encs/lib/ -lglut -lGL -lGLU -lGL -L/usr/X11R6/lib -lXmu -lXext -lXt -lXi -lX11 -TARGET = softbody -INCLUDE = -I/encs/include/GL -I../gfx -I../common - -OBJECTS := \ - common/Object.o \ - common/Object1D.o \ - common/Object2D.o \ - common/Object3D.o \ - common/Vector.o \ - common/Spring.o \ - common/Particle.o \ - common/Face.o \ - simulation/ViewSpace.o \ - simulation/Simulation.o \ - simulation/EulerIntegrator.o \ - simulation/Integrator.o \ - simulation/MidpointIntegrator.o \ - simulation/RungeKutta4Integrator.o - -.SUFFIXES: .cpp - -.cpp.o: - $(CC) -c $(CPPFLAGS) $(INCLUDE) -o $@ $< - - -all: $(TARGET) - -$(TARGET): - make -C common all - make -C simulation all - $(CC) $(LIBS) $(OBJECTS) -o $(TARGET) - -clean: - make -C common $@ - make -C simulation $@ - -rm -rf $(TARGET) *.pch *.ilk *.opt *.plg *.ncb Debug Release *.o - diff --git a/src/SoftBodySimulation/SoftBodySimulation.dsp b/src/SoftBodySimulation/SoftBodySimulation.dsp deleted file mode 100644 index 3ce4b85..0000000 --- a/src/SoftBodySimulation/SoftBodySimulation.dsp +++ /dev/null @@ -1,221 +0,0 @@ -# Microsoft Developer Studio Project File - Name="SoftBodySimulation" - Package Owner=<4> -# Microsoft Developer Studio Generated Build File, Format Version 6.00 -# ** DO NOT EDIT ** - -# TARGTYPE "Win32 (x86) Console Application" 0x0103 - -CFG=SoftBodySimulation - Win32 Debug -!MESSAGE This is not a valid makefile. To build this project using NMAKE, -!MESSAGE use the Export Makefile command and run -!MESSAGE -!MESSAGE NMAKE /f "SoftBodySimulation.mak". -!MESSAGE -!MESSAGE You can specify a configuration when running NMAKE -!MESSAGE by defining the macro CFG on the command line. For example: -!MESSAGE -!MESSAGE NMAKE /f "SoftBodySimulation.mak" CFG="SoftBodySimulation - Win32 Debug" -!MESSAGE -!MESSAGE Possible choices for configuration are: -!MESSAGE -!MESSAGE "SoftBodySimulation - Win32 Release" (based on "Win32 (x86) Console Application") -!MESSAGE "SoftBodySimulation - Win32 Debug" (based on "Win32 (x86) Console Application") -!MESSAGE - -# Begin Project -# PROP AllowPerConfigDependencies 0 -# PROP Scc_ProjName "" -# PROP Scc_LocalPath "" -CPP=cl.exe -RSC=rc.exe - -!IF "$(CFG)" == "SoftBodySimulation - Win32 Release" - -# PROP BASE Use_MFC 0 -# PROP BASE Use_Debug_Libraries 0 -# PROP BASE Output_Dir "Release" -# PROP BASE Intermediate_Dir "Release" -# PROP BASE Target_Dir "" -# PROP Use_MFC 0 -# PROP Use_Debug_Libraries 0 -# PROP Output_Dir "Release" -# PROP Intermediate_Dir "Release" -# PROP Target_Dir "" -# ADD BASE CPP /nologo /W3 /GX /O2 /D "WIN32" /D "NDEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /c -# ADD CPP /nologo /W3 /GX /O2 /D "WIN32" /D "NDEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /c -# ADD BASE RSC /l 0x1009 /d "NDEBUG" -# ADD RSC /l 0x1009 /d "NDEBUG" -BSC32=bscmake.exe -# ADD BASE BSC32 /nologo -# ADD BSC32 /nologo -LINK32=link.exe -# ADD BASE LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /machine:I386 -# ADD LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /machine:I386 - -!ELSEIF "$(CFG)" == "SoftBodySimulation - Win32 Debug" - -# PROP BASE Use_MFC 0 -# PROP BASE Use_Debug_Libraries 1 -# PROP BASE Output_Dir "Debug" -# PROP BASE Intermediate_Dir "Debug" -# PROP BASE Target_Dir "" -# PROP Use_MFC 0 -# PROP Use_Debug_Libraries 1 -# PROP Output_Dir "Debug" -# PROP Intermediate_Dir "Debug" -# PROP Target_Dir "" -# ADD BASE CPP /nologo /W3 /Gm /GX /ZI /Od /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /GZ /c -# ADD CPP /nologo /W3 /Gm /GX /ZI /Od /I "include" /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /GZ /c -# ADD BASE RSC /l 0x1009 /d "_DEBUG" -# ADD RSC /l 0x1009 /d "_DEBUG" -BSC32=bscmake.exe -# ADD BASE BSC32 /nologo -# ADD BSC32 /nologo -LINK32=link.exe -# ADD BASE LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /debug /machine:I386 /pdbtype:sept -# ADD LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /debug /machine:I386 /pdbtype:sept - -!ENDIF - -# Begin Target - -# Name "SoftBodySimulation - Win32 Release" -# Name "SoftBodySimulation - Win32 Debug" -# Begin Group "Source Files" - -# PROP Default_Filter "cpp;c;cxx;rc;def;r;odl;idl;hpj;bat" -# Begin Source File - -SOURCE=.\simulation\EulerIntegrator.cpp -# End Source File -# Begin Source File - -SOURCE=.\common\Face.cpp -# End Source File -# Begin Source File - -SOURCE=.\simulation\Integrator.cpp -# End Source File -# Begin Source File - -SOURCE=.\simulation\MidpointIntegrator.cpp -# End Source File -# Begin Source File - -SOURCE=.\common\Object.cpp -# End Source File -# Begin Source File - -SOURCE=.\common\Object1D.cpp -# End Source File -# Begin Source File - -SOURCE=.\common\Object2D.cpp -# End Source File -# Begin Source File - -SOURCE=.\common\Object3D.cpp -# End Source File -# Begin Source File - -SOURCE=.\common\Particle.cpp -# End Source File -# Begin Source File - -SOURCE=.\simulation\RungeKutta4Integrator.cpp -# End Source File -# Begin Source File - -SOURCE=.\simulation\Simulation.cpp -# End Source File -# Begin Source File - -SOURCE=.\common\Spring.cpp -# End Source File -# Begin Source File - -SOURCE=.\common\Vector.cpp - -!IF "$(CFG)" == "SoftBodySimulation - Win32 Release" - -!ELSEIF "$(CFG)" == "SoftBodySimulation - Win32 Debug" - -# ADD CPP /I "\include" - -!ENDIF - -# End Source File -# Begin Source File - -SOURCE=.\simulation\ViewSpace.cpp -# End Source File -# End Group -# Begin Group "Header Files" - -# PROP Default_Filter "h;hpp;hxx;hm;inl" -# Begin Source File - -SOURCE=.\include\EulerIntegrator.h -# End Source File -# Begin Source File - -SOURCE=.\include\Face.h -# End Source File -# Begin Source File - -SOURCE=.\include\global.h -# End Source File -# Begin Source File - -SOURCE=.\include\Integrator.h -# End Source File -# Begin Source File - -SOURCE=.\include\MidpointIntegrator.h -# End Source File -# Begin Source File - -SOURCE=.\include\Object.h -# End Source File -# Begin Source File - -SOURCE=.\include\Object1D.h -# End Source File -# Begin Source File - -SOURCE=.\include\Object2D.h -# End Source File -# Begin Source File - -SOURCE=.\include\Object3D.h -# End Source File -# Begin Source File - -SOURCE=.\include\Particle.h -# End Source File -# Begin Source File - -SOURCE=.\include\RungeKutta4Integrator.h -# End Source File -# Begin Source File - -SOURCE=.\include\Simulation.h -# End Source File -# Begin Source File - -SOURCE=.\include\Spring.h -# End Source File -# Begin Source File - -SOURCE=.\include\Vector.h -# End Source File -# Begin Source File - -SOURCE=.\include\ViewSpace.h -# End Source File -# End Group -# Begin Group "Resource Files" - -# PROP Default_Filter "ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe" -# End Group -# End Target -# End Project diff --git a/src/SoftBodySimulation/SoftBodySimulation.dsw b/src/SoftBodySimulation/SoftBodySimulation.dsw deleted file mode 100644 index 1dde5be..0000000 --- a/src/SoftBodySimulation/SoftBodySimulation.dsw +++ /dev/null @@ -1,29 +0,0 @@ -Microsoft Developer Studio Workspace File, Format Version 6.00 -# WARNING: DO NOT EDIT OR DELETE THIS WORKSPACE FILE! - -############################################################################### - -Project: "SoftBodySimulation"=.\SoftBodySimulation.dsp - Package Owner=<4> - -Package=<5> -{{{ -}}} - -Package=<4> -{{{ -}}} - -############################################################################### - -Global: - -Package=<5> -{{{ -}}} - -Package=<3> -{{{ -}}} - -############################################################################### - diff --git a/src/SoftBodySimulation/common/Face.cpp b/src/SoftBodySimulation/common/Face.cpp deleted file mode 100644 index d46eac9..0000000 --- a/src/SoftBodySimulation/common/Face.cpp +++ /dev/null @@ -1,40 +0,0 @@ -#include "Face.h" -#include - -// Class triface function definition -// Calculate normal vector to a triangle and its field -/** - * This does that. But it also does that too. - * @param fooo dfdjfh d - * @param vla fjhdjfdj - * @return true if succes - * @author - * @version - * @since - * @note ffd - * @see - */ -//##ModelId=45F4D7980066 -void Face::CalNormalNField(void) -{ - // the normal can be calculated by vector cross multiply - -/* cout<<"fp1->r->x=="<r->x<<"==fp1->r->y=="<r->y<<"==fp1->r->z=="<r->z<r->x=="<r->x<<"==fp2->r->y=="<r->y<<"==fp2->r->z=="<r->z<r->x=="<r->x<<"==fp3->r->y=="<r->y<<"==fp3->r->z=="<r->z<r - *fp2->r) ^ (*fp1->r - *fp3->r); -// Vector N = (*fs1->sp1->r - *fs2->sp1->r) ^ (*fs1->sp1->r - *fs3->sp1->r); - - -// cout<<"N.x=="< - -#include "Object.h" -#include "EulerIntegrator.h" -#include "MidpointIntegrator.h" -#include "RungeKutta4Integrator.h" - -//##ModelId=45F4D7970263 -Object::Object() -{ - numParticles = 2; //default particle number on the object - numSprings = 1; //default spring number on the object - integrator = NULL; //default integrator - integratorType = EULER; //default integrator tpye is Euler - dim = DIM1D; //default object is 1D - closest_i=0; // the point which is closest to mouse position -} - -//##ModelId=45F4D7970264 -Object::~Object() -{ - - delete integrator; -} - -//========================================================================== - -//##ModelId=45F4D7970293 -void Object::SetParticles() //model the particles on the object -{ - - assert(false); -} - -//##ModelId=45F4D7970297 -void Object::Add_Structural_Spring(int index, int head, int tail) -{ - assert(false); - -} - -//############################################################################## - -//##ModelId=45F4D7970295 -void Object::SetObject() //model the object -{ - - SetParticles(); //model the particles on the object - -} - -//############################################################################# - -//##ModelId=45F4D7970268 -void Object::setIntegratorType(integrator_type type) //set the integrator tpye as user selects from the GUI -{ - - this->integratorType = type; -} - -//############################################################################# -//update the forces, velocity, and position of the particle on the object - -//##ModelId=45F4D7970274 -void Object::Update(float deltaT, bool drag, float xDrag, float yDrag) -{ - - if(integrator == NULL) - { - - switch(integratorType) - { - case EULER: - integrator = new EulerIntegrator(*this); - break; - - case MIDPOINT: - integrator = new MidpointIntegrator(*this); - break; - - case RK4: - integrator = new RungeKutta4Integrator(*this); - break; - - default: - assert(false); - return; - } - - integrator->setDimension(dim); - } - - integrator->integrate(deltaT, drag, xDrag, yDrag); -} - - -/////////////////////////////////////////////////////////// - -//##ModelId=45F4D7970286 -void Object::FindClosestPoint(void) -{ - float dmin = 0; - float mousepointd = 0; - int i; - - // find closest point - dmin = sqrt(pow(outer_points[closest_i]->r->x - integrator->mDragX,2) + pow(outer_points[closest_i]->r->y - integrator->mDragY,2)); - - for(i=0; ir->x - integrator->mDragX,2) + - pow(outer_points[i]->r->y - integrator->mDragY,2)); - if(mousepointd < dmin) - { - dmin = mousepointd; - closest_i = i; - } - } - - //cout<<" closest_i=="<< closest_i< -//##ModelId=45F4D79701B9 -Object1D::Object1D() -{ - SetObject(); - dim = DIM1D; -} - - -//##ModelId=45F4D79701BA -Object1D::~Object1D() -{ -} - -//##ModelId=45F4D79701C7 -void Object1D::Draw() -{ - static bool findOnce = false; - - glPushMatrix(); - glBegin(GL_LINES); // the draw of inner circle - for(int i = 0 ; i < outer_springs.size(); i++) - { - glColor3f(1.0,1.0,1.0); - glVertex2f(outer_springs[i]->sp1->r->x,outer_springs[i]->sp1->r->y); - glVertex2f(outer_springs[i]->sp2->r->x,outer_springs[i]->sp2->r->y); - - } - glEnd(); - glBegin(GL_POINTS); - for(int j = 0 ; j < outer_springs.size(); j++) - { - glVertex2f(outer_springs[j]->sp1->r->x,outer_springs[j]->sp1->r->y); - glVertex2f(outer_springs[j]->sp2->r->x,outer_springs[j]->sp2->r->y); - } - glEnd(); - - if(integrator->dragExists) - { - if(findOnce == true) - { - FindClosestPoint(); - findOnce = false; - } - - // cout<<"mDragX="<mDragX<<",mDragY="<mDragY<<",x="<r->x<<",y="<r->y<mDragX, integrator->mDragY); - glVertex2f(outer_points[closest_i]->r->x,outer_points[closest_i]->r->y); - glEnd(); - } - else - { - FindClosestPoint(); - findOnce = true; - } - - - glPopMatrix(); - - -} - -//==================================================================== - -//##ModelId=45F4D79701C9 -void Object1D::Add_Structural_Spring(int index, int head, int tail) -{ - - outer_springs.push_back( new Spring(outer_points[head],outer_points[tail])); - inner_springs.push_back( new Spring()); - - /* outer_springs[index].sp1 = &outer_points[head]; - outer_springs[index].sp2 = &outer_points[tail]; - - inner_springs[index].sp1 = new Particle(); - inner_springs[index].sp2 = new Particle(); - - outer_springs[index].setRestLen(); - */ - inner_springs[index]->setRestLen(); - outer_springs[index]->setRestLen(); -} - - -//================================================================ -//##ModelId=45F4D79701CD -void Object1D::SetObject() -{ - - - int PosX = 0; - int PosY = 0; - int i; -// cout<<"numParticles---1D------"<x += PosX ; // outer X coordiation - outer_points[i].r->y += PosY; // outer Y coordiation - outer_points[i].mass = MASS; - */ - PosY += 2; - } - - - for(i=0; i<1 ;i++) // NUMP-1 springs from 1st to the NUMP for outer & inner - { - Add_Structural_Spring(i, i, (i+1) % numParticles); - } -} - - -//======================================================= -/* -void Object1D::FindClosestPoint(void) -{ - float dmin = 0; - float mousepointd = 0; - int i; - - // find closest point - dmin = sqrt(pow(outer_points[closest_i]->r->x - integrator->mDragX,2) + pow(outer_points[closest_i]->r->y - integrator->mDragY,2)); - - for(i=0; ir->x - integrator->mDragX,2) + - pow(outer_points[i]->r->y - integrator->mDragY,2)); - if(mousepointd < dmin) - { - dmin = mousepointd; - closest_i = i; - } - } - - cout<<" closest_i=="<< closest_i<fp1->r->x, outer_faces[i]->fp1->r->y, outer_faces[i]->fp1->r->z); - - glVertex3f((outer_faces[i]->normal->x + outer_faces[i]->fp1->r->x), - (outer_faces[i]->normal->y + outer_faces[i]->fp1->r->y), - (outer_faces[i]->normal->z + outer_faces[i]->fp1->r->z)); - - - } - glEnd(); - - glBegin(GL_POLYGON); // the draw of outer circle - for(i=0 ; ir->x, outer_points[i]->r->y); - } - glEnd(); - glPopMatrix(); - - glPushMatrix(); - glColor4f(0.0,1.0,0.0, 0.5 ); - glBegin(GL_POLYGON); // the draw of outer circle - for(i=0 ; ir->x, inner_points[i]->r->y); - } - glEnd(); - glPopMatrix(); - - glPushMatrix(); - glBegin(GL_LINES); // the draw of outer circle - for(i=0 ; isp1->r->x,outer_springs[i]->sp1->r->y); - glVertex2f(outer_springs[i]->sp2->r->x,outer_springs[i]->sp2->r->y); - - } - glEnd(); - - - glBegin(GL_POINTS); - for(i = 0 ; i < outer_springs.size(); i++) - { - // cout<<"i================"<sp1->r->x,outer_springs[i]->sp1->r->y); - // glVertex2f(outer_springs[i]->sp2->r->x,outer_springs[i]->sp2->r->y); - } - glEnd(); - - glBegin(GL_LINES); // the draw of inner circle - for(i=0 ; isp1->r->x,inner_springs[i]->sp1->r->y); - glVertex2f(inner_springs[i]->sp2->r->x,inner_springs[i]->sp2->r->y); - } - glEnd(); - - - glBegin(GL_POINTS); - for(i = 0 ; i < inner_springs.size(); i++) - { glColor3f(1.0,0.0,0.0); - glVertex2f(inner_springs[i]->sp1->r->x,inner_springs[i]->sp1->r->y); - // glVertex2f(inner_springs[i]->sp2->r->x,inner_springs[i]->sp2->r->y); - } - glEnd(); - - glPopMatrix(); - - - glPushMatrix(); - glBegin(GL_LINES); // the draw of radium lines from inner to outer - for(i=0 ; isp1->r->x, radium_springs[i]->sp1->r->y); - glVertex2f(radium_springs[i]->sp2->r->x, radium_springs[i]->sp2->r->y); - } - glEnd(); - glPopMatrix(); - - - glPushMatrix(); - glBegin(GL_LINES); // the left shear lines from inner to outer - for(i=0 ; isp1->r->x,shear_springs_left[i]->sp1->r->y); - glVertex2f(shear_springs_left[i]->sp2->r->x,shear_springs_left[i]->sp2->r->y); - } - glEnd(); - glPopMatrix(); - - - - glPushMatrix(); - glBegin(GL_LINES); // the right shear lines from inner to outer - for(i=0 ; isp1->r->x,shear_springs_right[i]->sp1->r->y); - glVertex2f(shear_springs_right[i]->sp2->r->x,shear_springs_right[i]->sp2->r->y); - } - glEnd(); - glPopMatrix(); - - if(integrator->dragExists) - { - if(findOnce == true) - { - FindClosestPoint(); - findOnce = false; - } - - glColor3f(1,1,0); // A white line between the Object3D point and the mouse point - glBegin(GL_LINES); - glVertex2f(integrator->mDragX, integrator->mDragY); - glVertex2f(outer_points[closest_i]->r->x,outer_points[closest_i]->r->y); - glEnd(); - } - else - { - FindClosestPoint(); - findOnce = true; - } - - - glEnable(GL_DEPTH_TEST); -} - -/* Function of adding a new spring on the inner rings*/ - -//##ModelId=45F4D797018A -void Object2D::Add_Structural_Spring(int index, int h, int t) -{ - - inner_springs.push_back( new Spring(inner_points[h],inner_points[t])); - outer_springs.push_back( new Spring(outer_points[h],outer_points[t])); - - inner_springs[index]->setRestLen(); //set the inner spring's length - outer_springs[index]->setRestLen(); //set the outer spring's length -} - - -//##ModelId=45F4D7970198 -void Object2D::Add_Radium_Spring(int index) -{ - - radium_springs.push_back( new Spring(inner_points[index],outer_points[index])); - radium_springs[index]->setRestLen(); //set the radium spring's length -} - - -//##ModelId=45F4D797019A -void Object2D::Add_Shear_Spring(int index, int h, int t ) -{ - - - shear_springs_left.push_back(new Spring(inner_points[h],outer_points[t])); - shear_springs_right.push_back(new Spring(inner_points[t],outer_points[h])); - - - shear_springs_left[index]->setRestLen(); //set the shear left spring's length - shear_springs_right[index]->setRestLen(); //set the shear right spring's length - -} - -/* Create 2 2D-rings (points + springs) */ -//##ModelId=45F4D797017F -void Object2D::SetObject(void) -{ - float tix, tiy, tiz, tox,toy, toz; - - tix=tiy=tiz=tox=toy=toz=0; - outer_points.clear(); - inner_points.clear(); - outer_springs.clear(); - inner_springs.clear(); - outer_faces.clear(); - inner_faces.clear(); - int i; - for(i=0; iCalNormalNField(); - - outer_faces[i]->CalNormalNField(); -// cout<<"outer_faces[i]->normal->x==="<normal->x<<"=== outer_faces[i]->normal->y==="<normal->y<<"===outer_faces[i]->normal->z===="<normal->z<fp1->r->x=="<fp1->r->x<<"==outer_faces[0]->fp1->r->y=="<fp1->r->y<<"==outer_faces[0]->fp1->r->z=="<fp1->r->z<fp2->r->x=="<fp2->r->x<<"==outer_faces[0]->fp2->r->y=="<fp2->r->y<<"==outer_faces[0]->fp2->r->z=="<fp2->r->z<fp3->r->x=="<fp3->r->x<<"==outer_faces[0]->fp3->r->y=="<fp3->r->y<<"==outer_faces[0]->fp3->r->z=="<fp3->r->z<CalNormalNField(); - cout<<"outer_faces[0]->normal->x==="<normal->x<<"=== outer_faces[0]->normal->y==="<normal->y<<"===outer_faces[0]->normal->z===="<normal->z< - -//##ModelId=45F4D79700EE -Object3D::Object3D() -{ //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -/* pa = new Particle(new Vector(0,0,0), MASS); - pb = new Particle(new Vector(0,0,0), MASS); - pc = new Particle(new Vector(0,0,0), MASS); -*/ - //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% - - - // It is *IMPORTANT* to realize that most of the time - // the number of particles and springs are not the same - // in 3D (unlike 2D) so these have to be updated to the - // actual number of particles and springs when the geometry - // is initially constructed or altered - numParticles = NUMPOINTS; - numSprings = NUMSPRINGS; - - iterations = 1; - SetObject(); - - dim = DIM3D; -} - - - -//##ModelId=45F4D79700FB -Object3D::~Object3D() -{ -/* delete pa; - delete pb; - delete pc; -*/ -} - - - -//##ModelId=45F4D797010B -void Object3D::Add_Structural_Spring(int index, int h, int t) -{ - // making sure nobody actuall calls it -/* assert(false); - - inner_springs.push_back( new Spring(inner_points[h],inner_points[t])); - outer_springs.push_back( new Spring(outer_points[h],outer_points[t])); - -*/ - -/* inner_springs.push_back( new Spring()); - outer_springs.push_back( new Spring()); -*/ -/* inner_springs[index]->setRestLen(); //set the inner spring's length - outer_springs[index]->setRestLen(); //set the outer spring's length -*/ -} - - -//////////////////////////////////////////////////// - - - -//##ModelId=45F4D797010F -void Object3D::Add_Radium_Spring(int index) -{ - radium_springs.push_back( new Spring(inner_springs[index]->sp1,outer_springs[index]->sp1, 5*KS, 2*KD)); - radium_springs[index]->setRestLen(); //set the radium spring's length -} - - -//##ModelId=45F4D797011B -void Object3D::Add_Shear_Spring(int index ) -{ - shear_springs_left.push_back(new Spring(inner_springs[index]->sp1,outer_springs[index]->sp2, 5*KS, 2*KD)); - shear_springs_right.push_back(new Spring(inner_springs[index]->sp2, outer_springs[index]->sp1,5*KS, 2*KD)); - - shear_springs_left[index]->setRestLen(); //set the shear left spring's length - shear_springs_right[index]->setRestLen(); //set the shear right spring's length -} - - - - - -//============================================================================== -//##ModelId=45F4D79700FF -void Object3D::Iteration() -{ - // Spring triplets counter for older springs particle replacements -// static int j = 0; - - for (int it=0; itr->x=(outer_faces[i]->fp1->r->x + outer_faces[i]->fp2->r->x)/2; - pa->r->y=(outer_faces[i]->fp1->r->y + outer_faces[i]->fp2->r->y)/2; - pa->r->z=(outer_faces[i]->fp1->r->z + outer_faces[i]->fp2->r->z)/2; - - pb->r->x=(outer_faces[i]->fp2->r->x + outer_faces[i]->fp3->r->x)/2; - pb->r->y=(outer_faces[i]->fp2->r->y + outer_faces[i]->fp3->r->y)/2; - pb->r->z=(outer_faces[i]->fp2->r->z + outer_faces[i]->fp3->r->z)/2; - - pc->r->x=(outer_faces[i]->fp1->r->x + outer_faces[i]->fp3->r->x)/2; - pc->r->y=(outer_faces[i]->fp1->r->y + outer_faces[i]->fp3->r->y)/2; - pc->r->z=(outer_faces[i]->fp1->r->z + outer_faces[i]->fp3->r->z)/2; - // - - /* cout<<"before normalization: "<x="<r->x<<"; pa->y="<r->y<<"; pa->z="<r->z<x="<r->x<<"; pb->y="<r->y<<"; pb->z="<r->z<x="<r->x<<"; pc->y="<r->y<<"; pc->z="<r->z<r->Normalize(); - pb->r->Normalize(); - pc->r->Normalize(); - - /* cout<<"after normalization: "<x="<r->x<<"; pa->y="<r->y<<"; pa->z="<r->z<x="<r->x<<"; pb->y="<r->y<<"; pb->z="<r->z<x="<r->x<<"; pc->y="<r->y<<"; pc->z="<r->z<r->x == pa->r->x - && outer_points[o]->r->y == pa->r->y - && outer_points[o]->r->z == pa->r->z - ) - { - delete pa; - pa = outer_points[o]; - a = true; - } - - if - ( - outer_points[o]->r->x == pb->r->x - && outer_points[o]->r->y == pb->r->y - && outer_points[o]->r->z == pb->r->z - ) - { - delete pb; - pb = outer_points[o]; - b = true; - } - - if - ( - outer_points[o]->r->x == pc->r->x - && outer_points[o]->r->y == pc->r->y - && outer_points[o]->r->z == pc->r->z - ) - { - delete pc; - pc = outer_points[o]; - c = true; - } - } - - // The new points are added to the general collection of points - if(!a) outer_points.push_back(pa); - if(!b) outer_points.push_back(pb); - if(!c) outer_points.push_back(pc); - - - // Create three new faces; these represent the three outer triangles - outer_faces.push_back(new Face(outer_faces[i]->fp1, pa, pc, outer_springs)); // Top - outer_faces.push_back(new Face(pa, outer_faces[i]->fp2, pb, outer_springs)); // Bottom-left -// outer_faces.push_back(new Face(pb, outer_faces[i]->fp3, pc, outer_springs)); // Bottom-right - outer_faces.push_back(new Face(pc, pb, outer_faces[i]->fp3, outer_springs)); // Bottom-right - - - // Replace the original face's points with its new one in the middle - - outer_faces[i]->fp1 = pa; - outer_faces[i]->fp2 = pb; - outer_faces[i]->fp3 = pc; - - - outer_faces[i]->fs1->sp1 = pa; - outer_faces[i]->fs1->sp2 = pb; - - outer_faces[i]->fs2->sp1 = pb; - outer_faces[i]->fs2->sp2 = pc; - - outer_faces[i]->fs3->sp1 = pc; - outer_faces[i]->fs3->sp2 = pa; - } - - initialFacesCount = inner_faces.size(); - - for(i=0;ir->x=(inner_faces[i]->fp1->r->x + inner_faces[i]->fp2->r->x)/2; - pa->r->y=(inner_faces[i]->fp1->r->y + inner_faces[i]->fp2->r->y)/2; - pa->r->z=(inner_faces[i]->fp1->r->z + inner_faces[i]->fp2->r->z)/2; - - pb->r->x=(inner_faces[i]->fp2->r->x + inner_faces[i]->fp3->r->x)/2; - pb->r->y=(inner_faces[i]->fp2->r->y + inner_faces[i]->fp3->r->y)/2; - pb->r->z=(inner_faces[i]->fp2->r->z + inner_faces[i]->fp3->r->z)/2; - - pc->r->x=(inner_faces[i]->fp1->r->x + inner_faces[i]->fp3->r->x)/2; - pc->r->y=(inner_faces[i]->fp1->r->y + inner_faces[i]->fp3->r->y)/2; - pc->r->z=(inner_faces[i]->fp1->r->z + inner_faces[i]->fp3->r->z)/2; - // - - /* cout<<"inner iteration before normalization: "<x="<r->x<<"; pa->y="<r->y<<"; pa->z="<r->z<x="<r->x<<"; pb->y="<r->y<<"; pb->z="<r->z<x="<r->x<<"; pc->y="<r->y<<"; pc->z="<r->z<r->Normalize(); - pb->r->Normalize(); - pc->r->Normalize(); - -/* cout<<"inner iteration after normalization: "<x="<r->x<<"; pa->y="<r->y<<"; pa->z="<r->z<x="<r->x<<"; pb->y="<r->y<<"; pb->z="<r->z<x="<r->x<<"; pc->y="<r->y<<"; pc->z="<r->z<r->x == pa->r->x - && inner_points[o]->r->y == pa->r->y - && inner_points[o]->r->z == pa->r->z - ) - { - delete pa; - pa = inner_points[o]; - a = true; - } - - if - ( - inner_points[o]->r->x == pb->r->x - && inner_points[o]->r->y == pb->r->y - && inner_points[o]->r->z == pb->r->z - ) - { - delete pb; - pb = inner_points[o]; - b = true; - } - - if - ( - inner_points[o]->r->x == pc->r->x - && inner_points[o]->r->y == pc->r->y - && inner_points[o]->r->z == pc->r->z - ) - { - delete pc; - pc = inner_points[o]; - c = true; - } - } - - // The new points are added to the general collection of points - if(!a) inner_points.push_back(pa); - if(!b) inner_points.push_back(pb); - if(!c) inner_points.push_back(pc); - - - // Create three new faces; these represent the three inner triangles - inner_faces.push_back(new Face(inner_faces[i]->fp1, pa, pc, inner_springs)); // Top - inner_faces.push_back(new Face(pa, inner_faces[i]->fp2, pb, inner_springs)); // Bottom-left -// inner_faces.push_back(new Face(pb, inner_faces[i]->fp3, pc, inner_springs)); // Bottom-right - inner_faces.push_back(new Face(pc, pb, inner_faces[i]->fp3, inner_springs)); // Bottom-right - - - // Replace the original face's points with its new one in the middle - - inner_faces[i]->fp1 = pa; - inner_faces[i]->fp2 = pb; - inner_faces[i]->fp3 = pc; - - - inner_faces[i]->fs1->sp1 = pa; - inner_faces[i]->fs1->sp2 = pb; - - inner_faces[i]->fs2->sp1 = pb; - inner_faces[i]->fs2->sp2 = pc; - - inner_faces[i]->fs3->sp1 = pc; - inner_faces[i]->fs3->sp2 = pa; - } - - - - - - } - - - int i; -for(i=0; ir->y<<";"<r->z<fp1->r->y<<";"<fp1->r->z<fp2->r->y<<";"<fp2->r->z<fp3->r->y<<";"<fp3->r->z<-90 && theta<90) - { - tix = 1*RING_RADIUS*cos(theta)*cos(phi+dphi); // outer point X coordiation - tiy = 1*RING_RADIUS*cos(theta)*sin(phi+dphi); // outer point Y coordiation - tiz = 1*RING_RADIUS*sin(theta); // outer point Y coordiation - - tox = 1*RING_RADIUS*cos(theta)*cos(phi+dphi); - toy = 1*RING_RADIUS*cos(theta)*sin(phi+dphi); // outer point Y coordiation - toz = 1*RING_RADIUS*sin(theta); // outer point Y coordiation - - outer_points.push_back( new Particle(new Vector(tox, toy, toz), MASS)); - - inner_points.push_back( new Particle(new Vector(tix, tiy, tiz), MASS)); - } - - // cout<<"tox=="<numParticles = this->outer_points.size(); - this->numSprings = this->outer_springs.size(); - -// cout<<"this->numParticles ==="<numParticles<<"-----this->numSprings===="<numSprings<<"-------this->numFaces===="<outer_faces.size()<CalNormalNField(); - outer_faces[i]->CalNormalNField(); -// cout<<"outer_faces[i]->normal->x==="<normal->x<<"=== outer_faces[i]->normal->y==="<normal->y<<"===outer_faces[i]->normal->z===="<normal->z<numParticles = outer_points.size(); - - - a = 1 / sqrt(2.0); - - for (i=0;ir->x *= a; - outer_points[i]->r->y *= a; - // outer_points[i]->r->z *= a; - - inner_points[i]->r->x *= a; - inner_points[i]->r->y *= a; - // inner_points[i]->r->z *= a; - } - - - outer_faces.push_back(new Face(outer_points[0], outer_points[3], outer_points[4], outer_springs)); - outer_faces.push_back(new Face(outer_points[0], outer_points[4], outer_points[5], outer_springs)); - outer_faces.push_back(new Face(outer_points[0], outer_points[5], outer_points[2], outer_springs)); - outer_faces.push_back(new Face(outer_points[0], outer_points[2], outer_points[3], outer_springs)); - outer_faces.push_back(new Face(outer_points[1], outer_points[4], outer_points[3], outer_springs)); - outer_faces.push_back(new Face(outer_points[1], outer_points[5], outer_points[4], outer_springs)); - outer_faces.push_back(new Face(outer_points[1], outer_points[2], outer_points[5], outer_springs)); - outer_faces.push_back(new Face(outer_points[1], outer_points[3], outer_points[2], outer_springs)); - - - inner_faces.push_back(new Face(inner_points[0], inner_points[3], inner_points[4], inner_springs)); - inner_faces.push_back(new Face(inner_points[0], inner_points[4], inner_points[5], inner_springs)); - inner_faces.push_back(new Face(inner_points[0], inner_points[5], inner_points[2], inner_springs)); - inner_faces.push_back(new Face(inner_points[0], inner_points[2], inner_points[3], inner_springs)); - inner_faces.push_back(new Face(inner_points[1], inner_points[4], inner_points[3], inner_springs)); - inner_faces.push_back(new Face(inner_points[1], inner_points[5], inner_points[4], inner_springs)); - inner_faces.push_back(new Face(inner_points[1], inner_points[2], inner_points[5], inner_springs)); - inner_faces.push_back(new Face(inner_points[1], inner_points[3], inner_points[2], inner_springs)); - - - this->numSprings = this->outer_springs.size(); - -} - - -//##ModelId=45F4D797011E -void Object3D::Pyramid3() -{ - - double a; - - int i; - - - outer_points.push_back( new Particle(new Vector(-2 * RING_RADIUS, 0, 2 * RING_RADIUS), MASS)); - outer_points.push_back( new Particle(new Vector(-2 * RING_RADIUS, 0, -2 * RING_RADIUS), MASS)); - outer_points.push_back( new Particle(new Vector( 2 * RING_RADIUS, 0, 0), MASS)); - outer_points.push_back( new Particle(new Vector( 0, 2 * RING_RADIUS,0), MASS)); - - // XXX: must match above - inner_points.push_back( new Particle(new Vector(0,0,RING_RADIUS), MASS)); - inner_points.push_back( new Particle(new Vector(0,0,-RING_RADIUS), MASS)); - inner_points.push_back( new Particle(new Vector(-RING_RADIUS,-RING_RADIUS,0), MASS)); - inner_points.push_back( new Particle(new Vector(RING_RADIUS,-RING_RADIUS,0), MASS)); - - - this->numParticles = outer_points.size(); - - - a = 1 / sqrt(2.0); - - - - for (i=0;ir->x *= a; - outer_points[i]->r->y *= a; - outer_points[i]->r->z *= a; - - - inner_points[i]->r->x *= a; - inner_points[i]->r->y *= a; - inner_points[i]->r->z *= a; - } - - - // Bottom face - outer_faces.push_back(new Face(outer_points[0], outer_points[1], outer_points[2], outer_springs)); - - // Left face - outer_faces.push_back(new Face(outer_points[0], outer_points[1], outer_points[3], outer_springs)); - - - // Front face - outer_faces.push_back(new Face(outer_points[0], outer_points[2], outer_points[3], outer_springs)); - - // Back face - outer_faces.push_back(new Face(outer_points[1], outer_points[2], outer_points[3], outer_springs)); - - - //----------------------------- - -///* - - inner_faces.push_back(new Face(inner_points[0], inner_points[1], inner_points[2], inner_springs)); - - // Left face - inner_faces.push_back(new Face(inner_points[0], inner_points[1], inner_points[3], inner_springs)); - - // Front face - inner_faces.push_back(new Face(inner_points[0], inner_points[2], inner_points[3], inner_springs)); - - // Back face - inner_faces.push_back(new Face(inner_points[1], inner_points[2], inner_points[3], inner_springs)); - -//*/ - - - - this->numSprings = this->outer_springs.size(); -} - - -//================================================================================ - -//##ModelId=45F4D79700FD -void Object3D::Draw() -{ - - - static bool findOnce = false; - - int i; - //=============================================================================== - glPushMatrix(); - glColor4f(0,0,1,1); // Blue color point distribution - glBegin(GL_POINTS); // Draw points which built the Object3D - glPointSize(10); - for(i=0; ir->x, outer_points[i]->r->y, outer_points[i]->r->z); - } - - glEnd(); - - glColor4f(0,0,1,1); - - glBegin(GL_POINTS); // Draw inner points which built the Object3D - glPointSize(10); - for(i=0; ir->x, inner_points[i]->r->y, inner_points[i]->r->z); - } - glEnd(); - - glPopMatrix(); -//=================================================================================== - - -// cout<<"Object3D spring size is : "<sp1->r->x, outer_springs[i]->sp1->r->y, outer_springs[i]->sp1->r->z); - glVertex3f(outer_springs[i]->sp2->r->x, outer_springs[i]->sp2->r->y, outer_springs[i]->sp2->r->z); - - - } - glEnd(); - glPopMatrix(); - //*/ -///========================================draw inner structural springs========================================== - -/* glPushMatrix(); - glColor4f(1,0,0,1); - glBegin(GL_LINES); - for(i = 0; isp1->r->x, inner_springs[i]->sp1->r->y, inner_springs[i]->sp1->r->z); - glVertex3f(inner_springs[i]->sp2->r->x, inner_springs[i]->sp2->r->y, inner_springs[i]->sp2->r->z); - } - glEnd(); - glPopMatrix(); -*/ -///========================================draw radium springs========================================== -/* glPushMatrix(); - glBegin(GL_LINES); // the draw of radium lines from inner to outer - glLineWidth(10); - for(i=0 ; isp1->r->x, radium_springs[i]->sp1->r->y, radium_springs[i]->sp1->r->z); - glVertex3f(radium_springs[i]->sp2->r->x, radium_springs[i]->sp2->r->y, radium_springs[i]->sp2->r->z); - } - glEnd(); - glPopMatrix();*/ - -///========================================draw shear left springs========================================== -/* glPushMatrix(); - glBegin(GL_LINES); // the left shear lines from inner to outer - for(i=0 ; isp1->r->x,shear_springs_left[i]->sp1->r->y, shear_springs_left[i]->sp1->r->z); - glVertex3f(shear_springs_left[i]->sp2->r->x,shear_springs_left[i]->sp2->r->y, shear_springs_left[i]->sp2->r->z); - } - glEnd(); - glPopMatrix();*/ - -///========================================draw shear right springs========================================== - -/* glPushMatrix(); - glBegin(GL_LINES); // the right shear lines from inner to outer - for(i=0 ; isp1->r->x,shear_springs_right[i]->sp1->r->y, shear_springs_right[i]->sp1->r->z); - glVertex3f(shear_springs_right[i]->sp2->r->x,shear_springs_right[i]->sp2->r->y, shear_springs_right[i]->sp2->r->z); - } - glEnd(); - glPopMatrix();*/ - - - -//======================================================================================= -/////----------------------------------------- Draw inner faces ---------------------------------------------- -// cout<<"inner_faces.size()???????????????"<normal->x,inner_faces[i]->normal->y,inner_faces[i]->normal->z); - glVertex3f(inner_faces[i]->fp1->r->x, inner_faces[i]->fp1->r->y, inner_faces[i]->fp1->r->z); - glVertex3f(inner_faces[i]->fp2->r->x, inner_faces[i]->fp2->r->y, inner_faces[i]->fp2->r->z); - glVertex3f(inner_faces[i]->fp3->r->x, inner_faces[i]->fp3->r->y, inner_faces[i]->fp3->r->z); - - } - glEnd(); -/////----------------------------------------- Draw inner faces normals---------------------------------------------- - -/* glBegin(GL_LINES); - for(i=0 ; ifp1->r->x, inner_faces[i]->fp1->r->y, inner_faces[i]->fp1->r->z); - - glVertex3f((inner_faces[i]->normal->x + inner_faces[i]->fp1->r->x), - (inner_faces[i]->normal->y + inner_faces[i]->fp1->r->y), - (inner_faces[i]->normal->z + inner_faces[i]->fp1->r->z)); - } - glEnd();*/ - - glPopMatrix(); - - -/////----------------------------------------- Draw outer faces ---------------------------------------------- - - glPushMatrix(); - glBegin(GL_TRIANGLES); - for(i=0; inormal->x,outer_faces[i]->normal->y,outer_faces[i]->normal->z); - glVertex3f(outer_faces[i]->fp1->r->x, outer_faces[i]->fp1->r->y, outer_faces[i]->fp1->r->z); - glVertex3f(outer_faces[i]->fp2->r->x, outer_faces[i]->fp2->r->y, outer_faces[i]->fp2->r->z); - glVertex3f(outer_faces[i]->fp3->r->x, outer_faces[i]->fp3->r->y, outer_faces[i]->fp3->r->z); - - } - glEnd(); -/////----------------------------------------- Draw outer faces normals---------------------------------------------- - - - glColor3f(0,0,1); - glBegin(GL_LINES); - for(i=0 ; ifp1->r->x, outer_faces[i]->fp1->r->y, outer_faces[i]->fp1->r->z); - glVertex3f(outer_faces[i]->fp2->r->x, outer_faces[i]->fp2->r->y, outer_faces[i]->fp2->r->z); - - glVertex3f(outer_faces[i]->fp2->r->x, outer_faces[i]->fp2->r->y, outer_faces[i]->fp2->r->z); - glVertex3f(outer_faces[i]->fp3->r->x, outer_faces[i]->fp3->r->y, outer_faces[i]->fp3->r->z); - - glVertex3f(outer_faces[i]->fp3->r->x, outer_faces[i]->fp3->r->y, outer_faces[i]->fp3->r->z); - glVertex3f(outer_faces[i]->fp1->r->x, outer_faces[i]->fp1->r->y, outer_faces[i]->fp1->r->z); - -// glVertex3f((outer_faces[i]->normal->x + outer_faces[i]->fp1->r->x), -// (outer_faces[i]->normal->y + outer_faces[i]->fp1->r->y), -// (outer_faces[i]->normal->z + outer_faces[i]->fp1->r->z)); - - - glVertex3f(inner_faces[i]->fp1->r->x, inner_faces[i]->fp1->r->y, inner_faces[i]->fp1->r->z); - glVertex3f(inner_faces[i]->fp2->r->x, inner_faces[i]->fp2->r->y, inner_faces[i]->fp2->r->z); - - glVertex3f(inner_faces[i]->fp2->r->x, inner_faces[i]->fp2->r->y, inner_faces[i]->fp2->r->z); - glVertex3f(inner_faces[i]->fp3->r->x, inner_faces[i]->fp3->r->y, inner_faces[i]->fp3->r->z); - - glVertex3f(inner_faces[i]->fp3->r->x, inner_faces[i]->fp3->r->y, inner_faces[i]->fp3->r->z); - glVertex3f(inner_faces[i]->fp1->r->x, inner_faces[i]->fp1->r->y, inner_faces[i]->fp1->r->z); - } - glEnd(); - - glPopMatrix(); -//--------------------------------------------------------------------------------------- - - if(integrator->dragExists) - { - if(findOnce == true) - { - FindClosestPoint(); - findOnce = false; - } - - glColor3f(1,1,0); // A white line between the Object3D point and the mouse point - glBegin(GL_LINES); - glVertex2f(integrator->mDragX, integrator->mDragY); - glVertex3f(outer_points[closest_i]->r->x,outer_points[closest_i]->r->y,outer_points[closest_i]->r->z); - glEnd(); - } - else - { - FindClosestPoint(); - findOnce = true; - } - -} - diff --git a/src/SoftBodySimulation/common/Particle.cpp b/src/SoftBodySimulation/common/Particle.cpp deleted file mode 100644 index 2d82516..0000000 --- a/src/SoftBodySimulation/common/Particle.cpp +++ /dev/null @@ -1,52 +0,0 @@ -#include "Particle.h" - - -// class Particle number functions - -//##ModelId=45F4D7970080 -void Particle::ClearForce(void) // reset forces at a Particle -{ - - if(f == NULL) - { - f = new Vector(0,0,0); // A force vector with all coordinates equal zero - } - else - { - f->x = 0; - f->y = 0; - f->z = 0; - } -} - - -ostream& operator<<(ostream& os, const Particle& c) // output operator -{ - - //os<x; - os<y; - os<z; - - os<x; // Particle space position - os<y; - os<z; - - os<x; // Particle space velocity - os<y; - os<z; - - os<x; // small change of position in derivatives - os<y; - os<z; - - os<x; // small change of velocity in derivatives - os<y; - os<z; - - return os; // Always return stream object. -} diff --git a/src/SoftBodySimulation/common/Spring.cpp b/src/SoftBodySimulation/common/Spring.cpp deleted file mode 100644 index 58975ed..0000000 --- a/src/SoftBodySimulation/common/Spring.cpp +++ /dev/null @@ -1,52 +0,0 @@ -#include "Spring.h" - - - -//##ModelId=45F4D79603CB -Spring::Spring(Particle *P1, Particle *P2,float Ks, float Kd): // spring constructor with spring head, tail, and Ks, Kd - sp1(P1), sp2(P2), ks(Ks), kd(Kd) -{ // *P1, *P2 Particles the srping two ends - // Spring_ks is the Hooke constant - // Spring_kd is the dampling constant - - if (restLen != 0) //????????????????????????????????????????? - { - restLen = (*sp1->r - *sp2->r).getLength(); // static length - } - changePercent = 10.0; - maxAllowedDist = (changePercent/1.0f)*restLen; - type = SPRING_STRUCTURAL; // default spring type is structural spring which - // construct the object shape -} - - -//##ModelId=45F4D79603DE -Spring::Spring() // spring constructor with no initial value -{ - sp1 = new Particle(); // head Particle of a Spring - sp2 = new Particle(); // tail Particle of a Spring - restLen = 0; // lenth when Fs = 0 (Hooke force) - changePercent = 0; // percentage of spring length change - maxAllowedDist = 0; // maximum distange allowed from the rest length - ks = 0; // Spring constant - kd = 0; // damping factor - type = SPRING_STRUCTURAL; // default spring type is structural spring which -} // construct the object shape - -//##ModelId=45F4D79603DF -Spring::~Spring() //spring destructor -{ -/* if(sp1 != NULL) - delete sp1; - - if(sp2 != NULL) - delete sp2; -*/ -} - -//##ModelId=45F4D79603E0 -//set the springs resting length -void Spring::setRestLen() -{ - restLen = (*sp1->r - *sp2->r).getLength(); // static length -} diff --git a/src/SoftBodySimulation/common/Vector.cpp b/src/SoftBodySimulation/common/Vector.cpp deleted file mode 100644 index 201e321..0000000 --- a/src/SoftBodySimulation/common/Vector.cpp +++ /dev/null @@ -1,147 +0,0 @@ -#include -#include -#include "Vector.h" - - -// Class Vector member functions - -Vector operator+ (const Vector &K, const Vector &N) // #1 fcn - Vector addition for two vectors -{ - cout<<"vecotr +"< &springs) : - fp1(Ap1), fp2(Ap2), fp3(Ap3) - { - normal = new Vector(0,0,0); // normal vecotr for face -// force = new Vector(0,0,0); - - fs1 = new Spring(Ap1, Ap2); // first spring points to particle p1&p2 - fs2 = new Spring(Ap2, Ap3); // second spring points to particle p2&p3 - fs3 = new Spring(Ap3, Ap1); // third spring points to particle p3&p1 - - - bool a = false, b = false, c = false; // the three new particles have not existed - - for(int o = 0; o < springs.size(); o++) // for all the spring in the requested spring vecotr - { - if // if the spring exist with particle p1&p2 - ( - springs[o]->sp1 == Ap1 - && springs[o]->sp2 == Ap2 - ) - { - delete fs1; // delete the builded spring fs1 - fs1 = springs[o]; // the new spring will point to the existed spring - a = true; // set the spring's particles have existed - } - - if // if the spring exist with particle p2&p3 - ( - springs[o]->sp1 == Ap2 - && springs[o]->sp2 == Ap3 - ) - { - delete fs2; // delete the builded spring fs2 - fs2 = springs[o]; // the new spring will point to the existed spring - b = true; // set the spring's particles have existed - } - - if // if the spring exist with particle p3&p1 - ( - springs[o]->sp1 == Ap3 - && springs[o]->sp2 == Ap1 - ) - { - delete fs3; // delete the builded spring fs3 - fs3 = springs[o]; // the new spring will point to the existed spring - c = true; // set the spring's particles have existed - } - } - - // The new points are added to the general collection of points - if(!a) springs.push_back(fs1); - if(!b) springs.push_back(fs2); - if(!c) springs.push_back(fs3); - } - -//////////////////////////////////////////////////////////////////////////////////////////////////// - //##ModelId=45F4D7980065 - ~Face() // face destructor - { -// delete fp1; -// delete fp2; -// delete fp3; - delete normal; -// delete force; - } -/////////////////////////////////////////////////////////////////////////////////////////////////// - //##ModelId=45F4D7980066 - void CalNormalNField(void); // Calculate normal vector to a triangle and its field -public: - - //##ModelId=45F4D7980070 - Particle *fp1; // first vertex - //##ModelId=45F4D7980075 - Particle *fp2; // second vertex - //##ModelId=45F4D798007F - Particle *fp3; // third vertex*/ - - //##ModelId=45F4D7980084 - Spring *fs1; // first spring on the face - //##ModelId=45F4D7980089 - Spring *fs2; // second spring on the face - //##ModelId=45F4D798008F - Spring *fs3; // third spring on the face -// float field; // normal field - //##ModelId=45F4D7980094 - Vector* normal; // triangle normal Vector -// Vector* force; // force on the triangle -}; - -#endif diff --git a/src/SoftBodySimulation/include/Integrator.h b/src/SoftBodySimulation/include/Integrator.h deleted file mode 100644 index a58863f..0000000 --- a/src/SoftBodySimulation/include/Integrator.h +++ /dev/null @@ -1,83 +0,0 @@ -#ifndef INTEGRATOR_H -#define INTEGRATOR_H - -#include "Object.h" - -class Object; - - -class Integrator -{ -protected: - - Object* object; - - - vector temp_inner_points0; // temp inner vector container for integrator computation - - vector temp_inner_points1; - - vector temp_inner_points2; - - vector temp_inner_points3; - - vector temp_inner_points4; - - - vector temp_outer_points0; // temp outer vector container for integrator computation - - vector temp_outer_points1; - - vector temp_outer_points2; - - vector temp_outer_points3; - - vector temp_outer_points4; - - - dimensionality dim; - -public: - - bool dragExists; //if there is user interaction - - float mDragX; // mouse position x axis - - float mDragY; // mouse position y axis - -public: - - Integrator(Object&); // integrator constructor with the computed object - - virtual ~Integrator(); // integrator destructor - - // the calculation of the integration with/without external dragging force - - virtual void integrate(float, bool drag = false, float xDrag = 0, float yDrag = 0); - - - dimensionality getDimension() {return dim;} // get the object type, 1D, 2D, or 3D - - void setDimension(dimensionality dim) {this->dim = dim;} // set the object type, 1D, 2D, or 3D - -protected: - - virtual void ExternalForces(); // accumulate gravity and dragging force - - virtual void SpringForces(); // accumulate all type of springs forces - - virtual void PressureForces(); // accumulate pressure - - virtual void AccumulateForces(); // accumulate all forces acted on object - - virtual void Derivatives(float, float) = 0; // Differential computation - - virtual void CollisionDetection(int); // calculate the collision detection - -private: - // general spring calculation function - - void CalculateSpringForces(vectorsprings, int i, bool special = false); -}; - -#endif /* INTEGRATOR_H */ diff --git a/src/SoftBodySimulation/include/MidpointIntegrator.h b/src/SoftBodySimulation/include/MidpointIntegrator.h deleted file mode 100644 index 234bfff..0000000 --- a/src/SoftBodySimulation/include/MidpointIntegrator.h +++ /dev/null @@ -1,23 +0,0 @@ -#ifndef MIDPOINTINTEGRATOR_H -#define MIDPOINTINTEGRATOR_H - -#include "EulerIntegrator.h" - - -class MidpointIntegrator : public EulerIntegrator -{ -public: - - MidpointIntegrator(Object&); // constructor - - ~MidpointIntegrator(); // destructor - -protected: - - - void k2(int, float, float);// 2nd integrate to get the derivatives of velocity and position - - void ynew(int, float, float);// get the new velocity and position -}; - -#endif /* MIDPOINTINTEGRATOR_H */ diff --git a/src/SoftBodySimulation/include/Object.h b/src/SoftBodySimulation/include/Object.h deleted file mode 100644 index e36e11d..0000000 --- a/src/SoftBodySimulation/include/Object.h +++ /dev/null @@ -1,77 +0,0 @@ -#ifndef OBJECT_H -#define OBJECT_H - -#include "Face.h" -#include "Spring.h" -#include "Integrator.h" -#include -using namespace std; - -class Integrator; - -//##ModelId=45F4D79701E6 -class Object -{ -protected: - //##ModelId=45F4D79701E7 - int numParticles; // number of particles of the object - //##ModelId=45F4D79701F5 - int numSprings; // number of springs of the object - //##ModelId=45F4D79701F7 - Integrator* integrator; // integrator object - //##ModelId=45F4D7970206 - integrator_type integratorType; // different type of integrator, Euler, Midpoint, RK4 - -public: - //##ModelId=45F4D797020B - vector inner_springs; // the springs on the inner layer of the object - //##ModelId=45F4D7970216 - vector outer_springs; // the springs on the outer layer of the object - //##ModelId=45F4D7970227 - vector inner_points; // the particles on the inner layer of the object - //##ModelId=45F4D7970235 - vector outer_points; // the particles on the outer layer of the object - //##ModelId=45F4D7970246 - vector inner_faces; // the surfaces on the inner layer of the object - //##ModelId=45F4D797024B - vector outer_faces; // the surfaces on the outer layer of the object - //##ModelId=45F4D7970254 - dimensionality dim; // dimension type to specify the object is 1D, 2D, or 3D - - //##ModelId=45F4D7970258 - int closest_i ; // the closest point on the outer layer to mouse position - -public: - //##ModelId=45F4D7970263 - Object(); // object constructor - //##ModelId=45F4D7970264 - virtual ~Object(); // object destructor - - //##ModelId=45F4D7970266 - int GetNumberOfParticles() { return numParticles; } // get the number of particles of the object - //##ModelId=45F4D7970267 - int GetNumberOfSprings() { return numSprings; } // get the number of springs of the object - - //##ModelId=45F4D7970268 - void setIntegratorType(integrator_type type); // choose the integrator - //##ModelId=45F4D7970273 - Integrator* getIntegrator() {return integrator;} // get the integrator type - - //##ModelId=45F4D7970274 - virtual void Update(float, bool = false, float = 0, float = 0); //update the object about forces, velocity, position - - //##ModelId=45F4D7970284 - virtual void Draw() = 0; // display the object - //##ModelId=45F4D7970286 - virtual void FindClosestPoint(void) ; // find the closet particle on th outer layer to mouse position - -protected: - //##ModelId=45F4D7970293 - virtual void SetParticles(); // model the particles on the object - //##ModelId=45F4D7970295 - virtual void SetObject(); // model the object - //##ModelId=45F4D7970297 - virtual void Add_Structural_Spring(int, int, int); // construct the springs to model the object -}; - -#endif /* OBJECT_H */ diff --git a/src/SoftBodySimulation/include/Object1D.h b/src/SoftBodySimulation/include/Object1D.h deleted file mode 100644 index eb21d02..0000000 --- a/src/SoftBodySimulation/include/Object1D.h +++ /dev/null @@ -1,27 +0,0 @@ -#ifndef OBJECT1D_H -#define OBJECT1D_H - -#include "Object.h" - -//##ModelId=45F4D79701B7 -class Object1D : public Object -{ -public: - //##ModelId=45F4D79701B9 - Object1D(); // object1D constructor - //##ModelId=45F4D79701BA - virtual ~Object1D(); // object1D destructor - //##ModelId=45F4D79701C7 - virtual void Draw(); // display object1D -// virtual void FindClosestPoint(void); - -private: - //model the object1D with structural spring - //##ModelId=45F4D79701C9 - void Add_Structural_Spring(int index, int head, int tail); -protected: - //##ModelId=45F4D79701CD - virtual void SetObject(); // construct the object1D -}; - -#endif /* OBJECT1D_H */ diff --git a/src/SoftBodySimulation/include/Object2D.h b/src/SoftBodySimulation/include/Object2D.h deleted file mode 100644 index a124eca..0000000 --- a/src/SoftBodySimulation/include/Object2D.h +++ /dev/null @@ -1,49 +0,0 @@ -#ifndef OBJECT2D_H -#define OBJECT2D_H - -#include "Object1D.h" -#include "global.h" - -//##ModelId=45F4D797013A -class Object2D : public Object1D -{ -protected: - //##ModelId=45F4D797014A - float pressure; // pressure at each Particles on Object2D along its normal -public: - //##ModelId=45F4D797015B - vector radium_springs; // for radium springs between two circles - //##ModelId=45F4D7970160 - vector shear_springs_left; // for shear spring toward left - //##ModelId=45F4D797016A - vector shear_springs_right; // for right spring toward right - -public: - - //##ModelId=45F4D797016E - Object2D(); // constructor object2D - //##ModelId=45F4D7970178 - virtual ~Object2D(); // destructor object2D - - //##ModelId=45F4D797017A - virtual void Draw(); // display the object 2D - //##ModelId=45F4D797017C - inline void setPressure(float pre){pressure = pre;} // Set Pressure - //##ModelId=45F4D797017E - inline float getPressure() {return(pressure);} // Get Pressure - -private: - //##ModelId=45F4D797017F - virtual void SetObject(void); // model the object2D - //##ModelId=45F4D797018A - void Add_Structural_Spring(int index, int h, int t); // model the inner or outer cirle with structural springs - //##ModelId=45F4D7970198 - void Add_Radium_Spring(int index); // add the radium springs with inner point i and outer point i - //##ModelId=45F4D797019A - void Add_Shear_Spring(int index, int h, int t ); // add the left shear springs with inner point i and outer point i+1 //add the right shear springs with inner point i+1 and outer point i - //##ModelId=45F4D79701A7 - virtual void Add_Faces(int i, int i1, int imid); // add the triangle surface to object2D - -}; - -#endif /* OBJECT2D_H */ diff --git a/src/SoftBodySimulation/include/Object3D.h b/src/SoftBodySimulation/include/Object3D.h deleted file mode 100644 index eeae7f4..0000000 --- a/src/SoftBodySimulation/include/Object3D.h +++ /dev/null @@ -1,73 +0,0 @@ -#ifndef OBJECT3D_H -#define OBJECT3D_H - - -#include "Particle.h" -#include "Spring.h" - -#include "Object2D.h" - - -//##ModelId=45F4D79700EC -class Object3D : public Object2D -{ -public: - - //##ModelId=45F4D79700EE - Object3D(); // object3D constructor -// Object3D(float Mass, float Ks, float Kd, float Press); -// Object3D (float Mass, float Ks, float Kd); - //##ModelId=45F4D79700FB - virtual ~Object3D(); // object3D destructor - - /* - void Mouse(int, int, int, int); - void Keyboard(unsigned char, int, int); - void SpecialKeys(int, int, int); - void Rotated(); - */ - -/* - void ExternalForces(); // gravity force - void PressureForces(); // pressure - void SpringForces(); // spring forces - void AccumulateForces(); // accumulate forces acted on - void Derivatives(float); // Differential computation -*/ - -public: - - //##ModelId=45F4D79700FD - virtual void Draw(); - //##ModelId=45F4D79700FF - void Iteration(); - //##ModelId=45F4D7970100 - void nonunitsphere(); - //void GetClosestParticle(); // the Particle on the Object3D, which is cloest to the mouse pos - - //void GetParticles (); - //void GetFace(); - -private: - //##ModelId=45F4D7970101 - virtual void SetObject(void); // model the object3D - //##ModelId=45F4D797010B - void Add_Structural_Spring(int index, int h, int t); // model the inner or outer cirle with structural springs - //##ModelId=45F4D797010F - void Add_Radium_Spring(int index); // add the radium springs with inner point i and outer point i - //##ModelId=45F4D797011B - void Add_Shear_Spring(int index); // add the left&right shear springs with inner point i and outer point i+1 - - //##ModelId=45F4D797011D - void Tetrahedron(); // draw the single shape - //##ModelId=45F4D797011E - void Pyramid3(); - //##ModelId=45F4D797011F - void Pyramid4(); - - Particle *pa, *pb, *pc; // the three points subdivide each edge on the triangle face - //##ModelId=45F4D797012A - int iterations; // the number of iteration for subdivision -}; - -#endif diff --git a/src/SoftBodySimulation/include/Particle.h b/src/SoftBodySimulation/include/Particle.h deleted file mode 100644 index 3efa1dd..0000000 --- a/src/SoftBodySimulation/include/Particle.h +++ /dev/null @@ -1,103 +0,0 @@ -#ifndef PARTICLE_H -#define PARTICLE_H - -#include "Vector.h" - -// Class Particle declaration -//##ModelId=45F4D797006F -class Particle -{ - public: - - //##ModelId=45F4D7970070 - Particle(Vector* R, float Mass) : mass(Mass) // particle constructor with variables position and mass - { - r = R; // initialize Particle space position - // OneOverMass=1.0/mass; - dr = new Vector(0,0,0); // initialize position in derivatives - dv = new Vector(0,0,0); // initialize velocity in derivatives - f = new Vector(0,0,0); // initialize Particle space force - v = new Vector(0,0,0); // initialize Particle space velocity - } - - //##ModelId=45F4D7970073 - Particle() // particle constructor with no variables - { - r = new Vector(0,0,0); // initialize Particle space position - dr = new Vector(0,0,0); // initialize position in derivatives - dv = new Vector(0,0,0); // initialize velocity in derivatives - f = new Vector(0,0,0); // initialize Particle space force - v = new Vector(0,0,0); // initialize Particle space velocity - mass = MASS; - } - - //##ModelId=45F4D7970074 - Particle(const Particle& p) // particle constructor with variables position - { - mass = p.mass; - - r = new Vector(0,0,0); // initialize Particle space position - r->x = p.r->x; - r->y = p.r->y; - r->z = p.r->z; - - dr = new Vector(0,0,0); // initialize position in derivatives - dr->x = p.dr->x; - dr->y = p.dr->y; - dr->z = p.dr->z; - - dv = new Vector(0,0,0); // initialize velocity in derivatives - dv->x = p.dv->x; - dv->y = p.dv->y; - dv->z = p.dv->z; - - f = new Vector(0,0,0); // initialize Particle space force - f->x = p.f->x; - f->y = p.f->y; - f->z = p.f->z; - - v = new Vector(0,0,0); // initialize Particle space velocity - v->x = p.v->x; - v->y = p.v->y; - v->z = p.v->z; - } - - - //##ModelId=45F4D797007F - ~Particle() // particle destructor - { - /* - delete norm; - delete dr; - delete dv; - delete f; - delete v; - */ - } - - //##ModelId=45F4D7970080 - void ClearForce(void); // reset forces at a Particle -// void AccumuForce(Vector const &); - //##ModelId=45F4D79700B2 - friend ostream& operator<<(ostream& os, const Particle& c); //output operator - - -public: - - //##ModelId=45F4D7970082 - float mass; // space Particle with mass - //##ModelId=45F4D797008E - float OneOverMass; // for computation - //##ModelId=45F4D7970090 - Vector* r; // Particle space position - //##ModelId=45F4D7970095 - Vector* f; // Particle space force - //##ModelId=45F4D797009F - Vector* v; // Particle space velocity - //##ModelId=45F4D79700A4 - Vector* dr; // small change of position in derivatives - //##ModelId=45F4D79700AE - Vector* dv; // small change of velocity in derivatives -}; - -#endif diff --git a/src/SoftBodySimulation/include/RungeKutta4Integrator.h b/src/SoftBodySimulation/include/RungeKutta4Integrator.h deleted file mode 100644 index cd0a689..0000000 --- a/src/SoftBodySimulation/include/RungeKutta4Integrator.h +++ /dev/null @@ -1,25 +0,0 @@ -#ifndef RUNGEKUTTA4INTEGRATOR_H -#define RUNGEKUTTA4INTEGRATOR_H - -#include "MidpointIntegrator.h" - - -class RungeKutta4Integrator : public MidpointIntegrator -{ -public: - - RungeKutta4Integrator(Object&);// constructor - - ~RungeKutta4Integrator();// destructor - - -protected: - - void k3(int, float, float);// 3rd integrate to get the derivatives of velocity and position - - void k4(int, float, float);// 4th integrate to get the derivatives of velocity and position - - void ynew(int, float, float);// get the new velocity and position -}; - -#endif /* RUNGEKUTTA4INTEGRATOR_H */ diff --git a/src/SoftBodySimulation/include/Simulation.h b/src/SoftBodySimulation/include/Simulation.h deleted file mode 100644 index 404e36b..0000000 --- a/src/SoftBodySimulation/include/Simulation.h +++ /dev/null @@ -1,22 +0,0 @@ -/******************************************************************************** - * * - * A demo 3D ball of multi-layers with deformation under non-uniform factors. * - * The deformation reaction under those non-distributed influences is well * - * observed * - * * - * Version V1.01 * - * Copyright by Miao Song * - * * - ********************************************************************************/ - - -#include "Vector.h" -#include "Particle.h" -#include "Spring.h" -#include "Face.h" -#include "ViewSpace.h" -#include "global.h" -#include "Object3D.h" - - -void Rotated(void); \ No newline at end of file diff --git a/src/SoftBodySimulation/include/Spring.h b/src/SoftBodySimulation/include/Spring.h deleted file mode 100644 index ed460d8..0000000 --- a/src/SoftBodySimulation/include/Spring.h +++ /dev/null @@ -1,47 +0,0 @@ -#ifndef SPRING_H -#define SPRING_H - - -#include "Particle.h" -// Class Spring is an element cell of 2D ball entire surface -// Linear hooke Spring is built between two space Particles - -//##ModelId=45F4D79603CA -class Spring -{ -public: - - //##ModelId=45F4D79603CB - Spring(Particle *P1, Particle *P2,float Ks=KS, float Kd=KD); // constructor with variables - //##ModelId=45F4D79603DE - Spring(); // constructor with no variable - //##ModelId=45F4D79603DF - ~Spring(); // destructor - - //##ModelId=45F4D79603E0 - void setRestLen(); - -public: -//protected: - //##ModelId=45F4D79603E2 - Particle *sp1; // head Particle of a Spring - //##ModelId=45F4D7970004 - Particle *sp2; // tail Particle of a Spring - //##ModelId=45F4D7970008 - float restLen; // length when spring force = 0 (Hooke force) - //##ModelId=45F4D7970009 - float changePercent; // percentage of spring length change - //##ModelId=45F4D7970011 - float maxAllowedDist; // maximum distaance allowed from the rest length - //##ModelId=45F4D7970012 - float ks; // Spring constant - //##ModelId=45F4D7970013 - float kd; // spring damping factor - //##ModelId=45F4D7970022 - Vector normal; // normal vector at the spring of a object - //##ModelId=45F4D7970027 - spring_type type; // different types of springs to construct the object, structural, radium -}; - - -#endif diff --git a/src/SoftBodySimulation/include/Vector.h b/src/SoftBodySimulation/include/Vector.h deleted file mode 100644 index ef7a0b2..0000000 --- a/src/SoftBodySimulation/include/Vector.h +++ /dev/null @@ -1,108 +0,0 @@ -#ifndef VECTOR_H -#define VECTOR_H - -#include "global.h" - -//Vector class is employed to store and operate on x, y, z coordinates, -//for particle position, particel velocity, or force information in their repective spaces - - - -//##ModelId=45F4D796031E -class Vector -{ - -public: - - //##ModelId=45F4D796031F - Vector(float Ax=0,float Ay=0,float Az=0): // constructor with variables, Ax, Ay, Az - x(Ax),y(Ay),z(Az) // default Vector(0,0,0) - { - // cout << "vector's getting born: x=" << x << ",y=" << y << ",z=" << z << endl; - } - - - //##ModelId=45F4D7960331 - ~Vector() // destructor - { - // cout << "vector's dying: x=" << x << ",y=" << y << ",z=" << z << endl; - } - - - // Computation operators. - - //Vector& operator+ (Vector const &); // #1 fcn - Vector addition with a vector to itself - //Vector& operator+ (float); // #1 fcn - Vector addition for a vector and a float - //##ModelId=45F4D796039D - friend Vector operator+ (const Vector & p, const Vector & q); // #1 fcn - Vector addition for two vectors - - - //Vector& operator- (Vector const &); // #2 fcn - Vector subtraction with a vector to itself - //Vector& operator- (float); // #2 fcn - Vector subtraction for a vector and a float - //##ModelId=45F4D79603AE - friend Vector operator- (const Vector & p, const Vector & q); // #2 fcn - Vector subtraction for two vectors - - //##ModelId=45F4D7960332 - Vector& operator* (Vector const &); // #3 fcn - Vector multiplication - //##ModelId=45F4D7960334 - Vector& operator* (float); // #3 fcn - Vector multiplication for a vector and a float - //##ModelId=45F4D79603B2 - friend Vector operator* (const Vector & p, const Vector & q); // #3 fcn - Vector multiplication for two vectors - - -// Vector& operator/ (Vector const &); // #4 fcn - Vector division with a vector to itself -// Vector& operator/ (float); // #4 fcn - Vector division for a vector and a float - - //##ModelId=45F4D79603BC - friend Vector operator/ (const Vector & p, const Vector & q); // #4 fcn - Vector division for two vectors - - - //##ModelId=45F4D79603C0 - friend Vector operator^ (Vector const &, Vector const &); // #5 fcn - Vector cross product, results in a vector - -// friend Vector operator^ (Vector const &, Vector const &); // #6 fcn - Vector dot product, results in a scalar - - - //##ModelId=45F4D796033E - Vector& operator= (Vector const &); // #7 fcn - Vector assign operator - - //##ModelId=45F4D7960340 - bool operator==(Vector const &); // #8 fcn - Vector equal operator - - - //##ModelId=45F4D7960342 - inline float getLength() // get vector length - { - return sqrt((this->x)*(this->x)+(this->y) * (this->y)+(this->z)*(this->z)); - } - - - //##ModelId=45F4D7960343 - inline void Normalize() // Normalize the Vector - { - float d = getLength(); // its length - -// cout<<"d="< -#include -#include -#include -#include -#include -#include - -using namespace std; - -// for 3D demo -///* -#define DT 0.003f // Delta time in derivatives - -#define KS 800.0f // Hooke spring constant -#define KD 15.0f // Hooke spring damping constant - -#define RKS 700.0f // radium spring constant -#define RKD 50.0f // radium spring damping constant - -#define MOUSE_KS 850.0f // mouse spring constant -#define MOUSE_KD 25.0f // mouse damping constant -#define MOUSE_REST 2.0f // mouse rest lenth - - -#define PI 3.1416f // Circle = 2*PI*RADIUS -#define GY -10.0f // Gravity -#define WIND 10.0f // External force, such as wind force -#define PRESSURE 200.0f // Pressure constant - -#define LIMIT 5.0f // Collision detection floor and wall screen size -#define Width 500 // Display window width -#define Height 500 // Display window Height - -#define SCRSIZE 10 // screen size factor for mouse dragging -#define MAX_POINTS_SPRINGS 40 //reserve temp space particle and spring -#define NUMPOINTS 10 //default number of particles on a object -#define NUMSPRINGS NUMPOINTS //default number of springs on a object -#define MASS 2.0f // point mass on the rings = 5.0 -#define RING_RADIUS 1.0f // default radius of the 2D and 3D object -//*/ -/* -#define DT 0.003f // Delta time in derivatives - -#define KS 600.0f // Hooke spring constant -#define KD 15.0f // Hooke spring damping constant - -#define RKS 600.0f // radium spring constant -#define RKD 5.0f // radium spring damping constant - -#define MOUSE_KS 150.0f // mouse spring constant -#define MOUSE_KD 25.0f // mouse damping constant -#define MOUSE_REST 2.0f // mouse rest lenth - - -#define PI 3.1416f // Circle = 2*PI*RADIUS -#define GY -10.0f // Gravity -#define WIND 10.0f // External force, such as wind force -#define PRESSURE 880.0f // Pressure constant - -#define LIMIT 5.0f // Collision detection floor and wall screen size -#define Width 500 // Display window width -#define Height 500 // Display window Height - -#define SCRSIZE 10 // screen size factor for mouse dragging -#define MAX_POINTS_SPRINGS 40 //reserve temp space particle and spring -#define NUMPOINTS 10 //default number of particles on a object -#define NUMSPRINGS NUMPOINTS //default number of springs on a object -#define MASS 0.2f // point mass on the rings = 5.0 -#define RING_RADIUS 0.5f // default radius of the 2D and 3D object - -*/ - -enum dimensionality {DIM1D, DIM2D, DIM3D}; //object type, 1D, 2D, and 3D - -enum integrator_type {EULER, MIDPOINT, RK4}; //integrator type, EULER, MIDPOINT, RK4 - -enum spring_type { SPRING_STRUCTURAL, SPRING_RADIUM, SPRING_SHEAR}; //spring type, structrual, radium, shear - -#endif /* _GLOBAL_H */ diff --git a/src/SoftBodySimulation/simulation/EulerIntegrator.cpp b/src/SoftBodySimulation/simulation/EulerIntegrator.cpp deleted file mode 100644 index 9599484..0000000 --- a/src/SoftBodySimulation/simulation/EulerIntegrator.cpp +++ /dev/null @@ -1,203 +0,0 @@ -#include "EulerIntegrator.h" - -//##ModelId=45F4D79800AE -EulerIntegrator::EulerIntegrator(Object& object) : Integrator(object)// constructor -{ -} - -//##ModelId=45F4D79800B0 -EulerIntegrator::~EulerIntegrator() // destructor -{ -} - - -// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -/*********************** - * Euler Integrator integrate all the particles on the object * - ***********************/ -//void EulerIntegrator::Derivatives(Particle[] points, int NUMP, float DT) -//void EulerIntegrator::integrate(Object object, int NUMP) - -//##ModelId=45F4D79800B2 -void EulerIntegrator::Derivatives(float deltaT, float k) -{ - - int NUMP = object->GetNumberOfParticles(); - -// cout<<"derivative,,,,,,,,,,,,,,,,,,object->GetNumberOfParticles======"<GetNumberOfParticles()<outer_points.size(); j++) - { - - temp_outer_points0[j]->mass = object->outer_points[j]->mass; - temp_outer_points0[j]->r = object->outer_points[j]->r; - temp_outer_points0[j]->v = object->outer_points[j]->v; - temp_outer_points0[j]->f = object->outer_points[j]->f; - temp_outer_points0[j]->dr = object->outer_points[j]->dr; - temp_outer_points0[j]->dv = object->outer_points[j]->dv; - temp_outer_points0[j]->OneOverMass = object->outer_points[j]->OneOverMass; - - temp_inner_points0[j]->mass = object->inner_points[j]->mass; - temp_inner_points0[j]->r = object->inner_points[j]->r; - temp_inner_points0[j]->v = object->inner_points[j]->v; - temp_inner_points0[j]->f = object->inner_points[j]->f; - temp_inner_points0[j]->dr = object->inner_points[j]->dr; - temp_inner_points0[j]->dv = object->inner_points[j]->dv; - temp_inner_points0[j]->OneOverMass = object->inner_points[j]->OneOverMass; - } - - - temp_outer_points1[i]->dv->x = k * (temp_outer_points0[i]->f->x/temp_outer_points0[i]->mass) * deltaT; - temp_outer_points1[i]->dv->y = k * (temp_outer_points0[i]->f->y/temp_outer_points0[i]->mass) * deltaT; - temp_outer_points1[i]->dv->z = k * (temp_outer_points0[i]->f->z/temp_outer_points0[i]->mass) * deltaT; - - temp_outer_points1[i]->dr->x = k * temp_outer_points0[i]->v->x * deltaT; - temp_outer_points1[i]->dr->y = k * temp_outer_points0[i]->v->y * deltaT; - temp_outer_points1[i]->dr->z = k * temp_outer_points0[i]->v->z * deltaT; - - - temp_inner_points1[i]->dv->x = k * (temp_inner_points0[i]->f->x/temp_inner_points0[i]->mass) * deltaT; - temp_inner_points1[i]->dv->y = k * (temp_inner_points0[i]->f->y/temp_inner_points0[i]->mass) * deltaT; - temp_inner_points1[i]->dv->z = k * (temp_inner_points0[i]->f->z/temp_inner_points0[i]->mass) * deltaT; - - temp_inner_points1[i]->dr->x = k * temp_inner_points0[i]->v->x * deltaT; - temp_inner_points1[i]->dr->y = k * temp_inner_points0[i]->v->y * deltaT; - temp_inner_points1[i]->dr->z = k * temp_inner_points0[i]->v->z * deltaT; - - - - -} - -//##ModelId=45F4D79800C2 -void EulerIntegrator::ynew(int i, float k, float deltaT) -{ - k1(i, k, deltaT); - - /*if(object->dim==1){ - if (i==0){ - object->outer_points[i]->dv->x = 0;//temp_outer_points1[i]->dv->x; - object->outer_points[i]->dv->y = 0;//temp_outer_points1[i]->dv->y; - object->outer_points[i]->dv->z = 0;//temp_outer_points1[i]->dv->z; - - object->outer_points[i]->dr->x = 0;//temp_outer_points1[i]->dr->x; // Change in position is velocity times the change in time - object->outer_points[i]->dr->y = 0;//temp_outer_points1[i]->dr->y; - object->outer_points[i]->dr->z = 0;//temp_outer_points1[i]->dr->z; - - object->outer_points[i]->v->x = 0;//temp_outer_points0[i]->v->x + temp_outer_points1[i]->dv->x; // Change in velocity is added to the velocity-> - object->outer_points[i]->v->y = 0;//temp_outer_points0[i]->v->y + temp_outer_points1[i]->dv->y; - object->outer_points[i]->v->z = 0;//temp_outer_points0[i]->v->z + temp_outer_points1[i]->dv->z; - - object->outer_points[i]->r->x = 0;//temp_outer_points0[i]->r->x + temp_outer_points1[i]->dr->x; // Change in position is velocity times the change in time - object->outer_points[i]->r->y = 0;//temp_outer_points0[i]->r->y + temp_outer_points1[i]->dr->y; - object->outer_points[i]->r->z = 0;//temp_outer_points0[i]->r->z + temp_outer_points1[i]->dr->z; - - - - object->inner_points[i]->dv->x = 0;//temp_inner_points1[i]->dv->x; // Change in position is velocity times the change in time - object->inner_points[i]->dv->y = 0;//temp_inner_points1[i]->dv->y; - object->inner_points[i]->dv->z = 0;//temp_inner_points1[i]->dv->z; - - object->inner_points[i]->dr->x = 0;//temp_inner_points1[i]->dr->x; // Change in position is velocity times the change in time - object->inner_points[i]->dr->y = 0;//temp_inner_points1[i]->dr->y; - object->inner_points[i]->dr->z = 0;//temp_inner_points1[i]->dr->z; - - - - object->inner_points[i]->v->x = 0;//temp_inner_points0[i]->v->x + temp_inner_points1[i]->dv->x; // Change in velocity is added to the velocity-> - object->inner_points[i]->v->y = 0;//temp_inner_points0[i]->v->y + temp_inner_points1[i]->dv->y; - object->inner_points[i]->v->z = 0;//temp_inner_points0[i]->v->z + temp_inner_points1[i]->dv->z; - - object->inner_points[i]->r->x = 0;//temp_inner_points0[i]->r->x + temp_inner_points1[i]->dr->x; // Change in position is velocity times the change in time - object->inner_points[i]->r->y = 0;//temp_inner_points0[i]->r->y + temp_inner_points1[i]->dr->y; - object->inner_points[i]->r->z = 0;//temp_inner_points0[i]->r->z + temp_inner_points1[i]->dr->z; - } - else{ - object->outer_points[i]->dv->x = temp_outer_points1[i]->dv->x; - object->outer_points[i]->dv->y = temp_outer_points1[i]->dv->y; - object->outer_points[i]->dv->z = temp_outer_points1[i]->dv->z; - - object->outer_points[i]->dr->x = temp_outer_points1[i]->dr->x; // Change in position is velocity times the change in time - object->outer_points[i]->dr->y = temp_outer_points1[i]->dr->y; - object->outer_points[i]->dr->z = temp_outer_points1[i]->dr->z; - - object->outer_points[i]->v->x = temp_outer_points0[i]->v->x + temp_outer_points1[i]->dv->x; // Change in velocity is added to the velocity-> - object->outer_points[i]->v->y = temp_outer_points0[i]->v->y + temp_outer_points1[i]->dv->y; - object->outer_points[i]->v->z = temp_outer_points0[i]->v->z + temp_outer_points1[i]->dv->z; - - object->outer_points[i]->r->x = temp_outer_points0[i]->r->x + temp_outer_points1[i]->dr->x; // Change in position is velocity times the change in time - object->outer_points[i]->r->y = temp_outer_points0[i]->r->y + temp_outer_points1[i]->dr->y; - object->outer_points[i]->r->z = temp_outer_points0[i]->r->z + temp_outer_points1[i]->dr->z; - - - - object->inner_points[i]->dv->x = temp_inner_points1[i]->dv->x; // Change in position is velocity times the change in time - object->inner_points[i]->dv->y = temp_inner_points1[i]->dv->y; - object->inner_points[i]->dv->z = temp_inner_points1[i]->dv->z; - - object->inner_points[i]->dr->x = temp_inner_points1[i]->dr->x; // Change in position is velocity times the change in time - object->inner_points[i]->dr->y = temp_inner_points1[i]->dr->y; - object->inner_points[i]->dr->z = temp_inner_points1[i]->dr->z; - - - - object->inner_points[i]->v->x = temp_inner_points0[i]->v->x + temp_inner_points1[i]->dv->x; // Change in velocity is added to the velocity-> - object->inner_points[i]->v->y = temp_inner_points0[i]->v->y + temp_inner_points1[i]->dv->y; - object->inner_points[i]->v->z = temp_inner_points0[i]->v->z + temp_inner_points1[i]->dv->z; - - object->inner_points[i]->r->x = temp_inner_points0[i]->r->x + temp_inner_points1[i]->dr->x; // Change in position is velocity times the change in time - object->inner_points[i]->r->y = temp_inner_points0[i]->r->y + temp_inner_points1[i]->dr->y; - object->inner_points[i]->r->z = temp_inner_points0[i]->r->z + temp_inner_points1[i]->dr->z; - } - } - - else{*/ - object->outer_points[i]->dv->x = temp_outer_points1[i]->dv->x; - object->outer_points[i]->dv->y = temp_outer_points1[i]->dv->y; - object->outer_points[i]->dv->z = temp_outer_points1[i]->dv->z; - - object->outer_points[i]->dr->x = temp_outer_points1[i]->dr->x; // Change in position is velocity times the change in time - object->outer_points[i]->dr->y = temp_outer_points1[i]->dr->y; - object->outer_points[i]->dr->z = temp_outer_points1[i]->dr->z; - - object->outer_points[i]->v->x = temp_outer_points0[i]->v->x + temp_outer_points1[i]->dv->x; // Change in velocity is added to the velocity-> - object->outer_points[i]->v->y = temp_outer_points0[i]->v->y + temp_outer_points1[i]->dv->y; - object->outer_points[i]->v->z = temp_outer_points0[i]->v->z + temp_outer_points1[i]->dv->z; - - object->outer_points[i]->r->x = temp_outer_points0[i]->r->x + temp_outer_points1[i]->dr->x; // Change in position is velocity times the change in time - object->outer_points[i]->r->y = temp_outer_points0[i]->r->y + temp_outer_points1[i]->dr->y; - object->outer_points[i]->r->z = temp_outer_points0[i]->r->z + temp_outer_points1[i]->dr->z; - - - - object->inner_points[i]->dv->x = temp_inner_points1[i]->dv->x; // Change in position is velocity times the change in time - object->inner_points[i]->dv->y = temp_inner_points1[i]->dv->y; - object->inner_points[i]->dv->z = temp_inner_points1[i]->dv->z; - - object->inner_points[i]->dr->x = temp_inner_points1[i]->dr->x; // Change in position is velocity times the change in time - object->inner_points[i]->dr->y = temp_inner_points1[i]->dr->y; - object->inner_points[i]->dr->z = temp_inner_points1[i]->dr->z; - - - - object->inner_points[i]->v->x = temp_inner_points0[i]->v->x + temp_inner_points1[i]->dv->x; // Change in velocity is added to the velocity-> - object->inner_points[i]->v->y = temp_inner_points0[i]->v->y + temp_inner_points1[i]->dv->y; - object->inner_points[i]->v->z = temp_inner_points0[i]->v->z + temp_inner_points1[i]->dv->z; - - object->inner_points[i]->r->x = temp_inner_points0[i]->r->x + temp_inner_points1[i]->dr->x; // Change in position is velocity times the change in time - object->inner_points[i]->r->y = temp_inner_points0[i]->r->y + temp_inner_points1[i]->dr->y; - object->inner_points[i]->r->z = temp_inner_points0[i]->r->z + temp_inner_points1[i]->dr->z; -// } -} - diff --git a/src/SoftBodySimulation/simulation/Integrator.cpp b/src/SoftBodySimulation/simulation/Integrator.cpp deleted file mode 100644 index ae46fa6..0000000 --- a/src/SoftBodySimulation/simulation/Integrator.cpp +++ /dev/null @@ -1,691 +0,0 @@ -#include "Integrator.h" -#include "Object2D.h" -#include -#include - - -//////////////// integrator constructor/////////////////////////////////////////////////////// -//##ModelId=45F4D797039C -Integrator::Integrator(Object& objectToIntegrate) : dragExists(false) -{ - this->object = &objectToIntegrate; - for(int j=0; j container for integrator computation - temp_outer_points0.push_back(new Particle (*this->object->outer_points[j])); - temp_outer_points1.push_back(new Particle (*this->object->outer_points[j])); - temp_outer_points2.push_back(new Particle (*this->object->outer_points[j])); - temp_outer_points3.push_back(new Particle (*this->object->outer_points[j])); - temp_outer_points4.push_back(new Particle (*this->object->outer_points[j])); - - // temp inner vector container for integrator computation - temp_inner_points0.push_back(new Particle (*this->object->inner_points[j])); - temp_inner_points1.push_back(new Particle (*this->object->inner_points[j])); - temp_inner_points2.push_back(new Particle (*this->object->inner_points[j])); - temp_inner_points3.push_back(new Particle (*this->object->inner_points[j])); - temp_inner_points4.push_back(new Particle (*this->object->inner_points[j])); - } - dim = DIM1D; // default object is 1D -} - -//////////////// integrator destructor/////////////////////////////////////////////////////// - -//##ModelId=45F4D79703AB -Integrator::~Integrator() -{ - temp_outer_points0.clear(); // clear up the vector and free the memory - temp_outer_points1.clear(); - temp_outer_points2.clear(); - temp_outer_points3.clear(); - temp_outer_points4.clear(); - - temp_inner_points0.clear(); - temp_inner_points1.clear(); - temp_inner_points2.clear(); - temp_inner_points3.clear(); - temp_inner_points4.clear(); -} - -/////the calculation of the integration with/without external dragging force////////////////// -//##ModelId=45F4D79703AD -void Integrator::integrate(float deltaT, bool drag, float xDrag, float yDrag) -{ - dragExists = drag; // if the mouse drag exist - mDragX = xDrag; // the mouse position x axis - mDragY = yDrag; // the mouse position y axis - AccumulateForces(); // accumulate all forces - Derivatives(deltaT, 1.0); // compute the derivatives -} - -//////////////////////// accumulate all forces //////////////////////////////////////////////// -//##ModelId=45F4D79703DA -void Integrator::AccumulateForces() // accumulate forces acted on -{ - ExternalForces(); // accumulate gravity and dragging force - SpringForces(); - - switch(dim) // there is addition pressure force for 2D&3D - { - case DIM1D: - break; - - - case DIM2D: // Pressure forces are applicable on 2D - case DIM3D: // Pressure forces are applicable on 3D - PressureForces(); - break; - } -} - -////////////////////// accumulate gravity and dragging force//////////////////////////////////////// -//##ModelId=45F4D79703CA -void Integrator::ExternalForces() -{ - int i; // runing index - float outer_x1,outer_x2,outer_y1,outer_y2,outer_z1,outer_z2; // points p1, p2 on the outer layer - float outer_rd12; // distance between p1, p2 - float outer_Fx,outer_Fy,outer_Fz; // force vector for outer layer - float f; // accumulated force - - for(i = 0; i < object->GetNumberOfParticles(); i++) // contribute gravity force - { - switch(dim) - { - case DIM2D: - // for the inner layer - object->inner_points[i]->f->x = 0;//80*sin(90*i); // initial force on x axis - object->inner_points[i]->f->y = 2.5* MASS * GY; // set the gravity along y axix - object->inner_points[i]->f->z = 0; // set to 0 - break; - case DIM3D: - // for the inner layer - object->inner_points[i]->f->x = 0;//80*sin(90*i); // initial force on x axis - object->inner_points[i]->f->y = MASS * GY; // set the gravity along y axix - object->inner_points[i]->f->z = 0; // set to 0 - break; - } - // for the outer layer - object->outer_points[i]->f->x = 0;//80* cos(25*i); // initial force on x axis - /* if (i==1) - object->outer_points[i]->f->y = 0;//MASS * GY; // set the gravity along y axix - else - */ object->outer_points[i]->f->y = MASS * GY; // set the gravity along y axix - object->outer_points[i]->f->z = 0; // set to 0 - - if(i == object->closest_i) // if is the nearest particle to mouse - { - if(dragExists) // if user clicked - { - outer_x1 = object->outer_points[ i ]->r->x; // get points X-coord - outer_y1 = object->outer_points[ i ]->r->y; // get points Y-coord - outer_z1 = object->outer_points[ i ]->r->z; // get points Z-coord - - outer_x2 = mDragX; // get Mouse X-coord - outer_y2 = mDragY; // get Mouse Y-coord - outer_z2 = 0; // set Mouse Z-coord - - outer_rd12 = // the distance nearest/mouse - sqrt((outer_x1-outer_x2) * (outer_x1-outer_x2) - +(outer_y1-outer_y2) * (outer_y1-outer_y2) - +(outer_z1-outer_z2) * (outer_z1-outer_z2) - ); - - f = (outer_rd12 - MOUSE_REST) * MOUSE_KS // the dragging force calculation - + ( - object->outer_points[i]->v->x * (outer_x1-outer_x2) - + object->outer_points[i]->v->y * (outer_y1-outer_y2) - + object->outer_points[i]->v->z * (outer_z1-outer_z2) - ) * MOUSE_KD / outer_rd12; - - // calculate spring force vector and its direction - outer_Fx = ((outer_x1 - outer_x2) / outer_rd12) * f; - outer_Fy = ((outer_y1 - outer_y2) / outer_rd12) * f; - outer_Fz = ((outer_z1 - outer_z2) / outer_rd12) * f; - - // contribute the dragging forces to the dragged point - object->outer_points[i]->f->x -= outer_Fx; - object->outer_points[i]->f->y -= outer_Fy; - object->outer_points[i]->f->z -= outer_Fz; - } - } - } -} - -/////// general spring calculation function ////////////////////////////////////////////////// -//##ModelId=45F4D79703CC -void Integrator::SpringForces() -{ - int i; // runing index - - // Three parts for computing the spring forces on all the points - for(i = 0; iGetNumberOfSprings(); i++) - { - - // Part #1 outer structual spring force contribution - // exist for 1D, 2D outer structual, and 3D outer structual - CalculateSpringForces(object->outer_springs, i); - - switch(dim) - { - case DIM1D: // intentionally empty, no more other type of springs - break; - - case DIM2D: // for 2D include the following additional springs - case DIM3D: // for 3D include the following additional springs - - { - // Part #2 inner structual spring force contribution - CalculateSpringForces - ( - ((Object2D*)object)->inner_springs, - i - ); - - // Part #3 radium spring contribution - CalculateSpringForces - ( - ((Object2D*)object)->radium_springs, - i, - false - ); - - // Part #4 shear left spring contribution - CalculateSpringForces - ( - ((Object2D*)object)->shear_springs_left, - i - ); - - // Part #5 shear right spring contribution - CalculateSpringForces - ( - ((Object2D*)object)->shear_springs_right, - i - ); - break; - } - } - } -} - - -//////////////////////////////////////////////////////////////////////////////////////////////////////////// -//##ModelId=45F4D7980004 -void Integrator::CalculateSpringForces(vectorsprings, int i, bool special) -{ - float vx12, vy12, vz12; - float Fx, Fy, Fz; - float f; - - float x1 = springs[i]->sp1->r->x; // tha head particle of the spring - float y1 = springs[i]->sp1->r->y; - float z1 = springs[i]->sp1->r->z; - - float x2 = springs[i]->sp2->r->x; // the tail particle of the spring - float y2 = springs[i]->sp2->r->y; - float z2 = springs[i]->sp2->r->z; - - // the length of the spring - float rd12 = (*springs[i]->sp1->r - *springs[i]->sp2->r).getLength(); - - if(rd12 == 0) - { - return; - } - - // Calculate normal vectors to springs - - if(special) ////?????????????????????????? - dim = DIM2D; - - switch(dim) - { - case DIM1D: - case DIM2D: - springs[i]->normal.x = -(y1 - y2) / rd12; // Normal X-vector - springs[i]->normal.y = +(x1 - x2) / rd12; // Normal Y-vector - springs[i]->normal.z = 0; // Normal Z-vector - break; - - case DIM3D: - springs[i]->normal.x = -(x1 - x2) / rd12; // Normal X-vector - springs[i]->normal.y = +(z1 - z2) / rd12; // Normal Y-vector - springs[i]->normal.z = +(y1 - y2) / rd12; // Normal Z-vector - break; - } - - // velocity vector of the two ends of the spring - vx12 = springs[i]->sp1->v->x - springs[i]->sp2->v->x; - vy12 = springs[i]->sp1->v->y - springs[i]->sp2->v->y; - vz12 = springs[i]->sp1->v->z - springs[i]->sp2->v->z; - - // hook spring force of its damping force - f = (rd12 - springs[i]->restLen) * KS - + (vx12 * (x1 - x2) - + vy12 * (y1 - y2) - + vz12 * (z1 - z2)) * KD / rd12; - - // spring vector on x, y, z axix - Fx = ((x1 - x2) / rd12) * f; - Fy = ((y1 - y2) / rd12) * f; - Fz = ((z1 - z2) / rd12) * f; - - // accumulate the force on the start partilce of the spring - springs[i]->sp1->f->x -= Fx; - springs[i]->sp1->f->y -= Fy; - springs[i]->sp1->f->z -= Fz; - // accumulate the force on the end partilce of the spring - springs[i]->sp2->f->x += Fx; - springs[i]->sp2->f->y += Fy; - springs[i]->sp2->f->z += Fz; -} - -///////////////////////////////////////////////////////////////////////////////////////////////////////////// - -//##ModelId=45F4D79703CE -void Integrator::PressureForces() -{ - int i; // runing index - - float inner_x1,inner_x2,inner_y1,inner_y2,inner_z1,inner_z2; // points p1, p2 on the inner layer - float outer_x1,outer_x2,outer_y1,outer_y2,outer_z1,outer_z2; // points p1, p2 on the outer layer - - float inner_rd12; // distance of innter p1, p2 - float outer_rd12; // distance of outer p1, p2 - - float inner_volume=0.0; // inner circle inner_volume - float outer_volume=0.0; // outer circle outer_volume - - float inner_p_accu; // inner pressure force accumulation - float outer_p_accu; // outer pressure force accumulation - - - // pressure force for the inner and outer circles - - for(i=0; iGetNumberOfSprings(); i++) // for every spring on the object - { - inner_x1 = object->inner_springs[i]->sp1->r->x; // inner spring start particle - inner_y1 = object->inner_springs[i]->sp1->r->y; - inner_z1 = object->inner_springs[i]->sp1->r->z; - - inner_x2 = object->inner_springs[i]->sp2->r->x; // inner spring end particle - inner_y2 = object->inner_springs[i]->sp2->r->y; - inner_z2 = object->inner_springs[i]->sp2->r->z; - - inner_rd12 = // need the inner spring length for volume - sqrt( - (inner_x1-inner_x2)*(inner_x1-inner_x2) - +(inner_y1-inner_y2)*(inner_y1-inner_y2) - +(inner_z1-inner_z2)*(inner_z1-inner_z2) - ); - - // based on the Gauss Theorem??????????????????????????????// calculation of inner circle volume - inner_volume += 40.5 * - fabs(inner_x1 - inner_x2) * - ( - fabs(object->inner_springs[i]->normal.x) - +fabs(object->inner_springs[i]->normal.y) - +fabs(object->inner_springs[i]->normal.z) - ) * - (inner_rd12); - - outer_x1 = object->outer_springs[i]->sp1->r->x; // outer spring start particle - outer_y1 = object->outer_springs[i]->sp1->r->y; - outer_z1 = object->outer_springs[i]->sp1->r->z; - - outer_x2 = object->outer_springs[i]->sp2->r->x; // outer spring end particle - outer_y2 = object->outer_springs[i]->sp2->r->y; - outer_z2 = object->outer_springs[i]->sp2->r->z; - - outer_rd12 = // need the outer spring length for volume - sqrt( - (outer_x1-outer_x2)*(outer_x1-outer_x2) - +(outer_y1-outer_y2)*(outer_y1-outer_y2) - +(outer_z1-outer_z2)*(outer_z1-outer_z2) - ); - - outer_volume += 40.5 * // calculation of outer layer volume - fabs(outer_x1 - outer_x2) * -// fabs(outer_y1 - outer_y2) * -// fabs(outer_z1 - outer_z2) * - ( - fabs(object->outer_springs[i]->normal.x) - +fabs(object->outer_springs[i]->normal.y) - +fabs(object->outer_springs[i]->normal.z) - ) * - (outer_rd12); - } - - // calculation inner and outer layer pressure force - for(i=0; iGetNumberOfSprings(); i++) - { - inner_x1 = object->inner_springs[i]->sp1->r->x; - inner_y1 = object->inner_springs[i]->sp1->r->y; - inner_z1 = object->inner_springs[i]->sp1->r->z; - - inner_x2 = object->inner_springs[i]->sp2->r->x; - inner_y2 = object->inner_springs[i]->sp2->r->y; - inner_z2 = object->inner_springs[i]->sp2->r->z; - - inner_rd12=sqrt((inner_x1-inner_x2)*(inner_x1-inner_x2) - + (inner_y1-inner_y2)*(inner_y1-inner_y2) - + (inner_z1-inner_z2)*(inner_z1-inner_z2) - ); - - inner_p_accu = inner_rd12 * ((Object2D*)object)->getPressure() * (1.0f / inner_volume); - - // contribute inner pressure to the start particle on this spring - object->inner_springs[i]->sp1->f->x += object->inner_springs[i]->normal.x*inner_p_accu; - object->inner_springs[i]->sp1->f->y += object->inner_springs[i]->normal.y*inner_p_accu; - object->inner_springs[i]->sp1->f->z += object->inner_springs[i]->normal.z*inner_p_accu; - - // contribute inner pressure to the end particle on this spring - object->inner_springs[i]->sp2->f->x += object->inner_springs[i]->normal.x*inner_p_accu; - object->inner_springs[i]->sp2->f->y += object->inner_springs[i]->normal.y*inner_p_accu; - object->inner_springs[i]->sp2->f->z += object->inner_springs[i]->normal.z*inner_p_accu; - - //////////////////////////////////////////// - outer_x1 = object->outer_springs[i]->sp1->r->x; - outer_y1 = object->outer_springs[i]->sp1->r->y; - outer_z1 = object->outer_springs[i]->sp1->r->z; - - outer_x2 = object->outer_springs[i]->sp2->r->x; - outer_y2 = object->outer_springs[i]->sp2->r->y; - outer_z2 = object->outer_springs[i]->sp2->r->z; - - outer_rd12=sqrt((outer_x1-outer_x2)*(outer_x1-outer_x2) - + (outer_y1-outer_y2)*(outer_y1-outer_y2) - + (outer_z1-outer_z2)*(outer_z1-outer_z2) - ); - - - outer_p_accu = outer_rd12 * ((Object2D*)object)->getPressure() * (1.0f / outer_volume); - - // contribute outer pressure to the start particle on this spring - object->outer_springs[i]->sp1->f->x += object->outer_springs[i]->normal.x * outer_p_accu; - object->outer_springs[i]->sp1->f->y += object->outer_springs[i]->normal.y * outer_p_accu; - object->outer_springs[i]->sp1->f->z += object->outer_springs[i]->normal.z * outer_p_accu; - - // contribute outer pressure to the end particle on this spring - object->outer_springs[i]->sp2->f->x += object->outer_springs[i]->normal.x * outer_p_accu; - object->outer_springs[i]->sp2->f->y += object->outer_springs[i]->normal.y * outer_p_accu; - object->outer_springs[i]->sp2->f->z += object->outer_springs[i]->normal.z * outer_p_accu; - } -} -//*/ - -/* -//////////////////////////////another unused pressure function based on the triangle face normal//////////// -void Integrator::PressureForces() -{ - int i; // runing index - - float inner_x1,inner_x2,inner_y1,inner_y2,inner_z1,inner_z2; // points inner_p1, inner_p2 on the inner circle - float outer_x1,outer_x2,outer_y1,outer_y2,outer_z1,outer_z2; // points outer_p1, outer_p2 on the outer circle - - float inner_rd12; // length of inner_p1, inner_p2 - float outer_rd12; // length of outer_p1, outer_p2 - - float inner_volume=0.0; // inner circle inner_volume - float outer_volume=0.0; // outer circle outer_volume - - float inner_p_accu=0;; // pressure force accumulation - float outer_p_accu=0;; // pressure force accumulation - - for(i=0;iinner_faces.size();i++) - { - object->inner_faces[i]->CalNormalNField(); - object->outer_faces[i]->CalNormalNField(); - } - - // pressure force for the inner circle - - for(i=0; iouter_faces.size(); i++) // calculation of inner circle volume - { - inner_volume += 0.5 * (object->inner_faces[i]->fp1->r->x + - object->inner_faces[i]->fp2->r->x + - object->inner_faces[i]->fp3->r->x)* - (object->inner_faces[i]->normal->x) * - (object->inner_faces[i]->normal->getLength()); - - outer_volume += 0.5 * (object->outer_faces[i]->fp1->r->x + - object->outer_faces[i]->fp2->r->x + - object->outer_faces[i]->fp3->r->x)* - (object->outer_faces[i]->normal->x) * - (object->outer_faces[i]->normal->getLength()); - } - - - for(i = 0; i < object->outer_faces.size(); i++) // calculation inner pressure force - { - inner_p_accu=(object->inner_faces[i]->normal->x) * - (object->inner_faces[i]->normal->getLength())* - ((Object2D*)object)->getPressure()*(1.0f/inner_volume); - - object->inner_faces[i]->fp1->f->x += 0.5 * inner_p_accu; - object->inner_faces[i]->fp1->f->y += 0.5 * inner_p_accu; - object->inner_faces[i]->fp1->f->z += 0.5 * inner_p_accu; - - object->inner_faces[i]->fp2->f->x += 0.5 * inner_p_accu; - object->inner_faces[i]->fp2->f->y += 0.5 * inner_p_accu; - object->inner_faces[i]->fp2->f->z += 0.5 * inner_p_accu; - - object->inner_faces[i]->fp3->f->x += 0.5 * inner_p_accu; - object->inner_faces[i]->fp3->f->y += 0.5 * inner_p_accu; - object->inner_faces[i]->fp3->f->z += 0.5 * inner_p_accu; - - //////////////////////////////////////////// - - outer_p_accu=(object->outer_faces[i]->normal->x) * - (object->outer_faces[i]->normal->getLength()) * - ((Object2D*)object)->getPressure() * (1.0f/outer_volume); - - object->outer_faces[i]->fp1->f->x += 0.5 * outer_p_accu; - object->outer_faces[i]->fp1->f->y += 0.5 * outer_p_accu; - object->outer_faces[i]->fp1->f->z += 0.5 * outer_p_accu; - - object->outer_faces[i]->fp2->f->x += 0.5 * outer_p_accu; - object->outer_faces[i]->fp2->f->y += 0.5 * outer_p_accu; - object->outer_faces[i]->fp2->f->z += 0.5 * outer_p_accu; - - object->outer_faces[i]->fp3->f->x += 0.5 * outer_p_accu; - object->outer_faces[i]->fp3->f->y += 0.5 * outer_p_accu; - object->outer_faces[i]->fp3->f->z += 0.5 * outer_p_accu; - } -} - - -//*/ - - - - -///////// calculate the collision detection ////////////////////////////////////////////////// -//##ModelId=45F4D7980001 -void Integrator::CollisionDetection(int i) -{ - bool changed = false; - float decay = 0.5; - - -/////////////////////check the outer layer particle if collide with the wall//////////////// - // Check #1 - X boundaries left - if((object->outer_points[i]->r->x + object->outer_points[i]->dr->x) < -LIMIT) - { - object->outer_points[i]->dr->x = -LIMIT - object->outer_points[i]->r->x; - changed = true; - } // Check #1 - X boundaries right - else if((object->outer_points[i]->r->x + object->outer_points[i]->dr->x) > LIMIT) - { - object->outer_points[i]->dr->x = LIMIT - object->outer_points[i]->r->x; - changed = true; - } - - if(changed) - { - object->outer_points[i]->v->x = -decay * object->outer_points[i]->v->x; - object->outer_points[i]->v->y = 0.5 * object->outer_points[i]->v->y; - object->outer_points[i]->v->z = 0.5 * object->outer_points[i]->v->z; - - /* if(object->dim == 1){ - if(i==1){ - object->outer_points[i]->r->x = 0;//object->outer_points[i]->r->x + object->outer_points[i]->dr->x; - } - - } - else{ - object->outer_points[i]->r->x = object->outer_points[i]->r->x + object->outer_points[i]->dr->x; - }*/ - object->outer_points[i]->r->x = object->outer_points[i]->r->x + object->outer_points[i]->dr->x; - changed = false; - } - - - // Check #2 - Y boundaries left - if((object->outer_points[i]->r->y + object->outer_points[i]->dr->y) < -LIMIT) - { - object->outer_points[i]->dr->y = -LIMIT - object->outer_points[i]->r->y; - changed = true; - } // Check #2 - Y boundaries right - else if((object->outer_points[i]->r->y + object->outer_points[i]->dr->y) > LIMIT) - { - object->outer_points[i]->dr->y = LIMIT - object->outer_points[i]->r->y; - changed = true; - } - - if(changed) - { - object->outer_points[i]->v->x = 0.5 * object->outer_points[i]->v->x; - object->outer_points[i]->v->y = - decay * object->outer_points[i]->v->y; - object->outer_points[i]->v->z = 0.5 * object->outer_points[i]->v->z; - - object->outer_points[i]->r->y = object->outer_points[i]->r->y + object->outer_points[i]->dr->y; - changed = false; - } - - - // Check #3 - Z boundary left - if((object->outer_points[i]->r->z + object->outer_points[i]->dr->z) < -LIMIT ) - { - object->outer_points[i]->dr->z = -LIMIT - object->outer_points[i]->r->z; - changed = true; - } // Check #3 - Z boundary right - else if((object->outer_points[i]->r->z + object->outer_points[i]->dr->z) > LIMIT ) - { - object->outer_points[i]->dr->z = LIMIT - object->outer_points[i]->r->z; - changed = true; - } - - - if(changed) - { - object->outer_points[i]->v->x = 0.5 * object->outer_points[i]->v->x; - object->outer_points[i]->v->y = 0.5 * object->outer_points[i]->v->y; - object->outer_points[i]->v->z = - decay * object->outer_points[i]->v->z; - - object->outer_points[i]->r->z = object->outer_points[i]->r->z + object->outer_points[i]->dr->z; - changed = false; - } - - - - // Check #4 - Boundary limitations - if (object->outer_points[i]->r->x < -LIMIT) object->outer_points[i]->r->x = -LIMIT; - if (object->outer_points[i]->r->y < -LIMIT) object->outer_points[i]->r->y = -LIMIT; - if (object->outer_points[i]->r->z < -LIMIT) object->outer_points[i]->r->z = -LIMIT; - - if (object->outer_points[i]->r->x > LIMIT) object->outer_points[i]->r->x = LIMIT; - if (object->outer_points[i]->r->y > LIMIT) object->outer_points[i]->r->y = LIMIT; - if (object->outer_points[i]->r->z > LIMIT) object->outer_points[i]->r->z = LIMIT; - - - -///////////////////////////////////////////////////////////////////////////////////////////////////////// - -/////////////////////check the inner layer particle if collide with the wall//////////////// - - // Check #1 - X boundaries - if((object->inner_points[i]->r->x + object->inner_points[i]->dr->x) < -LIMIT) - { - object->inner_points[i]->dr->x = -LIMIT - object->inner_points[i]->r->x; - changed = true; - } - else if((object->inner_points[i]->r->x + object->inner_points[i]->dr->x) > LIMIT) - { - object->inner_points[i]->dr->x = LIMIT - object->inner_points[i]->r->x; - changed = true; - } - - if(changed) - { - object->inner_points[i]->v->x = -decay * object->inner_points[i]->v->x; - object->inner_points[i]->v->y = 0.5 * object->inner_points[i]->v->y; - object->inner_points[i]->v->z = 0.5 * object->inner_points[i]->v->z; - - object->inner_points[i]->r->x = object->inner_points[i]->r->x + object->inner_points[i]->dr->x; - changed = false; - - } - - - // Check #2 - Y boundaries - if((object->inner_points[i]->r->y + object->inner_points[i]->dr->y) < -LIMIT) - { - object->inner_points[i]->dr->y = -LIMIT - object->inner_points[i]->r->y; - changed = true; - } - else if((object->inner_points[i]->r->y + object->inner_points[i]->dr->y) > LIMIT) - { - object->inner_points[i]->dr->y = LIMIT - object->inner_points[i]->r->y; - changed = true; - } - - if(changed) - { - object->inner_points[i]->v->x = 0.5 * object->inner_points[i]->v->x; - object->inner_points[i]->v->y = - decay * object->inner_points[i]->v->y; - object->inner_points[i]->v->z = 0.5 * object->inner_points[i]->v->z; - - object->inner_points[i]->r->y = object->inner_points[i]->r->y + object->inner_points[i]->dr->y; - changed = false; - } - - - // Check #3 - Z boundary - - if((object->inner_points[i]->r->z + object->inner_points[i]->dr->z) < -LIMIT ) - { - object->inner_points[i]->dr->z = -LIMIT - object->inner_points[i]->r->z; - changed = true; - } - else if((object->inner_points[i]->r->z + object->inner_points[i]->dr->z) > LIMIT ) - { - object->inner_points[i]->dr->z = LIMIT - object->inner_points[i]->r->z; - changed = true; - } - - - if(changed) - { - object->inner_points[i]->v->x = 0.5 * object->inner_points[i]->v->x; - object->inner_points[i]->v->y = 0.5 * object->inner_points[i]->v->y; - object->inner_points[i]->v->z = - decay * object->inner_points[i]->v->z; - - object->inner_points[i]->r->z = object->inner_points[i]->r->z + object->inner_points[i]->dr->z; - changed = false; - } - - - - // Check #4 - Boundary limitations - if (object->inner_points[i]->r->x < -LIMIT) object->inner_points[i]->r->x = -LIMIT; - if (object->inner_points[i]->r->y < -LIMIT) object->inner_points[i]->r->y = -LIMIT; - if (object->inner_points[i]->r->z < -LIMIT) object->inner_points[i]->r->z = -LIMIT; - - if (object->inner_points[i]->r->x > LIMIT) object->inner_points[i]->r->x = LIMIT; - if (object->inner_points[i]->r->y > LIMIT) object->inner_points[i]->r->y = LIMIT; - if (object->inner_points[i]->r->z > LIMIT) object->inner_points[i]->r->z = LIMIT; - - -} - diff --git a/src/SoftBodySimulation/simulation/Makefile b/src/SoftBodySimulation/simulation/Makefile deleted file mode 100644 index 2255889..0000000 --- a/src/SoftBodySimulation/simulation/Makefile +++ /dev/null @@ -1,26 +0,0 @@ -CC = g++ -CPPFLAGS = -g # -Wall -Wmissing-declarations -Wmissing-prototypes -ansi -pedantic -LIBS = -L/encs/lib/ -lglut -lGL -lGLU -lGL -L/usr/X11R6/lib -lXmu -lXext -lXt -lXi -lX11 -TARGET = softbody -INCLUDE = -I/encs/include/GL -I../include - -OBJECTS := EulerIntegrator.o Integrator.o MidpointIntegrator.o RungeKutta4Integrator.o ViewSpace.o Simulation.o - -.SUFFIXES: .cpp - -.cpp.o: - $(CC) -c $(CPPFLAGS) $(INCLUDE) -o $@ $< - - -all: $(OBJECTS) - -Simulation.o: Simulation.cpp ../include/Simulation.h -ViewSpace.o: ViewSpace.cpp ../include/ViewSpace.h -EulerIntegrator.o: EulerIntegrator.cpp ../include/EulerIntegrator.h -Integrator.o: Integrator.cpp ../include/Integrator.h -MidpointIntegrator.o: MidpointIntegrator.cpp ../include/MidpointIntegrator.h -RungeKutta4Integrator.o: RungeKutta4Integrator.cpp ../include/RungeKutta4Integrator.h - -clean: - -rm -rf $(TARGET) *.pch *.ilk *.opt *.plg *.ncb Debug Release *.o - diff --git a/src/SoftBodySimulation/simulation/MidpointIntegrator.cpp b/src/SoftBodySimulation/simulation/MidpointIntegrator.cpp deleted file mode 100644 index 81bcc0e..0000000 --- a/src/SoftBodySimulation/simulation/MidpointIntegrator.cpp +++ /dev/null @@ -1,96 +0,0 @@ -#include "MidpointIntegrator.h" -#include "Object2D.h" - -//##ModelId=45F4D79702B3 -MidpointIntegrator::MidpointIntegrator(Object& object) : EulerIntegrator(object) -{ -} - -//##ModelId=45F4D79702B5 -MidpointIntegrator::~MidpointIntegrator() -{ -} - -//##ModelId=45F4D79702B6 -void MidpointIntegrator::k2(int i, float k, float deltaT) -{ - k1(i, k, deltaT); - -// memcpy(temp_inner_points2, temp_inner_points1, sizeof(Particle) * MAX_POINTS_SPRINGS); -// memcpy(temp_outer_points2, temp_outer_points1, sizeof(Particle) * MAX_POINTS_SPRINGS); - for(int j=0; jouter_points.size(); j++) - { - - temp_outer_points1[j]->mass = temp_outer_points0[j]->mass; - temp_outer_points1[j]->r = temp_outer_points0[j]->r; - temp_outer_points1[j]->v = temp_outer_points0[j]->v; - temp_outer_points1[j]->f = temp_outer_points0[j]->f; - temp_outer_points1[j]->dr = temp_outer_points0[j]->dr; - temp_outer_points1[j]->dv = temp_outer_points0[j]->dv; -// temp_outer_points1[j]->OneOverMass = temp_outer_points0[j]->OneOverMass; - - temp_inner_points1[j]->mass = temp_inner_points0[j]->mass; - temp_inner_points1[j]->r = temp_inner_points0[j]->r; - temp_inner_points1[j]->v = temp_inner_points0[j]->v; - temp_inner_points1[j]->f = temp_inner_points0[j]->f; - temp_inner_points1[j]->dr = temp_inner_points0[j]->dr; - temp_inner_points1[j]->dv = temp_inner_points0[j]->dv; -// temp_inner_points1[j]->OneOverMass = temp_inner_points0[j]->OneOverMass; - } - - - temp_inner_points2[i]->dv->x = temp_inner_points0[i]->dv->x + k * 0.5 * (temp_inner_points1[i]->f->x/temp_inner_points1[i]->mass) * deltaT; - temp_inner_points2[i]->dv->y = temp_inner_points0[i]->dv->y + k * 0.5 * (temp_inner_points1[i]->f->y/temp_inner_points1[i]->mass) * deltaT; - temp_inner_points2[i]->dv->z = temp_inner_points0[i]->dv->z + k * 0.5 * (temp_inner_points1[i]->f->z/temp_inner_points1[i]->mass) * deltaT; - - temp_inner_points2[i]->dr->x = temp_inner_points0[i]->dr->x + k * 0.5 * temp_inner_points1[i]->dv->x * deltaT; - temp_inner_points2[i]->dr->y = temp_inner_points0[i]->dr->y + k * 0.5 * temp_inner_points1[i]->dv->y * deltaT; - temp_inner_points2[i]->dr->z = temp_inner_points0[i]->dr->z + k * 0.5 * temp_inner_points1[i]->dv->z * deltaT; - - temp_outer_points2[i]->dv->x = temp_outer_points0[i]->dv->x + k * 0.5 * (temp_outer_points1[i]->f->x/temp_outer_points1[i]->mass) * deltaT; - temp_outer_points2[i]->dv->y = temp_outer_points0[i]->dv->y + k * 0.5 * (temp_outer_points1[i]->f->y/temp_outer_points1[i]->mass) * deltaT; - temp_outer_points2[i]->dv->z = temp_outer_points0[i]->dv->z + k * 0.5 * (temp_outer_points1[i]->f->z/temp_outer_points1[i]->mass) * deltaT; - - temp_outer_points2[i]->dr->x = temp_outer_points0[i]->dr->x + k * 0.5 * temp_outer_points1[i]->dv->x * deltaT; - temp_outer_points2[i]->dr->y = temp_outer_points0[i]->dr->y + k * 0.5 * temp_outer_points1[i]->dv->y * deltaT; - temp_outer_points2[i]->dr->z = temp_outer_points0[i]->dr->z + k * 0.5 * temp_outer_points1[i]->dv->z * deltaT; -} - -//##ModelId=45F4D79702C4 -void MidpointIntegrator::ynew(int i, float k, float deltaT) -{ - k2(i, k, deltaT); - - object->inner_points[i]->dv->x = temp_inner_points2[i]->dv->x; - object->inner_points[i]->dv->y = temp_inner_points2[i]->dv->y; - object->inner_points[i]->dv->z = temp_inner_points2[i]->dv->z; - - object->inner_points[i]->dr->x = temp_inner_points2[i]->dr->x; // Change in position is velocity times the change in time - object->inner_points[i]->dr->y = temp_inner_points2[i]->dr->y; - object->inner_points[i]->dr->z = temp_inner_points2[i]->dr->z; - - object->inner_points[i]->v->x = temp_inner_points0[i]->v->x + temp_inner_points2[i]->v->x; // Change in velocity is added to the velocity. - object->inner_points[i]->v->y = temp_inner_points0[i]->v->y + temp_inner_points2[i]->v->y; - object->inner_points[i]->v->z = temp_inner_points0[i]->v->z + temp_inner_points2[i]->v->z; - - object->inner_points[i]->r->x = temp_inner_points0[i]->r->x + temp_inner_points2[i]->r->x; // Change in position is velocity times the change in time - object->inner_points[i]->r->y = temp_inner_points0[i]->r->y + temp_inner_points2[i]->r->y; - object->inner_points[i]->r->z = temp_inner_points0[i]->r->z + temp_inner_points2[i]->r->z; - - - object->outer_points[i]->dv->x = temp_outer_points2[i]->dv->x; - object->outer_points[i]->dv->y = temp_outer_points2[i]->dv->y; - object->outer_points[i]->dv->z = temp_outer_points2[i]->dv->z; - - object->outer_points[i]->dr->x = temp_outer_points2[i]->dr->x; // Change in position is velocity times the change in time - object->outer_points[i]->dr->y = temp_outer_points2[i]->dr->y; - object->outer_points[i]->dr->z = temp_outer_points2[i]->dr->z; - - object->outer_points[i]->v->x = temp_outer_points0[i]->v->x + temp_outer_points2[i]->v->x; // Change in velocity is added to the velocity. - object->outer_points[i]->v->y = temp_outer_points0[i]->v->y + temp_outer_points2[i]->v->y; - object->outer_points[i]->v->z = temp_outer_points0[i]->v->z + temp_outer_points2[i]->v->z; - - object->outer_points[i]->r->x = temp_outer_points0[i]->r->x + temp_outer_points2[i]->r->x; // Change in position is velocity times the change in time - object->outer_points[i]->r->y = temp_outer_points0[i]->r->y + temp_outer_points2[i]->r->y; - object->outer_points[i]->r->z = temp_outer_points0[i]->r->z + temp_outer_points2[i]->r->z; -} diff --git a/src/SoftBodySimulation/simulation/RungeKutta4Integrator.cpp b/src/SoftBodySimulation/simulation/RungeKutta4Integrator.cpp deleted file mode 100644 index ed9bd46..0000000 --- a/src/SoftBodySimulation/simulation/RungeKutta4Integrator.cpp +++ /dev/null @@ -1,167 +0,0 @@ -#include "RungeKutta4Integrator.h" - -//##ModelId=45F4D7970041 -RungeKutta4Integrator::RungeKutta4Integrator(Object& object) : MidpointIntegrator(object) -{ -} - -//##ModelId=45F4D7970043 -RungeKutta4Integrator::~RungeKutta4Integrator() -{ -} - - -//##ModelId=45F4D7970044 -void RungeKutta4Integrator::k4(int i, float k, float deltaT) -{ - k3(i, k, deltaT); - -// memcpy(temp_inner_points4, temp_inner_points3, sizeof(Particle) * MAX_POINTS_SPRINGS); -// memcpy(temp_outer_points4, temp_outer_points3, sizeof(Particle) * MAX_POINTS_SPRINGS); - - - for(int j=1; jouter_points.size(); j++) - { - - temp_outer_points4[j]->mass = temp_outer_points3[j]->mass; - temp_outer_points4[j]->r = temp_outer_points3[j]->r; - temp_outer_points4[j]->v = temp_outer_points3[j]->v; - temp_outer_points4[j]->f = temp_outer_points3[j]->f; - temp_outer_points4[j]->dr = temp_outer_points3[j]->dr; - temp_outer_points4[j]->dv = temp_outer_points3[j]->dv; - temp_outer_points4[j]->OneOverMass = temp_outer_points3[j]->OneOverMass; - - temp_inner_points4[j]->mass = temp_inner_points3[j]->mass; - temp_inner_points4[j]->r = temp_inner_points3[j]->r; - temp_inner_points4[j]->v = temp_inner_points3[j]->v; - temp_inner_points4[j]->f = temp_inner_points3[j]->f; - temp_inner_points4[j]->dr = temp_inner_points3[j]->dr; - temp_inner_points4[j]->dv = temp_inner_points3[j]->dv; - temp_inner_points4[j]->OneOverMass = temp_inner_points3[j]->OneOverMass; - } - -// k1(i, 0.5 * k, deltaT); - - temp_inner_points4[i]->dv->x = temp_inner_points0[i]->dv->x + k * (temp_inner_points3[i]->f->x/temp_inner_points3[i]->mass) * deltaT; - temp_inner_points4[i]->dv->y = temp_inner_points0[i]->dv->y + k * (temp_inner_points3[i]->f->y/temp_inner_points3[i]->mass) * deltaT; - temp_inner_points4[i]->dv->z = temp_inner_points0[i]->dv->z + k * (temp_inner_points3[i]->f->z/temp_inner_points3[i]->mass) * deltaT; - - temp_inner_points4[i]->dr->x = temp_inner_points0[i]->dr->x + k * temp_inner_points3[i]->dv->x * deltaT; - temp_inner_points4[i]->dr->y = temp_inner_points0[i]->dr->y + k * temp_inner_points3[i]->dv->y * deltaT; - temp_inner_points4[i]->dr->z = temp_inner_points0[i]->dr->z + k * temp_inner_points3[i]->dv->z * deltaT; - - temp_outer_points4[i]->dv->x = temp_outer_points0[i]->dv->x + k * (temp_outer_points3[i]->f->x/temp_outer_points3[i]->mass) * deltaT; - temp_outer_points4[i]->dv->y = temp_outer_points0[i]->dv->y + k * (temp_outer_points3[i]->f->y/temp_outer_points3[i]->mass) * deltaT; - temp_outer_points4[i]->dv->z = temp_outer_points0[i]->dv->z + k * (temp_outer_points3[i]->f->z/temp_outer_points3[i]->mass) * deltaT; - - temp_outer_points4[i]->dr->x = temp_outer_points0[i]->dr->x + k * temp_outer_points3[i]->dv->x * deltaT; - temp_outer_points4[i]->dr->y = temp_outer_points0[i]->dr->y + k * temp_outer_points3[i]->dv->y * deltaT; - temp_outer_points4[i]->dr->z = temp_outer_points0[i]->dr->z + k * temp_outer_points3[i]->dv->z * deltaT; - -} - -//##ModelId=45F4D7970050 -void RungeKutta4Integrator::k3(int i, float k, float deltaT) -{ - k2(i, k, deltaT); - -// memcpy(temp_inner_points3, temp_inner_points2, sizeof(Particle) * MAX_POINTS_SPRINGS); -// memcpy(temp_outer_points3, temp_outer_points2, sizeof(Particle) * MAX_POINTS_SPRINGS); - - - for(int j=1; jouter_points.size(); j++) - { - - temp_outer_points3[j]->mass = temp_outer_points2[j]->mass; - temp_outer_points3[j]->r = temp_outer_points2[j]->r; - temp_outer_points3[j]->v = temp_outer_points2[j]->v; - temp_outer_points3[j]->f = temp_outer_points2[j]->f; - temp_outer_points3[j]->dr = temp_outer_points2[j]->dr; - temp_outer_points3[j]->dv = temp_outer_points2[j]->dv; - temp_outer_points3[j]->OneOverMass = temp_outer_points2[j]->OneOverMass; - - temp_inner_points3[j]->mass = temp_inner_points2[j]->mass; - temp_inner_points3[j]->r = temp_inner_points2[j]->r; - temp_inner_points3[j]->v = temp_inner_points2[j]->v; - temp_inner_points3[j]->f = temp_inner_points2[j]->f; - temp_inner_points3[j]->dr = temp_inner_points2[j]->dr; - temp_inner_points3[j]->dv = temp_inner_points2[j]->dv; - temp_inner_points3[j]->OneOverMass = temp_inner_points2[j]->OneOverMass; - } - -// k1(i, 0.5 * k, deltaT); - - temp_inner_points3[i]->dv->x = temp_inner_points0[i]->dv->x + k * 0.5 * (temp_inner_points2[i]->f->x/temp_inner_points2[i]->mass) * deltaT; - temp_inner_points3[i]->dv->y = temp_inner_points0[i]->dv->y + k * 0.5 * (temp_inner_points2[i]->f->y/temp_inner_points2[i]->mass) * deltaT; - temp_inner_points3[i]->dr->x = temp_inner_points0[i]->dr->x + k * 0.5 * temp_inner_points2[i]->dv->x * deltaT; - temp_inner_points3[i]->dr->y = temp_inner_points0[i]->dr->y + k * 0.5 * temp_inner_points2[i]->dv->y * deltaT; - - temp_outer_points3[i]->dv->x = temp_outer_points0[i]->dv->x + k * 0.5 * (temp_outer_points2[i]->f->x/temp_outer_points2[i]->mass) * deltaT; - temp_outer_points3[i]->dv->y = temp_outer_points0[i]->dv->y + k * 0.5 * (temp_outer_points2[i]->f->y/temp_outer_points2[i]->mass) * deltaT; - temp_outer_points3[i]->dr->x = temp_outer_points0[i]->dr->x + k * 0.5 * temp_outer_points2[i]->dv->x * deltaT; - temp_outer_points3[i]->dr->y = temp_outer_points0[i]->dr->y + k * 0.5 * temp_outer_points2[i]->dv->y * deltaT; -} - -/* -void RungeKutta4Integrator::k2(int i, float k, float deltaT) -{ - k1(i, k, deltaT); - - memcpy(temp_inner_points2, temp_inner_points1, sizeof(Particle) * MAX_POINTS_SPRINGS); - memcpy(temp_outer_points2, temp_outer_points1, sizeof(Particle) * MAX_POINTS_SPRINGS); - - -// k1(i, 0.5 * k, deltaT); - - temp_inner_points2[i].v->x = temp_inner_points0[i].v->x + k * 0.5 * (temp_inner_points1[i].f->x/temp_inner_points1[i].mass) * deltaT; - temp_inner_points2[i].v->y = temp_inner_points0[i].v->y + k * 0.5 * (temp_inner_points1[i].f->y/temp_inner_points1[i].mass) * deltaT; - temp_inner_points2[i].r->x = temp_inner_points0[i].r->x + k * 0.5 * temp_inner_points1[i].v->x * deltaT; - temp_inner_points2[i].r->y = temp_inner_points0[i].r->y + k * 0.5 * temp_inner_points1[i].v->y * deltaT; - - temp_outer_points2[i].v->x = temp_outer_points0[i].v->x + k * 0.5 * (temp_outer_points1[i].f->x/temp_outer_points1[i].mass) * deltaT; - temp_outer_points2[i].v->y = temp_outer_points0[i].v->y + k * 0.5 * (temp_outer_points1[i].f->y/temp_outer_points1[i].mass) * deltaT; - temp_outer_points2[i].r->x = temp_outer_points0[i].r->x + k * 0.5 * temp_outer_points1[i].v->x * deltaT; - temp_outer_points2[i].r->y = temp_outer_points0[i].r->y + k * 0.5 * temp_outer_points1[i].v->y * deltaT; -} -*/ - -//##ModelId=45F4D7970054 -void RungeKutta4Integrator::ynew(int i, float k, float deltaT) -{ - k4(i, k, deltaT); - - object->inner_points[i]->dv->x = temp_inner_points4[i]->dv->x; - object->inner_points[i]->dv->y = temp_inner_points4[i]->dv->y; - object->inner_points[i]->dv->z = temp_inner_points4[i]->dv->z; - - object->inner_points[i]->dr->x = temp_inner_points4[i]->dr->x; // Change in position is velocity times the change in time - object->inner_points[i]->dr->y = temp_inner_points4[i]->dr->y; - object->inner_points[i]->dr->z = temp_inner_points4[i]->dr->z; - - - object->inner_points[i]->v->x = temp_inner_points0[i]->v->x + 1/6*temp_inner_points1[i]->v->x + 1/3*temp_inner_points2[i]->v->x + 1/3*temp_inner_points3[i]->v->x + 1/6*temp_inner_points4[i]->v->x; // Change in velocity is added to the velocity. - object->inner_points[i]->v->y = temp_inner_points0[i]->v->y + 1/6*temp_inner_points1[i]->v->y + 1/3*temp_inner_points2[i]->v->y + 1/3*temp_inner_points3[i]->v->y + 1/6*temp_inner_points4[i]->v->y; // Change in velocity is added to the velocity. - object->inner_points[i]->v->z = temp_inner_points0[i]->v->z + 1/6*temp_inner_points1[i]->v->z + 1/3*temp_inner_points2[i]->v->z + 1/3*temp_inner_points3[i]->v->z + 1/6*temp_inner_points4[i]->v->z; // Change in velocity is added to the velocity. - - object->inner_points[i]->r->x = temp_inner_points0[i]->r->x + 1/6*temp_inner_points1[i]->r->x + 1/3*temp_inner_points2[i]->r->x + 1/3*temp_inner_points3[i]->r->x + 1/6*temp_inner_points4[i]->r->x; // Change in velocity is added to the velocity. // Change in position is velocity times the change in time - object->inner_points[i]->r->y = temp_inner_points0[i]->r->y + 1/6*temp_inner_points1[i]->r->y + 1/3*temp_inner_points2[i]->r->y + 1/3*temp_inner_points3[i]->r->y + 1/6*temp_inner_points4[i]->r->y; // Change in velocity is added to the velocity. // Change in position is velocity times the change in time - object->inner_points[i]->r->z = temp_inner_points0[i]->r->z + 1/6*temp_inner_points1[i]->r->z + 1/3*temp_inner_points2[i]->r->z + 1/3*temp_inner_points3[i]->r->z + 1/6*temp_inner_points4[i]->r->z; // Change in velocity is added to the velocity. // Change in position is velocity times the change in time - - - object->outer_points[i]->dv->x = temp_outer_points4[i]->dv->x; - object->outer_points[i]->dv->y = temp_outer_points4[i]->dv->y; - object->outer_points[i]->dv->z = temp_outer_points4[i]->dv->z; - - object->outer_points[i]->dr->x = temp_outer_points4[i]->dr->x; // Change in position is velocity times the change in time - object->outer_points[i]->dr->y = temp_outer_points4[i]->dr->y; - object->outer_points[i]->dr->z = temp_outer_points4[i]->dr->z; - - object->outer_points[i]->v->x = temp_outer_points0[i]->v->x + 1/6*temp_outer_points1[i]->v->x + 1/3*temp_outer_points2[i]->v->x + 1/3*temp_outer_points3[i]->v->x + 1/6*temp_outer_points4[i]->v->x; // Change in velocity is added to the velocity. - object->outer_points[i]->v->y = temp_outer_points0[i]->v->y + 1/6*temp_outer_points1[i]->v->y + 1/3*temp_outer_points2[i]->v->y + 1/3*temp_outer_points3[i]->v->y + 1/6*temp_outer_points4[i]->v->y; // Change in velocity is added to the velocity. - object->outer_points[i]->v->z = temp_outer_points0[i]->v->z + 1/6*temp_outer_points1[i]->v->z + 1/3*temp_outer_points2[i]->v->z + 1/3*temp_outer_points3[i]->v->z + 1/6*temp_outer_points4[i]->v->z; // Change in velocity is added to the velocity. - - object->outer_points[i]->r->x = temp_outer_points0[i]->r->x + 1/6*temp_outer_points1[i]->r->x + 1/3*temp_outer_points2[i]->r->x + 1/3*temp_outer_points3[i]->r->x + 1/6*temp_outer_points4[i]->r->x; // Change in velocity is added to the velocity. // Change in position is velocity times the change in time - object->outer_points[i]->r->y = temp_outer_points0[i]->r->y + 1/6*temp_outer_points1[i]->r->y + 1/3*temp_outer_points2[i]->r->y + 1/3*temp_outer_points3[i]->r->y + 1/6*temp_outer_points4[i]->r->y; // Change in velocity is added to the velocity. // Change in position is velocity times the change in time - object->outer_points[i]->r->z = temp_outer_points0[i]->r->z + 1/6*temp_outer_points1[i]->r->z + 1/3*temp_outer_points2[i]->r->z + 1/3*temp_outer_points3[i]->r->z + 1/6*temp_outer_points4[i]->r->z; // Change in velocity is added to the velocity. // Change in position is velocity times the change in time - -} diff --git a/src/SoftBodySimulation/simulation/Simulation.cpp b/src/SoftBodySimulation/simulation/Simulation.cpp deleted file mode 100644 index 28c2faa..0000000 --- a/src/SoftBodySimulation/simulation/Simulation.cpp +++ /dev/null @@ -1,241 +0,0 @@ - -/******************************************************************************** - * * - * A demo 3D ball of multi-layers with deformation under non-uniform factors. * - * The deformation reaction under those non-distributed influences is well * - * observed * - * * - * Version V1.01 * - * Copyright by Miao Song * - * * - ********************************************************************************/ - - - - -//#include -//using namespace std; - -#include "Simulation.h" - -// Mouse variables -int mousedown =0; // for GLUT_LEFT_BUTTON, GLUT_RIGHT_BUTTON -float xMouse, yMouse, zMouse; // for mouse point r(x,y,z) -int closest_i, closest_j; // Closest point index i, j - -// Keyboard variables -int GoDir; // for GLUT_KEY_LEFT,GLUT_KEY_RIGHT,GLUT_KEY_UP,GLUT_KEY_DOWN -float RotateRegX=0; // for glRotatef() rotating direction -float RotateRegY=0; // for glRotatef() rotating direction -float RotateRegZ=0; // for glRotatef() rotating direction - - -int main_window; -int wireframe = 0; -int segments = 8; - - -Object1D object1D; // initial the object1D -Object2D object2D; // initial the object2D -Object3D object3D; // initial the object3D - - -ViewSpace box; // Creat a viewer space - -//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ - -void Reshape(int width, int height) -{ - glViewport(0, 0, width, height); // Define GL View Port rectangle - glMatrixMode(GL_PROJECTION); - gluPerspective(20.,float(Width)/float(Height), 1., 100.); - glLoadIdentity(); - glMatrixMode(GL_MODELVIEW); - glutPostRedisplay(); -} - -void Display(void) -{ - glClearColor(0.0, 0.0, 0.0, 0.0); // Initialize - // glClearColor(.8, 0.8, 0.8, 0.0); // Initialize glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); - - glMatrixMode(GL_PROJECTION); - glLoadIdentity(); - -///* gluPerspective(50,float(Width)/float(Height), 1, 100.); - glMatrixMode(GL_MODELVIEW); - glLoadIdentity(); - // gluLookAt(2,0,10,1,-1,6,0,1,0); // Camera difinition gluLookAt(2,0,10,1,-1,6,0,1,0); // Camera difinition -//*//* gluPerspective(40,float(Width)/float(Height), 1, 100.); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); gluLookAt(2,0,7,1,0,5,0,1,0); // Camera difinition*/ - glPointSize(8); - -//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - box.Draw(); // Draw the view box space - -// object1D.Draw(); - - Rotated(); -// object2D.Draw(); - object3D.Draw(); - glutSwapBuffers(); -} - -//=================================================================================== - - -// when mouse is clicked -void Mouse(int button, int state, int x, int y) -{ - - xMouse = (4 * ((float)x/(float)Width)) -2 ; - yMouse = -((4 * ( (float)y/(float)Height))-2) ; - - if (state == GLUT_DOWN) - { - mousedown = 1; - } - else if (state == GLUT_UP) - { - mousedown = 0; - } -} - -// when mouse is moving -void Motion(int x, int y) -{ - xMouse = (4 * ((float)x/(float)Width)) -2 ; - yMouse = -((4 * ( (float)y/(float)Height))-2 ); -} - - - -// when a rotation Key is pressed - -void Keyboard(unsigned char key, int x, int y) -{ -// ThreeDInner.Keyboard(key, x, y); - - cout << "key = " << key << endl; - - switch(key){ - case 'x': - case 'X': - RotateRegX+=10; - glutPostRedisplay(); - break; - - case 'y': - case 'Y': - RotateRegY+=10; - glutPostRedisplay(); - break; - - case 'z': - case 'Z': - RotateRegZ+=10; - glutPostRedisplay(); - break; - - case 'r': - case 'R': - RotateRegX+=5; - RotateRegY+=5; - RotateRegZ+=5; - glutPostRedisplay(); - break; - - default: - break; // do nothing - } -} - -void Rotated(void) -{ -// glPushMatrix(); - glRotated(RotateRegX, 1.0, 0.0, 0.0); // Rotate 90 about X-axis - glRotated(RotateRegY, 0.0, 1.0, 0.0); // Rotate 90 about X-axis - glRotated(RotateRegZ, 0.0, 0.0, 1.0); // Rotate 90 about X-axis -// glPopMatrix(); -} - -//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ - -// when a moving direction Key is pressed -void SpecialKeys(int key, int x, int y) -{ -// ThreeDInner.SpecialKeys(key, x, y); - switch (key){ - case GLUT_KEY_UP: - GoDir = 1; - break; - case GLUT_KEY_DOWN: - GoDir = 2; - break; - case GLUT_KEY_LEFT: - GoDir = 3; - break; - case GLUT_KEY_RIGHT: - GoDir = 4; - break; - default: - GoDir = 0; - break; - } -} - - -void Idle() -{ - object1D.Update(DT, mousedown != 0, xMouse, yMouse); - object2D.Update(DT, mousedown != 0, xMouse, yMouse); - object3D.Update(DT, mousedown != 0, xMouse, yMouse); - glutPostRedisplay(); -} - -/*void motion (int mx, int my) -{ -// Normalize mouse coordinates. - xMouse = double(mx) ; - yMouse = double(my) ; - glutPostRedisplay(); -} -*/ -//====================================================================================== -int main(void) -{ - glutInitWindowPosition(200, 200); - glutInitWindowSize( Width, Height); - glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA); - -// glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA); - main_window = glutCreateWindow("A Simulation Ball - Miao Song"); - - glutReshapeFunc(Reshape); - - glEnable(GL_BLEND); // transparent - glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA); // transparent - glEnable(GL_DEPTH_TEST); - -// glutPassiveMotionFunc(motion); - - glutMouseFunc(Mouse); - glutMotionFunc(Motion); - glutKeyboardFunc(Keyboard); - glutSpecialFunc(SpecialKeys); - - glutDisplayFunc(Display); - glutIdleFunc(Idle); - /* - GLUI *glui = GLUI_Master.create_glui( "GLUI" ); - - new GLUI_Checkbox( glui, "Wireframe", &wireframe ); - (new GLUI_Spinner( glui, "Segments:", &segments )) - ->set_int_limits( 3, 60 ); - - glui->set_main_gfx_window( main_window ); - GLUI_Master.set_glutIdleFunc( Idle ); - */ - glutMainLoop(); - return 0; -} diff --git a/src/SoftBodySimulation/simulation/ViewSpace.cpp b/src/SoftBodySimulation/simulation/ViewSpace.cpp deleted file mode 100644 index 59088c4..0000000 --- a/src/SoftBodySimulation/simulation/ViewSpace.cpp +++ /dev/null @@ -1,73 +0,0 @@ -#include "ViewSpace.h" -#include "global.h" - - - -//##ModelId=45F4D79602F5 -void ViewSpace::Draw(void) // Draw the view space -{ - int j; - - glPushMatrix(); - glBegin(GL_QUADS); - - // floor - j = 0; - { - glColor4f(1, 0.5, 0.1, 0.5); - glNormal3f(0, 1, 0); - glVertex3f(spaceVertex[j]->x, spaceVertex[j]->y, spaceVertex[j]->z); - glVertex3f(spaceVertex[j+1]->x, spaceVertex[j+1]->y, spaceVertex[j+1]->z); - glVertex3f(spaceVertex[j+2]->x, spaceVertex[j+2]->y, spaceVertex[j+2]->z); - glVertex3f(spaceVertex[j+3]->x, spaceVertex[j+3]->y, spaceVertex[j+3]->z); - } - - // ceiling - j += 4; - { - glColor4f(1, 0.5, 0.1, 0.5); - glNormal3f(0, -1, 0); - glVertex3f(spaceVertex[j]->x, spaceVertex[j]->y, spaceVertex[j]->z); - glVertex3f(spaceVertex[j+1]->x, spaceVertex[j+1]->y, spaceVertex[j+1]->z); - glVertex3f(spaceVertex[j+2]->x, spaceVertex[j+2]->y, spaceVertex[j+2]->z); - glVertex3f(spaceVertex[j+3]->x, spaceVertex[j+3]->y, spaceVertex[j+3]->z); - } - - // back wall - j = 1; - { - glColor4f(0, 0, 1, 0.5); - glNormal3f(0, 0, 1); - glVertex3f(spaceVertex[j]->x, spaceVertex[j]->y, spaceVertex[j]->z); - glVertex3f(spaceVertex[j+1]->x, spaceVertex[j+1]->y, spaceVertex[j+1]->z); - glVertex3f(spaceVertex[j+5]->x, spaceVertex[j+5]->y, spaceVertex[j+5]->z); - glVertex3f(spaceVertex[j+4]->x, spaceVertex[j+4]->y, spaceVertex[j+4]->z); - } - - // right wall - j = 0; - { - glColor4f(0.5, 0.9, 0.1, 0.5); - glNormal3f(-1, 0, 0); - glVertex3f(spaceVertex[j]->x, spaceVertex[j]->y, spaceVertex[j]->z); - glVertex3f(spaceVertex[j+1]->x, spaceVertex[j+1]->y, spaceVertex[j+1]->z); - glVertex3f(spaceVertex[j+5]->x, spaceVertex[j+5]->y, spaceVertex[j+5]->z); - glVertex3f(spaceVertex[j+4]->x, spaceVertex[j+4]->y, spaceVertex[j+4]->z); - } - - // left wall - j += 2; - { - glNormal3f(1, 0, 0); - glColor4f(0.5, 0.9, 0.1, 0.5); - glVertex3f(spaceVertex[j]->x, spaceVertex[j]->y, spaceVertex[j]->z); - glVertex3f(spaceVertex[j+1]->x, spaceVertex[j+1]->y, spaceVertex[j+1]->z); - glVertex3f(spaceVertex[j+5]->x, spaceVertex[j+5]->y, spaceVertex[j+5]->z); - glVertex3f(spaceVertex[j+4]->x, spaceVertex[j+4]->y, spaceVertex[j+4]->z); - } - - glEnd(); - - glPopMatrix(); - -}