Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Continued porting to 2D

  • Loading branch information...
commit d430c752fdcccd0c6d80ae7c4f64a14b2bbdee20 1 parent 12c1487
ANerd authored
View
17 core/Block.h
@@ -23,4 +23,21 @@ struct Block
}
};
+inline uint CoordX(uint index, const ConfigurationData *config)
+{
+ return index % config->Columns;
+}
+
+inline uint CoordY(uint index, const ConfigurationData *config)
+{
+ return index / config->Columns;
+}
+
+inline uint Index(uint coordY, uint coordX, const ConfigurationData *config)
+{
+ if(coordX >= config->Columns || coordY >= config->Rows)
+ return uint_max;
+ return coordY * config->Columns + coordX;
+}
+
#endif
View
28 core/Engine.h
@@ -2,23 +2,16 @@
#ifndef ENGINE_H_
#define ENGINE_H_
-#include <limits.h>
#include <string>
-#include <math.h>
#include <stdlib.h>
#include <stdio.h>
+#include "Types.h"
+#include "Vector.h"
using namespace std;
#ifdef DEBUG_MEM
#include <mcheck.h>
#endif
-typedef double real;
-typedef unsigned int uint;
-typedef unsigned char byte;
-
-#define REAL_HUGE HUGE_VAL
-#define REAL_MAX DBL_MAX
-
#include "Debug.h"
const uint DataTypeCount = 6;
@@ -54,21 +47,4 @@ typedef Vector DTT5;
#define DTExternalForce_t DTT4
#define DTAcceleration_t DTT5
-inline uint CoordX(uint index, ConfigurationData *config)
-{
- return index % config->Columns;
-}
-
-inline uint CoordY(uint index, ConfigurationData *config)
-{
- return index / config->Columns;
-}
-
-inline uint Index(uint coordY, uint coordX, ConfigurationData *config)
-{
- if(coordX >= config->Columns, coordY >= config->Rows)
- return uint_max;
- return coordY * config->Columns + coordX;
-}
-
#endif
View
19 core/ForceCalculator.cpp
@@ -28,34 +28,31 @@ void ForceCalculator::Prepare(uint frameNumber, real time)
void ForceCalculator::Run(uint i, uint n)
{
- uint x = CoordX(i, Sim->Config);
- uint y = CoordY(i, Sim->Config);
+ uint x = CoordX(i, mySimulation->Config);
+ uint y = CoordY(i, mySimulation->Config);
BlockEnvironment env;
for(int j = -1; j < 2; j++)
{
for(int i = -1; i < 2; i++)
{
- uint ind = Index(x+i, y+j, Sim->Config);
+ uint ind = Index(x+i, y+j, mySimulation->Config);
+ Block b;
if(ind != uint_max)
{
- Block b;
b.Row = x + j;
b.Column = x + i;
b.Velocity = myVelocities[ind];
b.Displacement = myPositions[ind];
- env.Set(i, j, b);
- }
- else
- {
- env.Set(i, j, Block());
}
+ env.Set(i, j, b);
}
}
- myNetForces[i] = 0;
+ myNetForces[i] = Vector();
for(uint f = 0; f < myForceGeneratorLength; f++)
{
- myNetForces[i] += myForceGenerators[f]->GetForce(env);
+ DTForces_t newForce = myForceGenerators[f]->GetForce(env);
+ Vector::Add(myNetForces + i, &newForce, myNetForces + i);
}
}
View
4 core/Vector.h
@@ -1,7 +1,7 @@
#ifndef VECTOR_H_
#define VECTOR_H_
-#include "Engine.h"
+#include "Types.h"
class Vector
{
@@ -22,7 +22,7 @@ class Vector
Vector operator+ (Vector &b);
Vector operator- (Vector &b);
real operator* (Vector &b);
- Vector operator* (real &s);
+ Vector operator* (real s);
};
View
7 generators/DampingForceGenerator.cpp
@@ -7,7 +7,10 @@ DampingForceGenerator::DampingForceGenerator(Simulation *simulation)
{
}
-real DampingForceGenerator::GetForce(BlockEnvironment &env)
+DTForces_t DampingForceGenerator::GetForce(BlockEnvironment &env)
{
- return -env.C.Velocity * Sim->Config->Damping * Sim->Config->MassPerBlock;
+ Block *current = env.Get(0,0);
+ DTForces_t force = current->Velocity * Sim->Config->Damping * Sim->Config->MassPerBlock;
+ Vector::Invert(&force, &force);
+ return force;
}
View
2  generators/DampingForceGenerator.h
@@ -8,7 +8,7 @@ class DampingForceGenerator : public ForceGenerator
public:
DampingForceGenerator(Simulation *simulation);
- virtual real GetForce(BlockEnvironment &env);
+ virtual DTForces_t GetForce(BlockEnvironment &env);
};
#endif
View
5 generators/ExternalForceGenerator.cpp
@@ -25,9 +25,10 @@ void ExternalForceGenerator::Prepare(uint frameNumber, real time)
myExternalForces = Sim->Set<DTExternalForce_t>(DTExternalForce);
}
-real ExternalForceGenerator::GetForce(BlockEnvironment &env)
+DTForces_t ExternalForceGenerator::GetForce(BlockEnvironment &env)
{
- uint index = Index(env.C.Row, env.C.Column);
+ Block *current = env.Get(0,0);
+ uint index = Index(current->Row, current->Column, Sim->Config);
if(nodeActivity[index] == 0)
return 0;
View
2  generators/ExternalForceGenerator.h
@@ -11,7 +11,7 @@ class ExternalForceGenerator : public ForceGenerator
public:
ExternalForceGenerator(Simulation *sim);
virtual void Prepare(uint frameNumber, real time);
- virtual real GetForce(BlockEnvironment &env);
+ virtual DTForces_t GetForce(BlockEnvironment &env);
};
#endif
View
22 generators/ForceGenerator.h
@@ -4,24 +4,18 @@
#include "Block.h"
#include "Simulation.h"
-union BlockEnvironment
+struct BlockEnvironment
{
- struct {
- Block NW;
- Block N;
- Block NE;
- Block W;
- Block C;
- Block E;
- Block SW;
- Block S;
- Block SE;
- };
Block Blocks[9];
- void Put(uint i, uint j, Block &value) {
+ void Set(int i, int j, Block &value) {
Blocks[i + 1 + (j + 1) * 3] = value;
}
+
+ Block *Get(int i, int j)
+ {
+ return Blocks + (i + 1 + (j + 1) * 3);
+ }
};
class ForceGenerator
@@ -37,7 +31,7 @@ class ForceGenerator
ForceGenerator(Simulation *simulation);
void Pre(uint frameNumber, real time);
- virtual real GetForce(BlockEnvironment &environment) = 0;
+ virtual DTForces_t GetForce(BlockEnvironment &environment) = 0;
};
#endif
View
15 generators/FrictionForceGenerator.cpp
@@ -44,13 +44,14 @@ void FrictionForceGenerator::Prepare(uint frameNumber, real time)
myBreakage = Sim->Set<DTBreakage_t>(DTBreakage);
}
-real FrictionForceGenerator::GetForce(const Block &block, const Block &previous, const Block &next)
+DTForces_t FrictionForceGenerator::GetForce(BlockEnvironment &env)
{
- real totalForce = 0;
+ Block *currentBlock = env.Get(0, 0);
+ DTForces_t totalForce = 0;
uint localBreakage = 0;
for (uint i = 0; i < mySpringsPerBlock; i++)
{
- FrictionSpringInfo *info = mySpringData + mySpringIndex(block.Index, i); &mySpringData[mySpringIndex(block.Index, i)];
+ FrictionSpringInfo *info = mySpringData + mySpringIndex(block.Index, i);
if(info->Anchor == -REAL_HUGE)
{
if(info->AttachTime > Time) {
@@ -66,11 +67,13 @@ real FrictionForceGenerator::GetForce(const Block &block, const Block &previous,
#endif
info->BreakForce = myRandom.GetNormalizedRandom();
info->SpringConstant = Sim->Config->FrictionSpringConstant * sqrt(info->BreakForce/Sim->Config->FrictionSpringBreakage);
- info->Anchor = block.Displacement;
+ info->Anchor = currentBlock->Displacement;
}
- real force = (info->Anchor - block.Displacement) * info->SpringConstant;
+ DTForces_t force = (info->Anchor - block.Displacement) * info->SpringConstant;
//DebugPF(DNote, "Anchor: %g, Disp: %g, Const: %g, Friction force is: %g", info->Anchor, block.Displacement, info->SpringConstant, totalForce);
- if(std::max(force, -force) > info->BreakForce)
+ real magnitude;
+ Vector::Magnitude(&force, &magnitude);
+ if(magnitude > info->BreakForce)
{
#ifdef DEBUG
SpringsBroken++;
View
4 generators/FrictionForceGenerator.h
@@ -7,7 +7,7 @@
struct FrictionSpringInfo
{
- real Anchor;
+ Vector Anchor;
union
{
struct
@@ -30,7 +30,7 @@ class FrictionForceGenerator : public ForceGenerator
FrictionForceGenerator(Simulation *sim, RandomGenerator *random);
virtual ~FrictionForceGenerator();
virtual void Prepare(uint frameNumber, real time);
- virtual real GetForce(const Block &block, const Block &previous, const Block &next);
+ virtual DTForces_t GetForce(BlockEnvironment &env);
};
View
18 generators/SystemForceGenerator.cpp
@@ -12,19 +12,29 @@ inline uint envIndex(uint i, uint j)
return i + 1 + (j + 1) * 3
}
-real SystemForceGenerator::GetForce(BlockEnvironment &env)
+DTForces_t SystemForceGenerator::GetForce(BlockEnvironment &env)
{
- real force[9];
+ DTPosition_t *thisPosition = env.Get(0,0);
+ DTForces_t totalForce;
for(int j = -1; j < 2; j++)
{
for(int i = -1; i < 2; i++)
{
- if(i*j == 0)
+ Block *currentBlock = env.Get(i, j);
+ if(currentBlock->Valid())
{
- force[envIndex(i, j)];
+ DTForces_t currentForce;
+ if(i * j == 0)
+ {
+ DTForces_t::Subtract(env.Get(i, j)->Displacement, thisPosition, &currentForce) * i == 0 ? j : i;
+ DTForces_t::Add(&totalForce, &currentForce, &totalForce);
+ }
}
}
}
+
+ return totalForce;
+
/*
real forcePrev = 0;
real forceNext = 0;
View
2  generators/SystemForceGenerator.h
@@ -9,7 +9,7 @@ class SystemForceGenerator : public ForceGenerator
{
public:
SystemForceGenerator(Simulation *sim);
- virtual real GetForce(BlockEnvironment &env);
+ virtual DTForces_t GetForce(BlockEnvironment &env);
};
#endif
Please sign in to comment.
Something went wrong with that request. Please try again.