add groundwork for LOD specific transmissions from VS

This commit is contained in:
Stephen Birarda 2013-03-22 12:01:30 -07:00
parent 6346a14cb5
commit 8ec5298c4a
6 changed files with 96 additions and 50 deletions

View file

@ -36,6 +36,7 @@ class AgentList {
UDPSocket& getAgentSocket(); UDPSocket& getAgentSocket();
int updateList(unsigned char *packetData, size_t dataBytes); int updateList(unsigned char *packetData, size_t dataBytes);
int indexOfMatchingAgent(sockaddr *senderAddress);
bool addOrUpdateAgent(sockaddr *publicSocket, sockaddr *localSocket, char agentType); bool addOrUpdateAgent(sockaddr *publicSocket, sockaddr *localSocket, char agentType);
void processAgentData(sockaddr *senderAddress, void *packetData, size_t dataBytes); void processAgentData(sockaddr *senderAddress, void *packetData, size_t dataBytes);
void updateAgentWithData(sockaddr *senderAddress, void *packetData, size_t dataBytes); void updateAgentWithData(sockaddr *senderAddress, void *packetData, size_t dataBytes);
@ -49,7 +50,6 @@ class AgentList {
std::vector<Agent> agents; std::vector<Agent> agents;
pthread_t removeSilentAgentsThread; pthread_t removeSilentAgentsThread;
int indexOfMatchingAgent(sockaddr *senderAddress);
void handlePingReply(sockaddr *agentAddress); void handlePingReply(sockaddr *agentAddress);
}; };

View file

@ -7,6 +7,7 @@
// //
#include <cstring> #include <cstring>
#include <cmath>
#include "SharedUtil.h" #include "SharedUtil.h"
#include "OctalCode.h" #include "OctalCode.h"
#include "VoxelTree.h" #include "VoxelTree.h"
@ -27,6 +28,22 @@ VoxelTree::~VoxelTree() {
} }
} }
int VoxelTree::levelForViewerPosition(float *position) {
// get the distance to the viewer
// for now the voxel tree starts at 0,0,0
float distance = sqrtf(powf(position[0] + 30, 2) + powf(position[2] + 30, 2));
// go through the if else branch to return the right level
// this is a gross way to do this for now for a friday demo
if (distance >= 50) {
return 3;
} else if (distance >= 20) {
return 4;
} else {
return 5;
}
}
VoxelNode * VoxelTree::nodeForOctalCode(VoxelNode *ancestorNode, unsigned char * needleCode) { VoxelNode * VoxelTree::nodeForOctalCode(VoxelNode *ancestorNode, unsigned char * needleCode) {
// find the appropriate branch index based on this ancestorNode // find the appropriate branch index based on this ancestorNode
if (*needleCode == 0) { if (*needleCode == 0) {
@ -135,7 +152,8 @@ void VoxelTree::readBitstreamToTree(unsigned char * bitstream, int bufferSizeByt
unsigned char * VoxelTree::loadBitstreamBuffer(unsigned char *& bitstreamBuffer, unsigned char * VoxelTree::loadBitstreamBuffer(unsigned char *& bitstreamBuffer,
unsigned char * stopOctalCode, unsigned char * stopOctalCode,
VoxelNode *currentVoxelNode) VoxelNode *currentVoxelNode,
int deepestLevel)
{ {
static unsigned char *initialBitstreamPos = bitstreamBuffer; static unsigned char *initialBitstreamPos = bitstreamBuffer;
@ -188,7 +206,9 @@ unsigned char * VoxelTree::loadBitstreamBuffer(unsigned char *& bitstreamBuffer,
// copy the childMask to the current position of the bitstreamBuffer // copy the childMask to the current position of the bitstreamBuffer
// and push the buffer pointer forwards // and push the buffer pointer forwards
*(bitstreamBuffer++) = currentVoxelNode->childMask; *(bitstreamBuffer++) = *currentVoxelNode->octalCode < deepestLevel
? currentVoxelNode->childMask
: 0;
} else { } else {
firstIndexToCheck = *stopOctalCode > 0 firstIndexToCheck = *stopOctalCode > 0
? branchIndexWithDescendant(currentVoxelNode->octalCode, stopOctalCode) ? branchIndexWithDescendant(currentVoxelNode->octalCode, stopOctalCode)
@ -197,6 +217,7 @@ unsigned char * VoxelTree::loadBitstreamBuffer(unsigned char *& bitstreamBuffer,
unsigned char * childStopOctalCode = NULL; unsigned char * childStopOctalCode = NULL;
if (*currentVoxelNode->octalCode < deepestLevel) {
for (int i = firstIndexToCheck; i < 8; i ++) { for (int i = firstIndexToCheck; i < 8; i ++) {
// ask the child to load this bitstream buffer // ask the child to load this bitstream buffer
@ -208,7 +229,7 @@ unsigned char * VoxelTree::loadBitstreamBuffer(unsigned char *& bitstreamBuffer,
return currentVoxelNode->children[i]->octalCode; return currentVoxelNode->children[i]->octalCode;
} else { } else {
if (oneAtBit(currentVoxelNode->childMask, i)) { if (oneAtBit(currentVoxelNode->childMask, i)) {
childStopOctalCode = loadBitstreamBuffer(bitstreamBuffer, stopOctalCode, currentVoxelNode->children[i]); childStopOctalCode = loadBitstreamBuffer(bitstreamBuffer, stopOctalCode, currentVoxelNode->children[i], deepestLevel);
} else { } else {
childStopOctalCode = NULL; childStopOctalCode = NULL;
} }
@ -219,6 +240,7 @@ unsigned char * VoxelTree::loadBitstreamBuffer(unsigned char *& bitstreamBuffer,
break; break;
} }
} }
}
return childStopOctalCode; return childStopOctalCode;
} }

View file

@ -24,10 +24,12 @@ public:
VoxelNode *rootNode; VoxelNode *rootNode;
int levelForViewerPosition(float * position);
void readBitstreamToTree(unsigned char * bitstream, int bufferSizeBytes); void readBitstreamToTree(unsigned char * bitstream, int bufferSizeBytes);
unsigned char * loadBitstreamBuffer(unsigned char *& bitstreamBuffer, unsigned char * loadBitstreamBuffer(unsigned char *& bitstreamBuffer,
unsigned char * octalCode, unsigned char * octalCode,
VoxelNode *currentVoxelNode); VoxelNode *currentVoxelNode,
int deepestLevel);
void printTreeForDebugging(VoxelNode *startNode); void printTreeForDebugging(VoxelNode *startNode);
}; };

View file

@ -9,7 +9,7 @@
#include "VoxelAgentData.h" #include "VoxelAgentData.h"
VoxelAgentData::VoxelAgentData() { VoxelAgentData::VoxelAgentData() {
lastSeenLevel = 0; lastSentLevel = 0;
} }
VoxelAgentData::~VoxelAgentData() { VoxelAgentData::~VoxelAgentData() {
@ -17,7 +17,7 @@ VoxelAgentData::~VoxelAgentData() {
} }
VoxelAgentData::VoxelAgentData(const VoxelAgentData &otherAgentData) { VoxelAgentData::VoxelAgentData(const VoxelAgentData &otherAgentData) {
lastSeenLevel = otherAgentData.lastSeenLevel; lastSentLevel = otherAgentData.lastSentLevel;
memcpy(position, otherAgentData.position, sizeof(float) * 3); memcpy(position, otherAgentData.position, sizeof(float) * 3);
} }

View file

@ -15,7 +15,7 @@
class VoxelAgentData : public AgentData { class VoxelAgentData : public AgentData {
public: public:
float position[3]; float position[3];
int lastSeenLevel; int lastSentLevel;
VoxelAgentData(); VoxelAgentData();
~VoxelAgentData(); ~VoxelAgentData();

View file

@ -14,6 +14,8 @@
#include <OctalCode.h> #include <OctalCode.h>
#include <AgentList.h> #include <AgentList.h>
#include <VoxelTree.h> #include <VoxelTree.h>
#include "VoxelAgentData.h"
#include <SharedUtil.h>
#ifdef _WIN32 #ifdef _WIN32
#include "Syssocket.h" #include "Syssocket.h"
@ -41,7 +43,7 @@ char DOMAIN_HOSTNAME[] = "highfidelity.below92.com";
char DOMAIN_IP[100] = ""; // IP Address will be re-set by lookup on startup char DOMAIN_IP[100] = ""; // IP Address will be re-set by lookup on startup
const int DOMAINSERVER_PORT = 40102; const int DOMAINSERVER_PORT = 40102;
const int MAX_VOXEL_TREE_DEPTH_LEVELS = 4; const int MAX_VOXEL_TREE_DEPTH_LEVELS = 6;
AgentList agentList(VOXEL_LISTEN_PORT); AgentList agentList(VOXEL_LISTEN_PORT);
in_addr_t localAddress; in_addr_t localAddress;
@ -85,7 +87,7 @@ int randomlyFillVoxelTree(int levelsToGo, VoxelNode *currentRootNode) {
createdChildren = false; createdChildren = false;
for (int i = 0; i < 8; i++) { for (int i = 0; i < 8; i++) {
if (randomBoolean() || levelsToGo == MAX_VOXEL_TREE_DEPTH_LEVELS) { if (((i == 0 || i == 1 | i == 4 | i == 5) && (randomBoolean() || levelsToGo != 1)) ) {
// create a new VoxelNode to put here // create a new VoxelNode to put here
currentRootNode->children[i] = new VoxelNode(); currentRootNode->children[i] = new VoxelNode();
@ -129,6 +131,13 @@ int randomlyFillVoxelTree(int levelsToGo, VoxelNode *currentRootNode) {
} }
} }
void attachVoxelAgentDataToAgent(Agent *newAgent) {
if (newAgent->getLinkedData() == NULL) {
newAgent->setLinkedData(new VoxelAgentData());
}
}
int main(int argc, const char * argv[]) int main(int argc, const char * argv[])
{ {
setvbuf(stdout, NULL, _IOLBF, 0); setvbuf(stdout, NULL, _IOLBF, 0);
@ -168,6 +177,8 @@ int main(int argc, const char * argv[])
pthread_t reportAliveThread; pthread_t reportAliveThread;
pthread_create(&reportAliveThread, NULL, reportAliveToDS, NULL); pthread_create(&reportAliveThread, NULL, reportAliveToDS, NULL);
agentList.linkedDataCreateCallback = &attachVoxelAgentDataToAgent;
srand((unsigned)time(0)); srand((unsigned)time(0));
// use our method to create a random voxel tree // use our method to create a random voxel tree
@ -184,21 +195,34 @@ int main(int argc, const char * argv[])
int packetCount; int packetCount;
int totalBytesSent; int totalBytesSent;
sockaddr_in agentPublicAddress; sockaddr agentPublicAddress;
char *packetData = new char[MAX_PACKET_SIZE]; char *packetData = new char[MAX_PACKET_SIZE];
ssize_t receivedBytes; ssize_t receivedBytes;
// loop to send to agents requesting data // loop to send to agents requesting data
while (true) { while (true) {
if (agentList.getAgentSocket().receive((sockaddr *)&agentPublicAddress, packetData, &receivedBytes)) { if (agentList.getAgentSocket().receive(&agentPublicAddress, packetData, &receivedBytes)) {
if (packetData[0] == 'I') { if (packetData[0] == 'H') {
agentList.addOrUpdateAgent(&agentPublicAddress, &agentPublicAddress, packetData[0]);
agentList.updateAgentWithData(&agentPublicAddress, (void *)packetData, receivedBytes);
VoxelAgentData *agentData = (VoxelAgentData *) agentList.getAgents()[agentList.indexOfMatchingAgent(&agentPublicAddress)].getLinkedData();
int newLevel = 6;
if (newLevel > agentData->lastSentLevel) {
// the agent has already received a deeper level than this from us
// do nothing
stopOctal = randomTree.rootNode->octalCode; stopOctal = randomTree.rootNode->octalCode;
packetCount = 0; packetCount = 0;
totalBytesSent = 0;
while (stopOctal != NULL) { while (stopOctal != NULL) {
voxelPacketEnd = voxelPacket; voxelPacketEnd = voxelPacket;
stopOctal = randomTree.loadBitstreamBuffer(voxelPacketEnd, stopOctal, randomTree.rootNode); stopOctal = randomTree.loadBitstreamBuffer(voxelPacketEnd,
stopOctal,
randomTree.rootNode,
newLevel);
agentList.getAgentSocket().send((sockaddr *)&agentPublicAddress, agentList.getAgentSocket().send((sockaddr *)&agentPublicAddress,
voxelPacket, voxelPacket,
@ -208,10 +232,8 @@ int main(int argc, const char * argv[])
totalBytesSent += voxelPacketEnd - voxelPacket; totalBytesSent += voxelPacketEnd - voxelPacket;
} }
printf("%d packets sent to agent %s totalling %d bytes\n", agentData->lastSentLevel = newLevel;
packetCount, }
inet_ntoa(agentPublicAddress.sin_addr),
totalBytesSent);
} }
} }
} }