示例#1
0
int main(int argc, char *argv[]){

    std::unique_ptr<FractalTree> fractalTree(new FractalTree(PATH));

    po::options_description desc("Allowed options");

    desc.add_options()
        ("help", "Produce help message")
        ("set", "Set a register, must have id, code and description")
        ("get", "Retrive a register, id must be handled")
        ("delete", "Delete a register, id must be handled")
        ("showAll", "Show all registers")
        ("id", po::value<uint32_t>(), "The register id")
        ("code", po::value<std::string>(), "The register code")
        ("description", po::value<std::string>(), "The register description")
    ;

    po::variables_map vm;
    po::store(po::parse_command_line(argc, argv, desc), vm);
    po::notify(vm);

    if (vm.count("help")) {
        std::cout << desc << "\n";
    } else if (vm.count("set") && vm.count("description") && vm.count("code") && vm.count("id")) {
        uint32_t id = vm["id"].as<uint32_t>();
        std::string code = vm["code"].as<std::string>();
        std::string description = vm["description"].as<std::string>();

        if( code.length() != 3 || description.length() > 1000 ){
            std::cout << "Error. Code must be 3 characters long and description under 1000" << std::endl;
            return 0;
        }
        Register _register = Register(id, code.c_str(), description.c_str());
        if(fractalTree->setRegister(&_register)){
            std::cout << "Ok" << std::endl;
            return 0;
        }
        std::cout << "Error" << std::endl;
    } else if(vm.count("get") && vm.count("id")){
        Register* _register = fractalTree->getRegister(vm["id"].as<uint32_t>());
        std::cout << "Id: " << _register->getId() << std::endl;
        std::cout << "Code: " << _register->getCode() << std::endl;
        std::cout << "Description: " << _register->getDescription() << std::endl;
        delete _register;
        return 0;
    } else if(vm.count("delete") && vm.count("id")){
        if( fractalTree->deleteRegister(vm["id"].as<uint32_t>()) ){
            std::cout << "Ok" << std::endl;
            return 0;
        }
        std::cout << "Not found" << std::endl;
    } else if(vm.count("showAll")){
        fractalTree->showAll();
    }  else{

        std::cout << "Invalid option" << std::endl;
    }

    return 0;
}
TreeBufferPos tree::genTree(Vertex* drawVertices, int startPoint, int count, float angleRight, float angleLeft, float height)
{
	drawVertices[startPoint] = { { 0.0,0.0f,0,1 } ,{0,0,0} , {0,0},{ 0.3f,0.1f,0,1 } };
	drawVertices[startPoint+1] = { { 0.0, height, 0,1 },{ 0,0,0 } ,{ 0,0 },{ 0.3f,0.1f,0,1 } };
	srand(time(0));
	fractalTree(count, drawVertices[startPoint], drawVertices[startPoint+1], angleRight, angleLeft);
	
	cout << "Number of branches: "<<branchesIterator << endl;
	int q = 0;
	int leafIterator = 0;
	for (int i = startPoint+2; i <= startPoint + branchesIterator*2 +1; i += 2) {
		drawVertices[i] = branches[q].zero;
		drawVertices[i + 1] = branches[q].one;
		if (branches[q].end) {
			leaves[leafIterator].base = branches[q].one;
			leafIterator++;
		}
		q++;
	}
	cout << "Number of leaves " << leafIterator << endl;
	//GenLeaves
	//Somereason always one extra leaf;
	for (int i = 0; i < leafIterator; i++) {
		genLeaves(i);
	}
	//Add leaves to buffer
	int z = 0;
	for (int i = startPoint + branchesIterator * 2 +2; i < startPoint + (branchesIterator * 2+2) + (leafIterator)*4; i += 4) {
		drawVertices[i] = leaves[z].base;
		drawVertices[i + 1] = leaves[z].left;
		drawVertices[i + 2] = leaves[z].right;
		drawVertices[i + 3] = leaves[z].top;
		z++;
	}
	cout << "Tree starts at buffer: " << startPoint << endl;
	cout << "Tree ends at buffer: " << startPoint+branchesIterator * 2 + 1 << endl;
	cout << "Leaves start at buffer: " << startPoint+branchesIterator * 2 + 2 << endl;
	cout << "Leaves ends at buffer: " << startPoint+((branchesIterator * 2 + 2) + (leafIterator)* 4) << endl;
	TreeBufferPos pos;
	pos.treeStart = startPoint;
	pos.treeFinish = startPoint + branchesIterator * 2 + 1;
	pos.leafStart = startPoint + branchesIterator * 2 + 2;
	pos.leafFinish = startPoint + ((branchesIterator * 2 + 2) + (leafIterator)* 4);
	pos.depth = depth;
	return pos;
}
void tree::fractalTree(int count, Vertex zero, Vertex one, float angleRight, float angleLeft)
{
	count--;
	if (count > 0) {
		vec4 Zero(zero.coords[0], zero.coords[1],zero.coords[2],1);
		vec4 One(one.coords[0], one.coords[1], one.coords[2],1);

		vec4 Scale = (One - Zero);
		Scale = Scale * 0.8f;
		//float randValue = randomFloat(-rand, rand);
		//float alpha = ((angleRight + randValue)*3.14 / 180);
		////mat3x3 rotMatrix(cos(alpha), sin(alpha), -sin(alpha), cos(alpha));
		//mat3x3 rotMatrixX(1, 0, 0, 0, cos(alpha), sin(alpha), 0, -sin(alpha), cos(alpha));
		//float randValue2 = randomFloat(-rand, rand);
		//float beta = ((angleRight + randValue2)*3.14 / 180);
		//mat3x3 rotMatrixZ(cos(beta), sin(beta), 0, -sin(beta), cos(beta), 0, 0, 0, 1);
		//vec3 Answer = (Scale * rotMatrixZ)*rotMatrixX;
		mat4 rotMatrix = mat4(1);
		rotMatrix = rotate(rotMatrix, randomFloat((-angleRight), angleRight)*3.14f/180.0f, vec3(0, 1, 0));
		rotMatrix = rotate(rotMatrix, randomFloat((-angleRight), angleRight)*3.14f / 180.0f, vec3(0, 0, 1));
		rotMatrix = rotate(rotMatrix, randomFloat((-angleRight), angleRight)*3.14f / 180.0f, vec3(1, 0, 0));
		vec4 Answer = rotMatrix * Scale;
		Answer = Answer + One;

		branches[branchesIterator].zero = one;
		branches[branchesIterator].zero.colors[0] = 0.3f;
		branches[branchesIterator].zero.colors[1] = 0.1f;
		branches[branchesIterator].one.coords[0] = Answer.x;
		branches[branchesIterator].one.coords[1] = Answer.y;
		branches[branchesIterator].one.coords[2] = Answer.z;
		branches[branchesIterator].one.coords[3] = 1;
		branches[branchesIterator].one.coords[3] = 1;
		branches[branchesIterator].one.colors[0] = 0.3f;
		branches[branchesIterator].one.colors[1] = 0.1f;
		if (count == 1)
			branches[branchesIterator].end = true;
		else
			branches[branchesIterator].end = false;
		branchesIterator++;
		depth.push_back(count);
		fractalTree(count, branches[branchesIterator - 1].zero, branches[branchesIterator - 1].one, angleRight, angleLeft);

		////////////////////////////////////////////////

		Zero = vec4(zero.coords[0], zero.coords[1], zero.coords[2],1);
		One = vec4(one.coords[0], one.coords[1], one.coords[2],1);

		Scale = One - Zero;
		Scale = Scale * 0.8f;
		/*randValue = randomFloat(-rand, rand);
		alpha = ((angleLeft +randValue)*3.14 / 180)*-1;
		rotMatrixX = mat3x3(1, 0, 0, 0, cos(alpha), sin(alpha), 0, -sin(alpha), cos(alpha));
		randValue2 = randomFloat(-rand, rand);
		beta = ((angleLeft + randValue2)*3.14 / 180)*-1;
		rotMatrixZ = mat3x3(cos(beta), sin(beta), 0, -sin(beta), cos(beta), 0, 0, 0, 1);
		Answer = (Scale * rotMatrixZ)*rotMatrixX;*/

		rotMatrix = mat4(1);
		rotMatrix = rotate(rotMatrix, (randomFloat((-angleLeft), angleLeft)*3.14f/180.0f), vec3(0, 1, 0));
		rotMatrix = rotate(rotMatrix, randomFloat((-angleLeft), angleLeft)*3.14f / 180.0f, vec3(0, 0, 1));
		rotMatrix = rotate(rotMatrix, randomFloat((-angleLeft), angleLeft)*3.14f / 180.0f, vec3(1, 0, 0));

		Answer = rotMatrix * Scale;
		Answer = Answer + One;

		branches[branchesIterator].zero = one;
		branches[branchesIterator].zero.colors[0] = 0.3f;
		branches[branchesIterator].zero.colors[1] = 0.1f;
		branches[branchesIterator].one.coords[0] = Answer.x;
		branches[branchesIterator].one.coords[1] = Answer.y;
		branches[branchesIterator].one.coords[2] = Answer.z;
		branches[branchesIterator].one.coords[3] = 1;
		branches[branchesIterator].one.coords[3] = 1;
		branches[branchesIterator].one.colors[0] = 0.3f;
		branches[branchesIterator].one.colors[1] = 0.1f;
		if (count == 1)
			branches[branchesIterator].end = true;
		else
			branches[branchesIterator].end = false;
		branchesIterator++;
		depth.push_back(count);
		fractalTree(count, branches[branchesIterator - 1].zero, branches[branchesIterator - 1].one, angleRight,angleLeft);
	}
}