rt300@0: /* rt300@0: * mesh.h rt300@0: * springstructure rt300@0: * rt300@0: * Created by Robert Tubb on 07/06/2011. rt300@0: * Copyright 2011 __MyCompanyName__. All rights reserved. rt300@0: * rt300@0: */ rt300@0: rt300@0: #ifndef _MESHH rt300@0: #define _MESHH rt300@0: rt300@0: #include rt300@0: #include "lump.h" rt300@0: #include "scanpath.h" rt300@0: #include "globalForces.h" rt300@8: #include "json.h" rt300@0: rt300@0: class Mesh{ rt300@0: public: rt300@8: int dim1, dim2; rt300@8: rt300@0: bool radial; // tells us if mesh is radial or not rt300@0: // toggles rt300@0: bool syrup; rt300@0: bool home; rt300@0: rt300@0: double propagationSpeed; rt300@0: rt300@0: double k,m,f; // for stability check and asym rt300@0: rt300@0: int numLumps; rt300@0: int numSprings; rt300@0: rt300@4: //Lump *lumps; rt300@4: vector lumps; rt300@3: vector springs; rt300@3: //Spring *springs; rt300@0: rt300@0: TwoVector centre; rt300@0: rt300@0: rt300@0: // modes rt300@0: enum constrainMode { CONSTRAIN_CORNERS, CONSTRAIN_EDGES,CONSTRAIN_EDGES_XY, CONSTRAIN_SINGLE_POINT, CONSTRAIN_SINGLE_POINT_X, CONSTRAIN_SINGLE_POINT_Y, CONSTRAIN_GRAB_REGION,CONSTRAIN_GRAB_REGION_X,CONSTRAIN_GRAB_REGION_Y, CONSTRAIN_ALL_X, CONSTRAIN_ALL_Y}; rt300@0: double GRAB_RANGE; rt300@0: rt300@9: enum MeshType {SQUARE_MESH, SQUARE_CROSS_MESH, SPIDER_MESH, SPIDER_CROSS_MESH, DROPLET_MESH, LINE_MESH, TRIANGLE_MESH, GROUNDED_LINE_MESH, HOLED_SPIDER_MESH}; rt300@8: MeshType meshType; rt300@0: rt300@0: // MEMBER FUNCTIONS rt300@0: Mesh(); rt300@9: Mesh(Json::Value& savedMesh); // contructor using saved mesh rt300@10: void setReasonableDefaults(); rt300@0: virtual ~Mesh(); rt300@0: rt300@0: void draw(); rt300@0: rt300@0: // INTERACTIONS rt300@0: void resetAll(); rt300@0: void resetPositions(); rt300@0: void resetVelocities(); rt300@0: void resetEverything(); rt300@0: rt300@0: // excite via position, velocity force rt300@0: // using shapes : noise, hamming, sine, rt300@0: void hit(double dax,double day,int velocity = 100, GlobalForces::excitationTypes aEType = GlobalForces::POSITION, rt300@0: GlobalForces::excitationShapes aEShape = GlobalForces::NOISE); rt300@0: void spatialHarmonic(int aharm1 = 1, int aharm2 = 0); rt300@0: void damp(); rt300@0: void forceField(double dax,double day,double strength); rt300@0: void averagingFilter(double amt); rt300@0: rt300@0: void grab(double x, double y, int touchID); rt300@0: void drag(double ax,double ay, int touchID); rt300@0: void unGrab(int touchID); rt300@0: rt300@0: virtual void constrain(double x, double y, constrainMode aMode); // diff meshes will have diff edges rt300@0: void unconstrain(); rt300@0: void unconstrain(double x, double y, constrainMode aMode); rt300@0: rt300@0: rt300@0: // PARAMETER SETTINGS rt300@0: void toggleSyrup(); rt300@0: void toggleSyrup(bool on); rt300@0: void toggleSpringForces(); rt300@0: void toggleSpringForces(bool on); rt300@0: void toggleGravity(); rt300@0: void toggleGravity(bool on); rt300@0: void toggleHome(bool on); rt300@0: rt300@0: void setRestLength(); rt300@0: void zeroRestLength(); rt300@0: rt300@0: void setPropagationSpeed(double aSpeed); // sets k and m to give a certain speed rt300@0: void decreasePropagationSpeed(); rt300@0: void increasePropagationSpeed(); rt300@0: void setSpringConstant(double aK); rt300@0: void setMass(double aM); rt300@0: void setFriction(double aF); rt300@0: rt300@0: // create varying constants rt300@0: void setSpringConstantAsym(double aAsym); rt300@0: void setMassAsym(double aAsym); rt300@0: void setFrictionAsym(double aAsym); rt300@0: void setZeroAudioLine(); rt300@0: rt300@0: // only for mouse cursor rt300@0: void checkHover(double x, double y); rt300@0: rt300@0: // SCANPATH STUFF rt300@0: void clearScanPath(); rt300@0: void disconnectDraw(); // enables you to draw scanpths anywhere rt300@0: int inscribeScanPath(double ax, double ay); rt300@8: rt300@8: // inscribeScanPathFromJson rt300@8: rt300@0: // called by scanpath.update wavetables rt300@0: double getNextSample(); rt300@0: rt300@0: virtual void update(); rt300@8: rt300@8: // save stuff rt300@8: Json::Value convertToJsonForSaving(); rt300@9: Json::Value saveConnectionsAsJson(); rt300@10: // UTILS rt300@10: TwoVector calculateCentre(); rt300@10: void connect(int springnum,int lumpnum); rt300@10: void connect(int springnum,int lumpnum,int alumpnum2); rt300@10: int getNearestLump(double ax,double ay); rt300@0: protected: rt300@0: rt300@0: int prevLump; rt300@0: rt300@0: // specific mesh shapes override these: rt300@0: rt300@0: virtual void makeComponents(int adimension1, int adimension2); rt300@0: virtual void setLumpPositions(); rt300@0: virtual void makeConnections(); rt300@0: virtual void makeDefaultScanPath(); rt300@9: rt300@9: // alternatively get from saved: rt300@9: rt300@13: void setLumpPositionsFromJson(Json::Value lumpPositions, Json::Value lumpRestPositions); rt300@9: void makeConnectionsFromJson(Json::Value connections); rt300@9: void makeScanPathFromJson(Json::Value scanPathElements); rt300@9: void constrainFromJson(Json::Value constrainedLumps); rt300@10: rt300@0: rt300@0: }; rt300@0: //--------------------------------------------------------- rt300@0: class SpiderMesh : public Mesh{ rt300@0: public: rt300@0: int numSpokes, numRings; rt300@0: rt300@0: SpiderMesh(int aNumSpokes,int aNumRings); rt300@0: rt300@0: virtual void constrain(double x, double y, constrainMode aMode); rt300@0: virtual void makeComponents(int adimension1, int adimension2); rt300@0: virtual void setLumpPositions(); rt300@0: virtual void makeConnections(); rt300@0: virtual void makeDefaultScanPath(); rt300@0: }; rt300@0: //--------------------------------------------------------- rt300@0: class HoledSpiderMesh : public Mesh{ rt300@0: public: rt300@0: int numSpokes, numRings; rt300@0: rt300@0: HoledSpiderMesh(int aNumSpokes,int aNumRings); rt300@0: void constrain(double x, double y, constrainMode aMode); rt300@0: void makeComponents(int adimension1, int adimension2); rt300@0: void setLumpPositions(); rt300@0: void makeConnections(); rt300@0: void makeDefaultScanPath(); rt300@0: }; rt300@0: //--------------------------------------------------------- rt300@0: class SpiderCrossMesh : public Mesh{ rt300@0: public: rt300@0: int numSpokes, numRings; rt300@0: rt300@0: SpiderCrossMesh(int aNumSpokes,int aNumRings); rt300@0: rt300@0: void constrain(double x, double y, constrainMode aMode); rt300@0: void makeComponents(int adimension1, int adimension2); // override rt300@0: void setLumpPositions(); // same rt300@0: void makeConnections(); // overrride rt300@0: void makeDefaultScanPath(); // same rt300@0: }; rt300@0: //--------------------------------------------------------- rt300@9: // square grid rt300@9: class SquareMesh : public Mesh{ rt300@0: public: rt300@0: // square specific rt300@0: int height; rt300@0: int width; rt300@9: SquareMesh(); rt300@9: SquareMesh(int height, int width); rt300@9: ~SquareMesh(); rt300@9: void constrain(double x, double y, constrainMode aMode); rt300@9: void makeComponents(int adimension1, int adimension2); rt300@9: void setLumpPositions(); rt300@9: void makeConnections(); rt300@9: void makeDefaultScanPath(); rt300@9: }; rt300@9: //--------------------------------------------------------- rt300@9: // square grid with all diagonals connected i.e. we override makeConnections rt300@9: class SquareCrossMesh : public SquareMesh{ rt300@9: public: rt300@9: SquareCrossMesh(){}; rt300@9: SquareCrossMesh(int height, int width); rt300@0: ~SquareCrossMesh(); rt300@0: void makeConnections(); rt300@9: rt300@0: }; rt300@0: //--------------------------------------------------------- rt300@0: // simple 1d line rt300@0: class LineMesh : public Mesh{ rt300@0: public: rt300@0: // square specific rt300@0: int numSegments; rt300@0: rt300@0: LineMesh(int aNumSegments); rt300@0: void constrain(double x, double y, constrainMode aMode); rt300@0: void makeComponents(int adimension1, int adimension2); rt300@0: void setLumpPositions(); rt300@0: void makeConnections(); rt300@0: void makeDefaultScanPath(); rt300@0: }; rt300@0: //--------------------------------------------------------- rt300@0: // simple 1d line attached to 'ground' (constrained lumps) by other springs... rt300@0: class GroundedLineMesh : public Mesh{ rt300@0: public: rt300@0: int numSegments; rt300@0: rt300@0: GroundedLineMesh(int aNumSegments); rt300@0: void constrain(double x, double y, constrainMode aMode); rt300@0: void makeComponents(int adimension1, int adimension2); rt300@0: void setLumpPositions(); rt300@0: void makeConnections(); rt300@0: void makeDefaultScanPath(); rt300@0: }; rt300@0: rt300@0: //---------------------------------------------------------- rt300@0: //--------------------------------------------------------- rt300@0: // circular spring with internal pressure rt300@0: class DropletMesh : public Mesh{ rt300@0: public: rt300@0: // specific rt300@0: int numSegments; rt300@0: double restArea; rt300@0: rt300@0: DropletMesh(int aNumSegments); rt300@0: void constrain(double x, double y, constrainMode aMode); rt300@0: void makeComponents(int adimension1, int adimension2); rt300@0: void setLumpPositions(); rt300@0: void makeConnections(); rt300@0: void makeDefaultScanPath(); rt300@0: double calculatePressure(); rt300@0: double calculateArea(); rt300@0: void update(); rt300@0: }; rt300@0: //--------------------------------------------------------- rt300@0: // trangular mesh rt300@0: class TriangleMesh : public Mesh{ rt300@0: public: rt300@0: // square specific rt300@0: int height; rt300@0: int width; rt300@0: rt300@0: TriangleMesh(int height, int width); rt300@0: void constrain(double x, double y, constrainMode aMode); rt300@0: void makeComponents(int adimension1, int adimension2); rt300@0: void setLumpPositions(); rt300@0: void makeConnections(); rt300@0: void makeDefaultScanPath(); rt300@0: }; rt300@0: //---------------------------------------------------------- rt300@0: rt300@0: #endif