Fixed getCommandOption and cmOptionExists to be more porable

added support for --NoConnect command line option to test running without connecting
added pruneTree to VoxelTree
This commit is contained in:
ZappoMan 2013-03-25 22:39:24 -07:00
parent 0711539fc6
commit 2f6b2466ea
6 changed files with 101 additions and 21 deletions

View file

@ -56,6 +56,7 @@ VoxelSystem::~VoxelSystem() {
// Complaints: Brad :) // Complaints: Brad :)
// To Do: Need to add color data to the file. // To Do: Need to add color data to the file.
void VoxelSystem::loadVoxelsFile(char* fileName) { void VoxelSystem::loadVoxelsFile(char* fileName) {
int vCount = 0;
std::ifstream file(fileName, std::ios::in|std::ios::binary); std::ifstream file(fileName, std::ios::in|std::ios::binary);
@ -65,10 +66,11 @@ void VoxelSystem::loadVoxelsFile(char* fileName) {
int totalBytesRead = 0; int totalBytesRead = 0;
if(file.is_open()) if(file.is_open())
{ {
while (!file.eof()) { bool bail = false;
while (!file.eof() && !bail) {
file.get(octets); file.get(octets);
totalBytesRead++; totalBytesRead++;
lengthInBytes = (octets*3/8)+1; lengthInBytes = bytesRequiredForCodeLength(octets)-1; //(octets*3/8)+1;
unsigned char * voxelData = new unsigned char[lengthInBytes+1+3]; unsigned char * voxelData = new unsigned char[lengthInBytes+1+3];
voxelData[0]=octets; voxelData[0]=octets;
char byte; char byte;
@ -78,16 +80,32 @@ void VoxelSystem::loadVoxelsFile(char* fileName) {
totalBytesRead++; totalBytesRead++;
voxelData[i+1] = byte; voxelData[i+1] = byte;
} }
// random color data // read color data
voxelData[lengthInBytes+1] = randomColorValue(65); char red,green,blue;
voxelData[lengthInBytes+2] = randomColorValue(65); file.get(red);
voxelData[lengthInBytes+3] = randomColorValue(65); file.get(green);
file.get(blue);
//printf("red:%d\n",red);
//printf("green:%d\n",green);
//printf("blue:%d\n",blue);
vCount++;
//printf("vCount:%d\n",vCount);
//randomColorValue(65);
float rf = randFloatInRange(.5,1); // add a little bit of variance to colors so we can see the voxels
voxelData[lengthInBytes+1] = red * rf;
voxelData[lengthInBytes+2] = green * rf;
voxelData[lengthInBytes+3] = blue * rf;
//printVoxelCode(voxelData);
tree->readCodeColorBufferToTree(voxelData); tree->readCodeColorBufferToTree(voxelData);
delete voxelData; delete voxelData;
} }
file.close(); file.close();
} }
tree->pruneTree(tree->rootNode);
// reset the verticesEndPointer so we're writing to the beginning of the array // reset the verticesEndPointer so we're writing to the beginning of the array
verticesEndPointer = verticesArray; verticesEndPointer = verticesArray;

View file

@ -64,7 +64,7 @@ int simulate_on = 1;
// Network Socket and network constants // Network Socket and network constants
// //
char DOMAIN_HOSTNAME[] = "highfidelity.below92.com"; char DOMAIN_HOSTNAME[100] = "highfidelity.below92.com";
char DOMAIN_IP[100] = ""; // IP Address will be used first if not empty string char DOMAIN_IP[100] = ""; // IP Address will be used first if not empty string
const int DOMAINSERVER_PORT = 40102; const int DOMAINSERVER_PORT = 40102;
@ -948,6 +948,14 @@ void audioMixerUpdate(in_addr_t newMixerAddress, in_port_t newMixerPort) {
int main(int argc, char** argv) int main(int argc, char** argv)
{ {
// pass --NoConnect as a command line option to force NOT connecting
// to the hifidelity domain, and therefore run locally.
if (cmdOptionExists(argc, argv, "--NoConnect"))
{
strcpy(DOMAIN_HOSTNAME,"");
}
#ifndef _WIN32 #ifndef _WIN32
struct ifaddrs * ifAddrStruct=NULL; struct ifaddrs * ifAddrStruct=NULL;
struct ifaddrs * ifa=NULL; struct ifaddrs * ifa=NULL;
@ -1018,7 +1026,7 @@ int main(int argc, char** argv)
// Check to see if the user passed in a command line option for loading a local // Check to see if the user passed in a command line option for loading a local
// Voxel File. If so, load it now. // Voxel File. If so, load it now.
char* voxelsFilename = getCmdOption(argv, argv + argc, "-i"); char* voxelsFilename = getCmdOption(argc, argv, "-i");
if (voxelsFilename) if (voxelsFilename)
{ {
voxels.loadVoxelsFile(voxelsFilename); voxels.loadVoxelsFile(voxelsFilename);

View file

@ -86,27 +86,42 @@ void switchToResourcesIfRequired() {
// immediately following the flag. For example if you ran: // immediately following the flag. For example if you ran:
// ./app -i filename.txt // ./app -i filename.txt
// then you're using the "-i" flag to set the input file name. // then you're using the "-i" flag to set the input file name.
// Usage: char * inputFilename = getCmdOption(argv, argv + argc, "-i"); // Usage: char * inputFilename = getCmdOption(argc, argv, "-i");
// Complaints: Brad :) // Complaints: Brad :)
char* getCmdOption(char ** begin, char ** end, const std::string & option) char* getCmdOption(int argc, char** argv,char* option)
{ {
char ** itr = std::find(begin, end, option); // check each arg
if (itr != end && ++itr != end) for (int i=0; i < argc; i++)
{ {
return *itr; // if the arg matches the desired option
if (strcmp(option,argv[i])==0 && i+1 < argc)
{
// then return the next option
return argv[i+1];
}
} }
return 0; return NULL;
} }
////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////
// Function: getCmdOption() // Function: getCmdOption()
// Description: Handy little function to tell you if a command line option flag was // Description: Handy little function to tell you if a command line option flag was
// included while launching the application. Returns bool true/false // included while launching the application. Returns bool true/false
// Usage: bool wantDump = cmdOptionExists(argv, argv+argc, "-d"); // Usage: bool wantDump = cmdOptionExists(argc, argv, "-d");
// Complaints: Brad :) // Complaints: Brad :)
bool cmdOptionExists(char** begin, char** end, const std::string& option) bool cmdOptionExists(int argc, char** argv,char* option)
{ {
return std::find(begin, end, option) != end; // check each arg
for (int i=0; i < argc; i++)
{
// if the arg matches the desired option
if (strcmp(option,argv[i])==0)
{
// then return the next option
return true;
}
}
return false;
} }
////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////

View file

@ -33,8 +33,8 @@ bool oneAtBit(unsigned char byte, int bitIndex);
void switchToResourcesIfRequired(); void switchToResourcesIfRequired();
char* getCmdOption(char ** begin, char ** end, const std::string& option); char* getCmdOption(int argc, char** argv,char* option);
bool cmdOptionExists(char** begin, char** end, const std::string& option); bool cmdOptionExists(int argc, char** argv,char* option);
unsigned char* pointToVoxel(float x, float y, float z, float s, unsigned char r, unsigned char g, unsigned char b ); unsigned char* pointToVoxel(float x, float y, float z, float s, unsigned char r, unsigned char g, unsigned char b );

View file

@ -77,6 +77,44 @@ VoxelNode * VoxelTree::createMissingNode(VoxelNode *lastParentNode, unsigned cha
} }
} }
/// If node has color & <= 4 children then prune children
void VoxelTree::pruneTree(VoxelNode* pruneAt) {
int childCount = 0;
// determine how many children we have
for (int i = 0; i < 8; i++)
{
if (pruneAt->children[i])
{
childCount++;
}
}
// if appropriate, prune them
if (pruneAt->color[3] && childCount <= 4)
{
for (int i = 0; i < 8; i++)
{
if (pruneAt->children[i])
{
delete pruneAt->children[i];
pruneAt->children[i]=NULL;
}
}
}
else
{
// Otherwise, iterate the children and recursively call
// prune on all of them
for (int i = 0; i < 8; i++)
{
if (pruneAt->children[i])
{
this->pruneTree(pruneAt->children[i]);
}
}
}
}
int VoxelTree::readNodeData(VoxelNode *destinationNode, unsigned char * nodeData, int bytesLeftToRead) { int VoxelTree::readNodeData(VoxelNode *destinationNode, unsigned char * nodeData, int bytesLeftToRead) {
// instantiate variable for bytes already read // instantiate variable for bytes already read

View file

@ -34,7 +34,8 @@ public:
VoxelNode *currentVoxelNode, VoxelNode *currentVoxelNode,
int deepestLevel); int deepestLevel);
void printTreeForDebugging(VoxelNode *startNode); void printTreeForDebugging(VoxelNode *startNode);
void pruneTree(VoxelNode* pruneAt);
}; };
#endif /* defined(__hifi__VoxelTree__) */ #endif /* defined(__hifi__VoxelTree__) */