From 1883dc950f8a5c2dc9dd3dc281a7c8a7c16e423f Mon Sep 17 00:00:00 2001 From: Alex Date: Sat, 26 Nov 2016 18:29:34 -0600 Subject: [PATCH 1/3] Changes to README --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index b51ffc9..eba2e03 100644 --- a/README.md +++ b/README.md @@ -3,4 +3,4 @@ A C++ engine for procedural city generation by manipulating .obj models and outputing an .obj model of a city. Use cmake file included to generate project. -Current command line arguments required: (project directory)/data/(desired .obj model to use) output.obj +Usage: [executable] [template].obj output.obj [optional: -d for debugging output] From da3889d4706b982da33dd1416ee3ee6961adb602 Mon Sep 17 00:00:00 2001 From: Alex Date: Sun, 27 Nov 2016 01:12:12 -0600 Subject: [PATCH 2/3] Template .obj now duplicates on modular scale with modular spacing between layers. Will clean up comments later. --- source/main.cpp | 108 ++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 101 insertions(+), 7 deletions(-) diff --git a/source/main.cpp b/source/main.cpp index 0dbe6bc..09456fd 100644 --- a/source/main.cpp +++ b/source/main.cpp @@ -7,6 +7,9 @@ #include #include "../include/CompFab.h" #include "../include/Mesh.h" +#include + +#define PI 3.14159265 void findLW(Mesh &m, double &l, double &w) { @@ -23,38 +26,129 @@ void findLW(Mesh &m, double &l, double &w) } if(m.v[i].m_y < minw) { - minw = m.v[i].m_x; + minw = m.v[i].m_y; } if(m.v[i].m_y > maxw) { - maxw = m.v[i].m_x; + maxw = m.v[i].m_y; } } l = maxl - minl; w = maxw - minw; } +// Calculate translation matrices and add them to the vector of translation matrices provided. +std::vector createVec3d(int layers, double spacing, double length, double width) +{ + std::vector *output = new std::vector(); + + CompFab::Vec3 *temp = new CompFab::Vec3(-(length + spacing), -(width + spacing), 0); + CompFab::Vec3 *trans = new CompFab::Vec3(0, spacing, 0); + + for(int cl = 1; cl < layers; cl++) + { + *temp = CompFab::Vec3(-(length + spacing)*cl, -(width + spacing)*cl, 0); + + for(int c = 0; c < cl*8; c++) + { + /* + double cosine = cos(((2*PI)/(cl*8))*c); + double sine = sin(((2*PI)/(cl*8))*c); + // + double xcoord = 1; + double angle = tan((2*PI)/(cl*8))*c; + double ycoord = 1; + if(cosine != 0) + { + ycoord = sine/cosine; + } + if(sine != 0) + { + xcoord = cosine/sine; + } + // + double x = cosine;//(sqrt(1 - (sine*sine)/2)); + if(x > 0) + { + x = floor(x); + } + else + { + x = ceil(x); + } + double y = sine;//(sqrt(1-(cosine*cosine)/2)); + if(y > 0) + { + y = floor(x); + } + else + { + y = ceil(x); + } + */ + *trans = CompFab::Vec3(spacing*cos(floor((c/(2*cl)))*(0.5*PI)) + length*cos(floor((c/(2*cl)))*(0.5*PI)), spacing*sin(floor((c/(2*cl)))*(0.5*PI)) + width*sin(floor((c/(2*cl)))*(0.5*PI)), 0); + *temp = *temp + *trans; + + output->push_back(*temp); + } + } + + /* + temp->push_back(CompFab::Vec3(length,0,0)); + temp->push_back(CompFab::Vec3(length,width,0)); + temp->push_back(CompFab::Vec3(0,width,0)); + temp->push_back(CompFab::Vec3(-length,width,0)); + temp->push_back(CompFab::Vec3(-length,0,0)); + temp->push_back(CompFab::Vec3(-length,-width,0)); + temp->push_back(CompFab::Vec3(0,-width,0)); + temp->push_back(CompFab::Vec3(length,-width,0)); + */ + + return *output; +} + int main(int argc, char **argv) { - unsigned int num = 16; //number of voxels (e.g. 16x16x16) + // Error checking. if(argc < 3) { std::cout << "Usage: [executable] [template].obj output.obj [optional: -d for debugging output]" << std::endl; std::exit(1); } + + // Modularize this later. + 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); + + int layers = 10; double l = 0, w = 0; double *length = &l, *width = &w; + + // Find dimensions for the mesh. Assumes the mesh is facing upright. findLW(*test, *length, *width); - for(int i = 0; i < test->v.size(); i++) + + std::vector 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++) { - output->v.push_back(*new CompFab::Vec3(test->v[i].m_x + *length + 1, test->v[i].m_y, test->v[i].m_z)); + for(int j = 0; j < test->v.size(); j++) + { + output->v.push_back(CompFab::Vec3(test->v[j] + d[i])); + } } - for(int k = 0; k < test->t.size(); k++) + + // Copying needed triangle data. + for(int n = 1; n < (2*layers - 1)*(2*layers - 1); n++) { - output->t.push_back(*new CompFab::Vec3i(test->t[k].m_x + test->v.size(), test->t[k].m_y + test->v.size(), test->t[k].m_z + test->v.size())); + for(int k = 0; k < test->t.size(); k++) + { + output->t.push_back(CompFab::Vec3i(test->t[k].m_x + test->v.size()*n, test->t[k].m_y + test->v.size()*n, test->t[k].m_z + test->v.size()*n)); + } } // Debugging From 70dfe11124db3f1e9fa637d737c1de8c1355139b Mon Sep 17 00:00:00 2001 From: Alex Date: Sun, 27 Nov 2016 02:52:25 -0600 Subject: [PATCH 3/3] Cleaned up code. Added comments. Hope they help, Jeremy. --- CMakeLists.txt | 8 ++--- source/main.cpp | 81 ++++++++++++++++--------------------------------- 2 files changed, 30 insertions(+), 59 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index a5320ca..8029128 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,10 +1,10 @@ CMAKE_MINIMUM_REQUIRED(VERSION 2.8) -project(voxelizer) +project(citygen) -file(GLOB_RECURSE HEADER_CODE ${voxelizer_SOURCE_DIR}/include/*.h) -file(GLOB_RECURSE SRC_CODE ${voxelizer_SOURCE_DIR}/source/*.cpp) +file(GLOB_RECURSE HEADER_CODE ${citygen_SOURCE_DIR}/include/*.h) +file(GLOB_RECURSE SRC_CODE ${citygen_SOURCE_DIR}/source/*.cpp) -ADD_EXECUTABLE(voxelizer ${SRC_CODE} ${HEADER_CODE}) +ADD_EXECUTABLE(citygen ${SRC_CODE} ${HEADER_CODE}) diff --git a/source/main.cpp b/source/main.cpp index 09456fd..df403bb 100644 --- a/source/main.cpp +++ b/source/main.cpp @@ -11,6 +11,7 @@ #define PI 3.14159265 +// A function to find the X and Y dimensions of the template obj void findLW(Mesh &m, double &l, double &w) { double minl, maxl, minw, maxw; @@ -37,73 +38,41 @@ void findLW(Mesh &m, double &l, double &w) w = maxw - minw; } -// Calculate translation matrices and add them to the vector of translation matrices provided. +// Calculate translation matrices and output them as a vector of Vec3s. std::vector createVec3d(int layers, double spacing, double length, double width) { std::vector *output = new std::vector(); - CompFab::Vec3 *temp = new CompFab::Vec3(-(length + spacing), -(width + spacing), 0); + double ls = length + spacing; + 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); + // cl for current layer. for(int cl = 1; cl < layers; cl++) { - *temp = CompFab::Vec3(-(length + spacing)*cl, -(width + spacing)*cl, 0); + // 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. + *temp = CompFab::Vec3(-ls*cl, -ws*cl, 0); for(int c = 0; c < cl*8; c++) { - /* - double cosine = cos(((2*PI)/(cl*8))*c); - double sine = sin(((2*PI)/(cl*8))*c); - // - double xcoord = 1; - double angle = tan((2*PI)/(cl*8))*c; - double ycoord = 1; - if(cosine != 0) - { - ycoord = sine/cosine; - } - if(sine != 0) - { - xcoord = cosine/sine; - } - // - double x = cosine;//(sqrt(1 - (sine*sine)/2)); - if(x > 0) - { - x = floor(x); - } - else - { - x = ceil(x); - } - double y = sine;//(sqrt(1-(cosine*cosine)/2)); - if(y > 0) - { - y = floor(x); - } - else - { - y = ceil(x); - } - */ - *trans = CompFab::Vec3(spacing*cos(floor((c/(2*cl)))*(0.5*PI)) + length*cos(floor((c/(2*cl)))*(0.5*PI)), spacing*sin(floor((c/(2*cl)))*(0.5*PI)) + width*sin(floor((c/(2*cl)))*(0.5*PI)), 0); + angle = (c/(2*cl))*(0.5*PI); + *trans = CompFab::Vec3(ls*cos(angle), ws*sin(angle), 0); *temp = *temp + *trans; output->push_back(*temp); } } - /* - temp->push_back(CompFab::Vec3(length,0,0)); - temp->push_back(CompFab::Vec3(length,width,0)); - temp->push_back(CompFab::Vec3(0,width,0)); - temp->push_back(CompFab::Vec3(-length,width,0)); - temp->push_back(CompFab::Vec3(-length,0,0)); - temp->push_back(CompFab::Vec3(-length,-width,0)); - temp->push_back(CompFab::Vec3(0,-width,0)); - temp->push_back(CompFab::Vec3(length,-width,0)); - */ - return *output; } @@ -117,20 +86,21 @@ int main(int argc, char **argv) std::exit(1); } - // Modularize this later. + // 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); - int layers = 10; double l = 0, w = 0; double *length = &l, *width = &w; - // Find dimensions for the mesh. Assumes the mesh is facing upright. + // 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 d = createVec3d(layers, spacing, *length, *width); // Duplicating template, will later be replaced with a much more robust procedural generation function. @@ -143,11 +113,12 @@ int main(int argc, char **argv) } // Copying needed triangle data. - for(int n = 1; n < (2*layers - 1)*(2*layers - 1); n++) + 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 + test->v.size()*n, test->t[k].m_y + test->v.size()*n, test->t[k].m_z + test->v.size()*n)); + output->t.push_back(CompFab::Vec3i(test->t[k].m_x +offset, test->t[k].m_y + offset, test->t[k].m_z + offset)); } }