Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
iamyoukou committed Oct 10, 2019
1 parent b20cc53 commit bd9e579
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 51 deletions.
2 changes: 1 addition & 1 deletion src/constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Binary file modified src/main
Binary file not shown.
112 changes: 62 additions & 50 deletions src/particle.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<Vector2f> ptsFromImg(std::string name, Vector2f trans,
Vector2f scale) {
std::vector<Vector2f> 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::Vec3b>(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
};

/* ----------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -62,6 +112,17 @@ class Water : public Particle {
/* Static Functions */
static std::vector<Water> InitializeParticles() {
std::vector<Water> outParticles;
Vector2f v = Vector2f(30, 0); // Initial velocity
Matrix2f a = Matrix2f(0);

// get position from image
std::vector<Vector2f> 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;
}

Expand Down Expand Up @@ -222,56 +283,7 @@ class Snow : public Particle {
std::vector<Snow> outParticles;
return outParticles;
}

static int myRand(int down, int up) {
return (rand() % (up - down - 1) + down);
}

// Generate point positions from image
static std::vector<Vector2f> ptsFromImg(std::string name, Vector2f trans,
Vector2f scale) {
std::vector<Vector2f> 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::Vec3b>(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 {
Expand Down

0 comments on commit bd9e579

Please sign in to comment.