diff --git a/src/constants.h b/src/constants.h index fd9a32c..4d9c294 100755 --- a/src/constants.h +++ b/src/constants.h @@ -25,7 +25,7 @@ const static float DT = 0.0005f; // Time-step // Material // #define Material Elastic // [Water] - [DrySand] - [Snow] - [Elastic] -#define Material Snow +#define Material Water /* ----------------------------------------------------------------------- | CONSTANTS diff --git a/src/main b/src/main index 3b97a35..fc2f4e2 100755 Binary files a/src/main and b/src/main differ diff --git a/src/particle.h b/src/particle.h index 367b758..de659ba 100755 --- a/src/particle.h +++ b/src/particle.h @@ -27,6 +27,56 @@ class Particle { Particle(const float inVp0, const float inMp, const Vector2f &inXp, const Vector2f &inVp, const Matrix2f &inBp); ~Particle(){}; + + /* Static function */ + static int myRand(int down, int up) { + return (rand() % (up - down - 1) + down); + } + + // Generate point positions from image + static std::vector ptsFromImg(std::string name, Vector2f trans, + Vector2f scale) { + std::vector ps; + cv::Mat inImg = cv::imread(name); + + int rndSize = 100; // resolution along each axis + int imgWidth = inImg.size().width; + int imgHeight = inImg.size().height; + + for (int i = 0; i < rndSize; i++) { + for (int j = 0; j < rndSize; j++) { + int x, y; + x = myRand(0, imgWidth - 1); + y = myRand(0, imgHeight - 1); + + cv::Vec3b pixelSrc = inImg.at(cv::Point(x, y)); + + int color = pixelSrc[0] + pixelSrc[1] + pixelSrc[2]; + // some threshold + if (color < 10) { + Vector2f pos; + + // rescale + float fx, fy; + fx = (float)x / (float)imgWidth; // to [0, 1.0] + fy = (float)y / (float)imgHeight; + fx *= scale[0]; + fy *= scale[0]; + + // translate + fx += trans[0]; + fy += trans[1]; + + // back to world space + pos = Vector2f(fx * (float)X_GRID, fy * (float)Y_GRID); + + ps.push_back(pos); + } + } // end inner for + } // end outer for + + return ps; + } // end ptsFromImg }; /* ---------------------------------------------------------------------------------------------- @@ -62,6 +112,17 @@ class Water : public Particle { /* Static Functions */ static std::vector InitializeParticles() { std::vector outParticles; + Vector2f v = Vector2f(30, 0); // Initial velocity + Matrix2f a = Matrix2f(0); + + // get position from image + std::vector heart = + ptsFromImg("heart.png", Vector2f(0.25f, 0.45f), Vector2f(0.5f, 0.5f)); + + for (int i = 0; i < heart.size(); i++) { + outParticles.push_back(Water(1.14f, 0.0005f, heart[i], v, a)); + } + return outParticles; } @@ -222,56 +283,7 @@ class Snow : public Particle { std::vector outParticles; return outParticles; } - - static int myRand(int down, int up) { - return (rand() % (up - down - 1) + down); - } - - // Generate point positions from image - static std::vector ptsFromImg(std::string name, Vector2f trans, - Vector2f scale) { - std::vector ps; - cv::Mat inImg = cv::imread(name); - - int rndSize = 100; // resolution along each axis - int imgWidth = inImg.size().width; - int imgHeight = inImg.size().height; - - for (int i = 0; i < rndSize; i++) { - for (int j = 0; j < rndSize; j++) { - int x, y; - x = myRand(0, imgWidth - 1); - y = myRand(0, imgHeight - 1); - - cv::Vec3b pixelSrc = inImg.at(cv::Point(x, y)); - - int color = pixelSrc[0] + pixelSrc[1] + pixelSrc[2]; - // some threshold - if (color < 10) { - Vector2f pos; - - // rescale - float fx, fy; - fx = (float)x / (float)imgWidth; // to [0, 1.0] - fy = (float)y / (float)imgHeight; - fx *= scale[0]; - fy *= scale[0]; - - // translate - fx += trans[0]; - fy += trans[1]; - - // back to world space - pos = Vector2f(fx * (float)X_GRID, fy * (float)Y_GRID); - - ps.push_back(pos); - } - } // end inner for - } // end outer for - - return ps; - } // end ptsFromImg -}; // end class Snow +}; // end class Snow /* ELASTIC */ class Elastic : public Particle {