Merge pull request #2 from shadow8t4/alexdev

Alexdev
This commit is contained in:
Alex Huddleston 2016-12-05 11:35:01 -06:00 committed by GitHub Enterprise
commit e783869545
5 changed files with 119 additions and 61 deletions

View file

@ -49,6 +49,7 @@ namespace CompFab
Vec3iStruct();
Vec3iStruct(int x, int y, int z);
Vec3iStruct(int i);
union
{
int m_pos[3];
@ -104,7 +105,15 @@ namespace CompFab
//Compute v1 - v2
Vec3 operator-(const Vec3 &v1, const Vec3 &v2);
Vec3 operator+(const Vec3 &v1, const Vec3 &v2);
Vec3 operator+(const Vec3 & v1, const Vec3 & v2);
Vec3i operator+(const Vec3i & v1, const Vec3i & v2);
//Matrix multiplication
Vec3 mmult(const Vec3 & v1, const Vec3 & v2);
//Scalar multiplication
Vec3 smult(double s, const Vec3 & v);
//Cross Product
Vec3 operator%(const Vec3 &v1, const Vec3 &v2);

View file

@ -48,6 +48,14 @@ CompFab::Vec3iStruct::Vec3iStruct(int x, int y, int z)
m_z = z;
}
CompFab::Vec3iStruct::Vec3iStruct(int i)
{
m_x = i;
m_y = i;
m_z = i;
}
CompFab::Vec2fStruct::Vec2fStruct()
{
m_x = m_y = 0.0;
@ -99,9 +107,30 @@ CompFab::Vec3 CompFab::operator+(const Vec3 &v1, const Vec3 &v2)
return v3;
}
CompFab::Vec3i CompFab::operator+(const Vec3i &v1, const Vec3i &v2)
{
Vec3i v3i;
v3i[0] = v1[0] + v2[0];
v3i[1] = v1[1] + v2[1];
v3i[2] = v1[2] + v2[2];
return v3i;
}
//Matrix multiplication
CompFab::Vec3 CompFab::mmult(const Vec3& v1, const Vec3& v2)
{
return Vec3(v1.m_x*v2.m_x, v1.m_y*v2.m_y, v1.m_z*v2.m_z);
}
//Scalar multiplication
CompFab::Vec3 CompFab::smult(double s, const Vec3 & v)
{
return mmult(CompFab::Vec3(s,s,s), v);
}
//Cross Product
Vec3 CompFab::operator%(const Vec3 &v1, const Vec3 &v2)
CompFab::Vec3 CompFab::operator%(const Vec3 &v1, const Vec3 &v2)
{
Vec3 v3;
v3[0] = v1[1]*v2[2] - v1[2]*v2[1];

View file

@ -1,4 +1,4 @@
#include "PerlinNoise.h"
#include "../include/PerlinNoise.h"
#include <cmath>
#include <random>
#include <algorithm>

View file

@ -63,7 +63,7 @@ void findLW(Mesh &m, double &l, double &w)
}
// Calculate translation matrices and output them as a vector of Vec3s.
std::vector<CompFab::Vec3> createVec3d(int layers, double spacing, double length, double width)
std::vector<CompFab::Vec3> createVec3d(std::vector<CompFab::Vec3> &t, int layers, double spacing, double length, double width)
{
std::vector<CompFab::Vec3> *output = new std::vector<CompFab::Vec3>();
@ -71,36 +71,63 @@ std::vector<CompFab::Vec3> createVec3d(int layers, double spacing, double length
double ws = width + spacing;
// Will be used later to determine the direction of the translation matrix.
// This is used to bypass needing to create a rotation matrix.
// Should consider doing so anyway to speed up process, use less memory, and add modularization.
double angle = 0.0;
CompFab::Vec3 *temp = new CompFab::Vec3(-ls, -ws, 0);
// Vec3 to hold our current translation matrix.
CompFab::Vec3 *trans = new CompFab::Vec3(0, spacing, 0);
CompFab::Vec3 *trans = new CompFab::Vec3(ls, ws, 0);
//Vec3 to hold our current height multiplication materx.
CompFab::Vec3 *mult = new CompFab::Vec3(1,1,1);
//push_back initial templat's mesh, multiplied by the height multiplication matrix.
for(int i = 0; i < t.size(); i++)
{
output->push_back(mmult(*mult, t[i]));
}
CompFab::Vec3 *coord = new CompFab::Vec3(0.5,0.5,0);
PerlinNoise *p = new PerlinNoise();
// cl for current layer.
for(int cl = 1; cl < layers; cl++)
{
// Constructor used to bypass needing to create a new operator override for multiplication.
// Should also consider doing so anyway to speed up process, use less memory, and add modularization.
// Constructor used to re-initialize temp.
*temp = CompFab::Vec3(-ls*cl, -ws*cl, 0);
for(int c = 0; c < cl*8; c++)
{
angle = (c/(2*cl))*(0.5*PI);
*trans = CompFab::Vec3(ls*cos(angle), ws*sin(angle), 0);
*temp = *temp + *trans;
*temp += *trans;
*coord = *coord + mmult(*trans, CompFab::Vec3((1/(layers*2.0-1)/2.0), (1/(layers*2.0-1)/2.0), 0));
output->push_back(*temp);
*coord = *coord + mmult(trans, Vec3(1/(layers*2-1)/2),1/(layers*2-1)/2,0);
*temp=mmult(temp,Vec3(1,1,n));
// Used to change the multiplciation matrix per layer.
mult->m_z = jerfunc(coord->m_x, coord->m_y, *p);
for(int j = 0; j < t.size(); j++)
{
output->push_back(mmult(*mult, t[j]) + *temp);
}
}
}
return *output;
}
std::vector<CompFab::Vec3i> createVec3id(std::vector<CompFab::Vec3i> &t, std::vector<CompFab::Vec3> &v, int layers)
{
std::vector<CompFab::Vec3i> *output = new std::vector<CompFab::Vec3i>();
*output = t;
for(int n = 1; n < pow((2*layers - 1), 2); n++)
{
CompFab::Vec3i *offset = new CompFab::Vec3i(v.size()*n);
for(int k = 0; k < t.size(); k++)
{
output->push_back(t[k] + *offset);
}
}
@ -113,61 +140,54 @@ int main(int argc, char **argv)
// Error checking.
if(argc < 3)
{
std::cout << "Usage: [executable] [template].obj output.obj [optional: -d for debugging output]" << std::endl;
std::cout << "Usage: [executable] [template].obj output.obj [mode (-g, etc.)] [optional: -d for debugging output]" << std::endl;
std::exit(1);
}
// TODO: Modularize these.
int layers = 10;
double spacing = 1.0;
// Create Mesh object from file, output to manipulate from template Mesh.
Mesh *test = new Mesh(argv[1], false);
Mesh *output = new Mesh(test->v, test->t);
double l = 0, w = 0;
double *length = &l, *width = &w;
// Find the X and Y dimensions for the mesh. Assumes the mesh is facing upright.
findLW(*test, *length, *width);
// Calculate the translation matrices needed.
std::vector<CompFab::Vec3> d = createVec3d(layers, spacing, *length, *width);
// Duplicating template, will later be replaced with a much more robust procedural generation function.
for(int i = 0; i < d.size(); i++)
{
for(int j = 0; j < test->v.size(); j++)
{
output->v.push_back(CompFab::Vec3(test->v[j] + d[i]));
}
}
// Copying needed triangle data.
for(int n = 1; n < pow((2*layers - 1), 2); n++)
{
int offset = test->v.size()*n;
for(int k = 0; k < test->t.size(); k++)
{
output->t.push_back(CompFab::Vec3i(test->t[k].m_x +offset, test->t[k].m_y + offset, test->t[k].m_z + offset));
}
}
// Initialization of needed output variables.
std::vector<CompFab::Vec3> vv;
std::vector<CompFab::Vec3i> tt;
Mesh *output = new Mesh();
// Debugging
if(argc > 3)
{
// {
if(strcmp(argv[3], "-g") == 0 || strcmp(argv[3], "-d") == 0)
{
// TODO: Modularize these.
int layers = 10;
double spacing = 1;
// Create Mesh object from file, output to manipulate from template Mesh.
Mesh *test = new Mesh(argv[1], false);
double l = 0, w = 0;
double *length = &l, *width = &w;
// Find the X and Y dimensions for the mesh. Assumes the mesh is facing upright.
findLW(*test, *length, *width);
// Calculate the translation matrices needed and apply them.
vv = createVec3d(test->v, layers, spacing, *length, *width);
// Copying needed triangle data.
tt = createVec3id(test->t, test->v, layers);
// Using contructor to create new output Mesh.
*output = Mesh(vv, tt);
}
if(strcmp(argv[argc - 1], "-d") == 0)
{
for(int j = 0; j < output->v.size(); j++)
{
std::cout << output->v[j].m_x << " " << output->v[j].m_y << " " << output->v[j].m_z << std::endl;
std::cout << output->t[j].m_x << " " << output->t[j].m_y << " " << output->t[j].m_z << std::endl;
std::cout << std::endl;
}
}
else
{
std::cout << "Usage: [executable] [template].obj output.obj [optional: -d for debugging output]" << std::endl;
std::cout << std::endl;
for(int k = 0; k < output->t.size(); k++)
{
std::cout << output->t[k].m_x << " " << output->t[k].m_y << " " << output->t[k].m_z << std::endl;
}
}
}

View file

@ -3,7 +3,7 @@
#include <sstream>
#include <exception>
#include "ppm.h"
#include "../include/ppm.h"
//init with default values