Merge branch 'master' of github.com:/worklist/hifi into 19179

This commit is contained in:
Leonardo Murillo 2013-03-25 15:13:39 -06:00
commit fc382997bd
11 changed files with 446 additions and 17 deletions

Binary file not shown.

View file

@ -8,6 +8,8 @@
#include <cstring>
#include <cmath>
#include <iostream> // to load voxels from file
#include <fstream> // to load voxels from file
#include <SharedUtil.h>
#include <OctalCode.h>
#include <AgentList.h>
@ -46,6 +48,167 @@ VoxelSystem::~VoxelSystem() {
delete tree;
}
//////////////////////////////////////////////////////////////////////////////////////////
// Method: VoxelSystem::loadVoxelsFile()
// Description: Loads HiFidelity encoded Voxels from a binary file. The current file
// format is a stream of single voxels with NO color data. Currently
// colors are set randomly
// Complaints: Brad :)
// To Do: Need to add color data to the file.
void VoxelSystem::loadVoxelsFile(char* fileName) {
std::ifstream file(fileName, std::ios::in|std::ios::binary);
char octets;
unsigned int lengthInBytes;
int totalBytesRead = 0;
if(file.is_open())
{
while (!file.eof()) {
file.get(octets);
totalBytesRead++;
lengthInBytes = (octets*3/8)+1;
unsigned char * voxelData = new unsigned char[lengthInBytes+1+3];
voxelData[0]=octets;
char byte;
for (size_t i = 0; i < lengthInBytes; i++) {
file.get(byte);
totalBytesRead++;
voxelData[i+1] = byte;
}
// random color data
voxelData[lengthInBytes+1] = randomColorValue(65);
voxelData[lengthInBytes+2] = randomColorValue(65);
voxelData[lengthInBytes+3] = randomColorValue(65);
tree->readCodeColorBufferToTree(voxelData);
delete voxelData;
}
file.close();
}
// reset the verticesEndPointer so we're writing to the beginning of the array
verticesEndPointer = verticesArray;
// call recursive function to populate in memory arrays
// it will return the number of voxels added
voxelsRendered = treeToArrays(tree->rootNode);
// set the boolean if there are any voxels to be rendered so we re-fill the VBOs
voxelsToRender = (voxelsRendered > 0);
}
//////////////////////////////////////////////////////////////////////////////////////////
// Method: VoxelSystem::createSphere()
// Description: Creates a sphere of voxels in the local system at a given location/radius
// To Do: Move this function someplace better? I put it here because we need a
// mechanism to tell the system to redraw it's arrays after voxels are done
// being added. This is a concept mostly only understood by VoxelSystem.
// Complaints: Brad :)
void VoxelSystem::createSphere(float r,float xc, float yc, float zc, float s, bool solid)
{
// About the color of the sphere... we're going to make this sphere be a gradient
// between two RGB colors. We will do the gradient along the phi spectrum
unsigned char r1 = randomColorValue(165);
unsigned char g1 = randomColorValue(165);
unsigned char b1 = randomColorValue(165);
unsigned char r2 = randomColorValue(65);
unsigned char g2 = randomColorValue(65);
unsigned char b2 = randomColorValue(65);
// we don't want them to match!!
if (r1==r2 && g1==g2 && b1==b2)
{
r2=r1/2;
g2=g1/2;
b2=b1/2;
}
/**
std::cout << "creatSphere COLORS ";
std::cout << " r1=" << (int)r1;
std::cout << " g1=" << (int)g1;
std::cout << " b1=" << (int)b1;
std::cout << " r2=" << (int)r2;
std::cout << " g2=" << (int)g2;
std::cout << " b2=" << (int)b2;
std::cout << std::endl;
**/
// Psuedocode for creating a sphere:
//
// for (theta from 0 to 2pi):
// for (phi from 0 to pi):
// x = xc+r*cos(theta)*sin(phi)
// y = yc+r*sin(theta)*sin(phi)
// z = zc+r*cos(phi)
int t=0; // total points
// We want to make sure that as we "sweep" through our angles
// we use a delta angle that's small enough to not skip any voxels
// we can calculate theta from our desired arc length
//
// lenArc = ndeg/360deg * 2pi*R
// lenArc = theta/2pi * 2pi*R
// lenArc = theta*R
// theta = lenArc/R
// theta = g/r
float angleDelta = (s/r);
// assume solid for now
float ri = 0.0;
if (!solid)
{
ri=r; // just the outer surface
}
// If you also iterate form the interior of the sphere to the radius, makeing
// larger and larger sphere's you'd end up with a solid sphere. And lots of voxels!
for (; ri <= r; ri+=s)
{
for (float theta=0.0; theta <= 2*M_PI; theta += angleDelta)
{
for (float phi=0.0; phi <= M_PI; phi += angleDelta)
{
t++; // total voxels
float x = xc+r*cos(theta)*sin(phi);
float y = yc+r*sin(theta)*sin(phi);
float z = zc+r*cos(phi);
/*
std::cout << " r=" << r;
std::cout << " theta=" << theta;
std::cout << " phi=" << phi;
std::cout << " x=" << x;
std::cout << " y=" << y;
std::cout << " z=" << z;
std::cout << " t=" << t;
std::cout << std::endl;
*/
// gradient color data
float gradient = (phi/M_PI);
unsigned char red = r1+((r2-r1)*gradient);
unsigned char green = g1+((g2-g1)*gradient);
unsigned char blue = b1+((b2-b1)*gradient);
unsigned char* voxelData = pointToVoxel(x,y,z,s,red,green,blue);
tree->readCodeColorBufferToTree(voxelData);
delete voxelData;
}
}
}
// reset the verticesEndPointer so we're writing to the beginning of the array
verticesEndPointer = verticesArray;
// call recursive function to populate in memory arrays
// it will return the number of voxels added
voxelsRendered = treeToArrays(tree->rootNode);
// set the boolean if there are any voxels to be rendered so we re-fill the VBOs
voxelsToRender = (voxelsRendered > 0);
}
void VoxelSystem::parseData(void *data, int size) {
// output the bits received from the voxel server
unsigned char *voxelData = (unsigned char *) data + 1;
@ -68,7 +231,7 @@ void VoxelSystem::parseData(void *data, int size) {
int VoxelSystem::treeToArrays(VoxelNode *currentNode) {
int voxelsAdded = 0;
for (int i = 0; i < 8; i++) {
// check if there is a child here
if (currentNode->children[i] != NULL) {
@ -95,7 +258,7 @@ int VoxelSystem::treeToArrays(VoxelNode *currentNode) {
delete [] startVertex;
}
return voxelsAdded;
}
@ -191,3 +354,4 @@ void VoxelSystem::simulate(float deltaTime) {
}

View file

@ -33,6 +33,8 @@ public:
void render();
void setVoxelsRendered(int v) {voxelsRendered = v;};
int getVoxelsRendered() {return voxelsRendered;};
void loadVoxelsFile(char* fileName);
void createSphere(float r,float xc, float yc, float zc, float s, bool solid);
private:
int voxelsRendered;
VoxelTree *tree;

View file

@ -53,6 +53,7 @@
#include "Oscilloscope.h"
#include "UDPSocket.h"
#include "SerialInterface.h"
#include <SharedUtil.h>
using namespace std;
@ -324,7 +325,6 @@ void initDisplay(void)
void init(void)
{
voxels.init();
myHead.setRenderYaw(start_yaw);
head_mouse_x = WIDTH/2;
@ -692,6 +692,45 @@ void display(void)
framecount++;
}
void testPointToVoxel()
{
float y=0;
float z=0;
float s=0.1;
for (float x=0; x<=1; x+= 0.05)
{
std::cout << " x=" << x << " ";
unsigned char red = 200; //randomColorValue(65);
unsigned char green = 200; //randomColorValue(65);
unsigned char blue = 200; //randomColorValue(65);
unsigned char* voxelCode = pointToVoxel(x, y, z, s,red,green,blue);
printVoxelCode(voxelCode);
delete voxelCode;
std::cout << std::endl;
}
}
void addRandomSphere()
{
float r = randFloatInRange(0.05,0.1);
float xc = randFloatInRange(r,(1-r));
float yc = randFloatInRange(r,(1-r));
float zc = randFloatInRange(r,(1-r));
float s = 0.001; // size of voxels to make up surface of sphere
bool solid = false;
printf("random sphere\n");
printf("radius=%f\n",r);
printf("xc=%f\n",xc);
printf("yc=%f\n",yc);
printf("zc=%f\n",zc);
voxels.createSphere(r,xc,yc,zc,s,solid);
}
const float KEYBOARD_YAW_RATE = 0.8;
const float KEYBOARD_STRAFE_RATE = 0.03;
const float KEYBOARD_FLY_RATE = 0.08;
@ -766,6 +805,13 @@ void key(unsigned char k, int x, int y)
{
myHead.SetNewHeadTarget((randFloat()-0.5)*20.0, (randFloat()-0.5)*20.0);
}
// press the . key to get a new random sphere of voxels added
if (k == '.')
{
addRandomSphere();
//testPointToVoxel();
}
}
//
@ -969,6 +1015,14 @@ int main(int argc, char** argv)
printf( "Initialized Display.\n" );
init();
// Check to see if the user passed in a command line option for loading a local
// Voxel File. If so, load it now.
char* voxelsFilename = getCmdOption(argv, argv + argc, "-i");
if (voxelsFilename)
{
voxels.loadVoxelsFile(voxelsFilename);
}
// create thread for receipt of data via UDP
pthread_create(&networkReceiveThread, NULL, networkReceive, NULL);

View file

@ -43,7 +43,7 @@ int bytesRequiredForCodeLength(unsigned char threeBitCodes) {
}
}
char branchIndexWithDescendant(unsigned char * ancestorOctalCode, unsigned char * descendantOctalCode) {
int branchIndexWithDescendant(unsigned char * ancestorOctalCode, unsigned char * descendantOctalCode) {
int parentSections = numberOfThreeBitSectionsInCode(ancestorOctalCode);
int branchStartBit = parentSections * 3;

View file

@ -14,7 +14,7 @@
void printOctalCode(unsigned char * octalCode);
int bytesRequiredForCodeLength(unsigned char threeBitCodes);
bool isDirectParentOfChild(unsigned char *parentOctalCode, unsigned char * childOctalCode);
char branchIndexWithDescendant(unsigned char * ancestorOctalCode, unsigned char * descendantOctalCode);
int branchIndexWithDescendant(unsigned char * ancestorOctalCode, unsigned char * descendantOctalCode);
unsigned char * childOctalCode(unsigned char * parentOctalCode, char childNumber);
float * firstVertexForCode(unsigned char * octalCode);

View file

@ -28,6 +28,10 @@ float randFloat () {
return (rand() % 10000)/10000.f;
}
float randFloatInRange (float min,float max) {
return min + ((rand() % 10000)/10000.f * (max-min));
}
unsigned char randomColorValue(int miniumum) {
return miniumum + (rand() % (255 - miniumum));
}
@ -73,4 +77,193 @@ void switchToResourcesIfRequired() {
chdir(path);
#endif
}
}
//////////////////////////////////////////////////////////////////////////////////////////
// Function: getCmdOption()
// Description: Handy little function to tell you if a command line flag and option was
// included while launching the application, and to get the option value
// immediately following the flag. For example if you ran:
// ./app -i filename.txt
// then you're using the "-i" flag to set the input file name.
// Usage: char * inputFilename = getCmdOption(argv, argv + argc, "-i");
// Complaints: Brad :)
char* getCmdOption(char ** begin, char ** end, const std::string & option)
{
char ** itr = std::find(begin, end, option);
if (itr != end && ++itr != end)
{
return *itr;
}
return 0;
}
//////////////////////////////////////////////////////////////////////////////////////////
// Function: getCmdOption()
// Description: Handy little function to tell you if a command line option flag was
// included while launching the application. Returns bool true/false
// Usage: bool wantDump = cmdOptionExists(argv, argv+argc, "-d");
// Complaints: Brad :)
bool cmdOptionExists(char** begin, char** end, const std::string& option)
{
return std::find(begin, end, option) != end;
}
//////////////////////////////////////////////////////////////////////////////////////////
// Function: pointToVoxel()
// Description: Given a universal point with location x,y,z this will return the voxel
// voxel code corresponding to the closest voxel which encloses a cube with
// lower corners at x,y,z, having side of length S.
// The input values x,y,z range 0.0 <= v < 1.0
// TO DO: This code is not very DRY. It should be cleaned up to be DRYer.
// IMPORTANT: The voxel is returned to you a buffer which you MUST delete when you are
// done with it.
// Usage:
// unsigned char* voxelData = pointToVoxel(x,y,z,s,red,green,blue);
// tree->readCodeColorBufferToTree(voxelData);
// delete voxelData;
//
// Complaints: Brad :)
unsigned char* pointToVoxel(float x, float y, float z, float s, unsigned char r, unsigned char g, unsigned char b ) {
float xTest, yTest, zTest, sTest;
xTest = yTest = zTest = sTest = 0.5;
// First determine the voxelSize that will properly encode a
// voxel of size S.
int voxelSizeInBits = 0;
while (sTest > s) {
sTest /= 2.0;
voxelSizeInBits+=3;
}
unsigned int voxelSizeInBytes = (voxelSizeInBits/8)+1;
unsigned int voxelSizeInOctets = (voxelSizeInBits/3);
unsigned int voxelBufferSize = voxelSizeInBytes+1+3; // 1 for size, 3 for color
// allocate our resulting buffer
unsigned char* voxelOut = new unsigned char[voxelBufferSize];
// first byte of buffer is always our size in octets
voxelOut[0]=voxelSizeInOctets;
sTest = 0.5; // reset sTest so we can do this again.
unsigned char byte = 0; // we will be adding coding bits here
int bitInByteNDX = 0; // keep track of where we are in byte as we go
int byteNDX = 1; // keep track of where we are in buffer of bytes as we go
int octetsDone = 0;
// Now we actually fill out the voxel code
while (octetsDone < voxelSizeInOctets) {
if (x > xTest) {
//<write 1 bit>
byte = (byte << 1) | true;
xTest += sTest/2.0;
}
else {
//<write 0 bit;>
byte = (byte << 1) | false;
xTest -= sTest/2.0;
}
bitInByteNDX++;
// If we've reached the last bit of the byte, then we want to copy this byte
// into our buffer. And get ready to start on a new byte
if (bitInByteNDX > 7)
{
voxelOut[byteNDX]=byte;
byteNDX++;
bitInByteNDX=0;
byte=0;
}
if (y > yTest) {
//<write 1 bit>
byte = (byte << 1) | true;
yTest += sTest/2.0;
}
else {
//<write 0 bit;>
byte = (byte << 1) | false;
yTest -= sTest/2.0;
}
bitInByteNDX++;
// If we've reached the last bit of the byte, then we want to copy this byte
// into our buffer. And get ready to start on a new byte
if (bitInByteNDX > 7)
{
voxelOut[byteNDX]=byte;
byteNDX++;
bitInByteNDX=0;
byte=0;
}
if (z > zTest) {
//<write 1 bit>
byte = (byte << 1) | true;
zTest += sTest/2.0;
}
else {
//<write 0 bit;>
byte = (byte << 1) | false;
zTest -= sTest/2.0;
}
bitInByteNDX++;
// If we've reached the last bit of the byte, then we want to copy this byte
// into our buffer. And get ready to start on a new byte
if (bitInByteNDX > 7)
{
voxelOut[byteNDX]=byte;
byteNDX++;
bitInByteNDX=0;
byte=0;
}
octetsDone++;
sTest /= 2.0;
}
// If we've got here, and we didn't fill the last byte, we need to zero pad this
// byte before we copy it into our buffer.
if (bitInByteNDX > 0 && bitInByteNDX < 7)
{
// Pad the last byte
while (bitInByteNDX <= 7)
{
byte = (byte << 1) | false;
bitInByteNDX++;
}
// Copy it into our output buffer
voxelOut[byteNDX]=byte;
byteNDX++;
}
// copy color data
voxelOut[byteNDX]=r;
voxelOut[byteNDX+1]=g;
voxelOut[byteNDX+2]=b;
return voxelOut;
}
void printVoxelCode(unsigned char* voxelCode)
{
unsigned char octets = voxelCode[0];
unsigned int voxelSizeInBits = octets*3;
unsigned int voxelSizeInBytes = (voxelSizeInBits/8)+1;
unsigned int voxelSizeInOctets = (voxelSizeInBits/3);
unsigned int voxelBufferSize = voxelSizeInBytes+1+3; // 1 for size, 3 for color
printf("octets=%d\n",octets);
printf("voxelSizeInBits=%d\n",voxelSizeInBits);
printf("voxelSizeInBytes=%d\n",voxelSizeInBytes);
printf("voxelSizeInOctets=%d\n",voxelSizeInOctets);
printf("voxelBufferSize=%d\n",voxelBufferSize);
for(int i=0;i<voxelBufferSize;i++)
{
printf("i=%d ",i);
outputBits(voxelCode[i]);
}
}

View file

@ -22,13 +22,20 @@ double usecTimestamp(timeval *time);
double usecTimestampNow();
float randFloat();
float randFloatInRange (float min,float max);
unsigned char randomColorValue(int minimum);
bool randomBoolean();
void outputBits(unsigned char byte);
void printVoxelCode(unsigned char* voxelCode);
int numberOfOnes(unsigned char byte);
bool oneAtBit(unsigned char byte, int bitIndex);
void switchToResourcesIfRequired();
char* getCmdOption(char ** begin, char ** end, const std::string& option);
bool cmdOptionExists(char** begin, char** end, const std::string& option);
unsigned char* pointToVoxel(float x, float y, float z, float s, unsigned char r, unsigned char g, unsigned char b );
#endif /* defined(__hifi__SharedUtil__) */

View file

@ -12,8 +12,6 @@
#include "OctalCode.h"
#include "VoxelTree.h"
const int MAX_TREE_SLICE_BYTES = 26;
VoxelTree::VoxelTree() {
rootNode = new VoxelNode();
rootNode->octalCode = new unsigned char[1];
@ -46,9 +44,7 @@ int VoxelTree::levelForViewerPosition(float *position) {
VoxelNode * VoxelTree::nodeForOctalCode(VoxelNode *ancestorNode, unsigned char * needleCode) {
// find the appropriate branch index based on this ancestorNode
if (*needleCode == 0) {
return ancestorNode;
} else {
if (*needleCode > 0) {
int branchForNeedle = branchIndexWithDescendant(ancestorNode->octalCode, needleCode);
VoxelNode *childNode = ancestorNode->children[branchForNeedle];
@ -156,7 +152,8 @@ void VoxelTree::readCodeColorBufferToTree(unsigned char *codeColorBuffer) {
// create the node if it does not exist
if (*lastCreatedNode->octalCode != *codeColorBuffer) {
lastCreatedNode = createMissingNode(lastCreatedNode, codeColorBuffer);
VoxelNode *parentNode = createMissingNode(lastCreatedNode, codeColorBuffer);
lastCreatedNode = parentNode->children[branchIndexWithDescendant(parentNode->octalCode, codeColorBuffer)];
}
// give this node its color
@ -220,7 +217,7 @@ unsigned char * VoxelTree::loadBitstreamBuffer(unsigned char *& bitstreamBuffer,
// copy the childMask to the current position of the bitstreamBuffer
// and push the buffer pointer forwards
*(bitstreamBuffer++) = *currentVoxelNode->octalCode < deepestLevel
*(bitstreamBuffer++) = *currentVoxelNode->octalCode < deepestLevel - 1
? currentVoxelNode->childMask
: 0;
} else {
@ -231,7 +228,11 @@ unsigned char * VoxelTree::loadBitstreamBuffer(unsigned char *& bitstreamBuffer,
unsigned char * childStopOctalCode = NULL;
if (*currentVoxelNode->octalCode < deepestLevel) {
if (currentVoxelNode->childMask == 0) {
leavesWrittenToBitstream++;
}
if (*currentVoxelNode->octalCode < deepestLevel - 1) {
for (int i = firstIndexToCheck; i < 8; i ++) {
// ask the child to load this bitstream buffer
@ -285,7 +286,7 @@ void VoxelTree::printTreeForDebugging(VoxelNode *startNode) {
// ask children to recursively output their trees
// if they aren't a leaf
for (int k = 0; k < 8; k++) {
if (startNode->children[k] != NULL && oneAtBit(startNode->childMask, k)) {
if (startNode->children[k] != NULL) {
printTreeForDebugging(startNode->children[k]);
}
}

View file

@ -13,6 +13,7 @@
#include "VoxelNode.h"
const int MAX_VOXEL_PACKET_SIZE = 1492;
const int MAX_TREE_SLICE_BYTES = 26;
class VoxelTree {
VoxelNode * nodeForOctalCode(VoxelNode *ancestorNode, unsigned char * needleCode);
@ -23,6 +24,7 @@ public:
~VoxelTree();
VoxelNode *rootNode;
int leavesWrittenToBitstream;
int levelForViewerPosition(float * position);
void readBitstreamToTree(unsigned char * bitstream, int bufferSizeBytes);

View file

@ -43,7 +43,7 @@ char DOMAIN_HOSTNAME[] = "highfidelity.below92.com";
char DOMAIN_IP[100] = ""; // IP Address will be re-set by lookup on startup
const int DOMAINSERVER_PORT = 40102;
const int MAX_VOXEL_TREE_DEPTH_LEVELS = 6;
const int MAX_VOXEL_TREE_DEPTH_LEVELS = 10;
AgentList agentList(VOXEL_LISTEN_PORT);
in_addr_t localAddress;
@ -209,7 +209,7 @@ int main(int argc, const char * argv[])
agentList.updateAgentWithData(&agentPublicAddress, (void *)packetData, receivedBytes);
VoxelAgentData *agentData = (VoxelAgentData *) agentList.getAgents()[agentList.indexOfMatchingAgent(&agentPublicAddress)].getLinkedData();
int newLevel = 6;
int newLevel = 10;
if (newLevel > agentData->lastSentLevel) {
// the agent has already received a deeper level than this from us
// do nothing
@ -217,6 +217,7 @@ int main(int argc, const char * argv[])
stopOctal = randomTree.rootNode->octalCode;
packetCount = 0;
totalBytesSent = 0;
randomTree.leavesWrittenToBitstream = 0;
while (stopOctal != NULL) {
voxelPacketEnd = voxelPacket;
@ -233,6 +234,11 @@ int main(int argc, const char * argv[])
totalBytesSent += voxelPacketEnd - voxelPacket;
}
printf("%d packets sent to client totalling %d bytes\n", packetCount, totalBytesSent);
printf("%d leaves were sent - %f bpv\n",
randomTree.leavesWrittenToBitstream,
(float)totalBytesSent / randomTree.leavesWrittenToBitstream);
agentData->lastSentLevel = newLevel;
}
}