From 9e0df2dbd15ac1bbe36d4132b4972be4019c9de8 Mon Sep 17 00:00:00 2001 From: Leonardo Murillo Date: Mon, 18 Mar 2013 17:10:05 -0600 Subject: [PATCH 01/17] Add unique id to agents and broadcast back --- domain/src/main.cpp | 1 + shared/src/Agent.cpp | 11 ++++++++++- shared/src/Agent.h | 5 ++++- shared/src/AgentList.cpp | 5 ++++- shared/src/AgentList.h | 2 +- 5 files changed, 20 insertions(+), 4 deletions(-) diff --git a/domain/src/main.cpp b/domain/src/main.cpp index e32434c543..8bdd0a736b 100644 --- a/domain/src/main.cpp +++ b/domain/src/main.cpp @@ -44,6 +44,7 @@ AgentList agentList(DOMAIN_LISTEN_PORT); unsigned char * addAgentToBroadcastPacket(unsigned char *currentPosition, Agent *agentToAdd) { *currentPosition++ = agentToAdd->getType(); + currentPosition += agentToAdd->getAgentId(); currentPosition += packSocket(currentPosition, agentToAdd->getPublicSocket()); currentPosition += packSocket(currentPosition, agentToAdd->getLocalSocket()); diff --git a/shared/src/Agent.cpp b/shared/src/Agent.cpp index cacb3a5f94..f48d139eae 100644 --- a/shared/src/Agent.cpp +++ b/shared/src/Agent.cpp @@ -14,7 +14,7 @@ Agent::Agent() {} -Agent::Agent(sockaddr *agentPublicSocket, sockaddr *agentLocalSocket, char agentType) { +Agent::Agent(sockaddr *agentPublicSocket, sockaddr *agentLocalSocket, char agentType, int16_t thisAgentId) { publicSocket = new sockaddr; memcpy(publicSocket, agentPublicSocket, sizeof(sockaddr)); @@ -22,6 +22,7 @@ Agent::Agent(sockaddr *agentPublicSocket, sockaddr *agentLocalSocket, char agent memcpy(localSocket, agentLocalSocket, sizeof(sockaddr)); type = agentType; + agentId = thisAgentId; firstRecvTimeUsecs = usecTimestampNow(); lastRecvTimeUsecs = usecTimestampNow(); @@ -75,6 +76,14 @@ void Agent::setType(char newType) { type = newType; } +int16_t Agent::getAgentId() { + return agentId; +} + +void Agent::setAgentId(int16_t thisAgentId) { + agentId = thisAgentId; +} + double Agent::getFirstRecvTimeUsecs() { return firstRecvTimeUsecs; } diff --git a/shared/src/Agent.h b/shared/src/Agent.h index dafbeb768f..edb8f67f50 100644 --- a/shared/src/Agent.h +++ b/shared/src/Agent.h @@ -16,7 +16,7 @@ class Agent { public: Agent(); - Agent(sockaddr *agentPublicSocket, sockaddr *agentLocalSocket, char agentType); + Agent(sockaddr *agentPublicSocket, sockaddr *agentLocalSocket, char agentType, int16_t thisAgentId); Agent(const Agent &otherAgent); ~Agent(); Agent& operator=(Agent otherAgent); @@ -25,6 +25,8 @@ class Agent { bool matches(sockaddr *otherPublicSocket, sockaddr *otherLocalSocket, char otherAgentType); char getType(); void setType(char newType); + int16_t getAgentId(); + void setAgentId(int16_t thisAgentId); double getFirstRecvTimeUsecs(); void setFirstRecvTimeUsecs(double newTimeUsecs); double getLastRecvTimeUsecs(); @@ -44,6 +46,7 @@ class Agent { void swap(Agent &first, Agent &second); sockaddr *publicSocket, *localSocket, *activeSocket; char type; + int16_t agentId; double firstRecvTimeUsecs; double lastRecvTimeUsecs; AgentData *linkedData; diff --git a/shared/src/AgentList.cpp b/shared/src/AgentList.cpp index 0bdedb10cd..4fc27b498b 100644 --- a/shared/src/AgentList.cpp +++ b/shared/src/AgentList.cpp @@ -20,12 +20,14 @@ AgentList::AgentList() : agentSocket(AGENT_SOCKET_LISTEN_PORT) { linkedDataCreateCallback = NULL; audioMixerSocketUpdate = NULL; voxelServerAddCallback = NULL; + lastAgentId = 0; } AgentList::AgentList(int socketListenPort) : agentSocket(socketListenPort) { linkedDataCreateCallback = NULL; audioMixerSocketUpdate = NULL; voxelServerAddCallback = NULL; + lastAgentId = 0; } AgentList::~AgentList() { @@ -139,7 +141,8 @@ bool AgentList::addOrUpdateAgent(sockaddr *publicSocket, sockaddr *localSocket, if (agent == agents.end()) { // we didn't have this agent, so add them - Agent newAgent = Agent(publicSocket, localSocket, agentType); + Agent newAgent = Agent(publicSocket, localSocket, agentType, this->lastAgentId); + ++this->lastAgentId; if (socketMatch(publicSocket, localSocket)) { // likely debugging scenario with DS + agent on local network diff --git a/shared/src/AgentList.h b/shared/src/AgentList.h index d4c9b69d23..d1c607f066 100644 --- a/shared/src/AgentList.h +++ b/shared/src/AgentList.h @@ -45,7 +45,7 @@ class AgentList { UDPSocket agentSocket; std::vector agents; pthread_t removeSilentAgentsThread; - + int16_t lastAgentId; int indexOfMatchingAgent(sockaddr *senderAddress); void handlePingReply(sockaddr *agentAddress); }; From e396269d78bb032525850065196e96488dd3adb1 Mon Sep 17 00:00:00 2001 From: Leonardo Murillo Date: Tue, 19 Mar 2013 11:02:47 -0600 Subject: [PATCH 02/17] Switching to unsigned --- shared/src/Agent.cpp | 6 +++--- shared/src/Agent.h | 8 ++++---- shared/src/AgentList.h | 2 +- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/shared/src/Agent.cpp b/shared/src/Agent.cpp index f48d139eae..2c0d6b8927 100644 --- a/shared/src/Agent.cpp +++ b/shared/src/Agent.cpp @@ -14,7 +14,7 @@ Agent::Agent() {} -Agent::Agent(sockaddr *agentPublicSocket, sockaddr *agentLocalSocket, char agentType, int16_t thisAgentId) { +Agent::Agent(sockaddr *agentPublicSocket, sockaddr *agentLocalSocket, char agentType, uint16_t thisAgentId) { publicSocket = new sockaddr; memcpy(publicSocket, agentPublicSocket, sizeof(sockaddr)); @@ -76,11 +76,11 @@ void Agent::setType(char newType) { type = newType; } -int16_t Agent::getAgentId() { +uint16_t Agent::getAgentId() { return agentId; } -void Agent::setAgentId(int16_t thisAgentId) { +void Agent::setAgentId(uint16_t thisAgentId) { agentId = thisAgentId; } diff --git a/shared/src/Agent.h b/shared/src/Agent.h index edb8f67f50..61f01b4b60 100644 --- a/shared/src/Agent.h +++ b/shared/src/Agent.h @@ -16,7 +16,7 @@ class Agent { public: Agent(); - Agent(sockaddr *agentPublicSocket, sockaddr *agentLocalSocket, char agentType, int16_t thisAgentId); + Agent(sockaddr *agentPublicSocket, sockaddr *agentLocalSocket, char agentType, uint16_t thisAgentId); Agent(const Agent &otherAgent); ~Agent(); Agent& operator=(Agent otherAgent); @@ -25,8 +25,8 @@ class Agent { bool matches(sockaddr *otherPublicSocket, sockaddr *otherLocalSocket, char otherAgentType); char getType(); void setType(char newType); - int16_t getAgentId(); - void setAgentId(int16_t thisAgentId); + uint16_t getAgentId(); + void setAgentId(uint16_t thisAgentId); double getFirstRecvTimeUsecs(); void setFirstRecvTimeUsecs(double newTimeUsecs); double getLastRecvTimeUsecs(); @@ -46,7 +46,7 @@ class Agent { void swap(Agent &first, Agent &second); sockaddr *publicSocket, *localSocket, *activeSocket; char type; - int16_t agentId; + uint16_t agentId; double firstRecvTimeUsecs; double lastRecvTimeUsecs; AgentData *linkedData; diff --git a/shared/src/AgentList.h b/shared/src/AgentList.h index d1c607f066..b7e12c9627 100644 --- a/shared/src/AgentList.h +++ b/shared/src/AgentList.h @@ -45,7 +45,7 @@ class AgentList { UDPSocket agentSocket; std::vector agents; pthread_t removeSilentAgentsThread; - int16_t lastAgentId; + uint16_t lastAgentId; int indexOfMatchingAgent(sockaddr *senderAddress); void handlePingReply(sockaddr *agentAddress); }; From a76f8a46cde507d172be1ea9ba42cf7f0a6f251a Mon Sep 17 00:00:00 2001 From: Leonardo Murillo Date: Fri, 22 Mar 2013 11:20:45 -0600 Subject: [PATCH 03/17] Handle new and existing agent Ids --- domain/src/main.cpp | 2 ++ shared/src/Agent.cpp | 4 ++++ shared/src/Agent.h | 1 + shared/src/AgentList.cpp | 25 +++++++++++++++++++++---- shared/src/AgentList.h | 6 ++++-- shared/src/UDPSocket.cpp | 2 +- 6 files changed, 33 insertions(+), 7 deletions(-) diff --git a/domain/src/main.cpp b/domain/src/main.cpp index cc51bb79b6..05fb088329 100644 --- a/domain/src/main.cpp +++ b/domain/src/main.cpp @@ -50,7 +50,9 @@ AgentList agentList(DOMAIN_LISTEN_PORT); unsigned char * addAgentToBroadcastPacket(unsigned char *currentPosition, Agent *agentToAdd) { *currentPosition++ = agentToAdd->getType(); + // FIX THIS - NOT ONE BYTE currentPosition += agentToAdd->getAgentId(); + // --- currentPosition += packSocket(currentPosition, agentToAdd->getPublicSocket()); currentPosition += packSocket(currentPosition, agentToAdd->getLocalSocket()); diff --git a/shared/src/Agent.cpp b/shared/src/Agent.cpp index b95c3e7dbd..269703260f 100644 --- a/shared/src/Agent.cpp +++ b/shared/src/Agent.cpp @@ -162,6 +162,10 @@ bool Agent::matches(sockaddr *otherPublicSocket, sockaddr *otherLocalSocket, cha && socketMatch(localSocket, otherLocalSocket); } +bool Agent::exists(uint16_t *otherAgentId) { + return agentId == *otherAgentId; +} + std::ostream& operator<<(std::ostream& os, const Agent* agent) { sockaddr_in *agentPublicSocket = (sockaddr_in *)agent->publicSocket; sockaddr_in *agentLocalSocket = (sockaddr_in *)agent->localSocket; diff --git a/shared/src/Agent.h b/shared/src/Agent.h index 087d828b49..e5fa583f20 100644 --- a/shared/src/Agent.h +++ b/shared/src/Agent.h @@ -28,6 +28,7 @@ class Agent { bool operator==(const Agent& otherAgent); bool matches(sockaddr *otherPublicSocket, sockaddr *otherLocalSocket, char otherAgentType); + bool exists(uint16_t *agentId); char getType(); void setType(char newType); uint16_t getAgentId(); diff --git a/shared/src/AgentList.cpp b/shared/src/AgentList.cpp index 13869f53af..134672e971 100644 --- a/shared/src/AgentList.cpp +++ b/shared/src/AgentList.cpp @@ -109,10 +109,16 @@ int AgentList::indexOfMatchingAgent(sockaddr *senderAddress) { return -1; } +int AgentList::unpackAgentId(unsigned char *packedData, uint16_t *agentId) { + memcpy(packedData, agentId, sizeof(uint16_t)); + return sizeof(uint16_t); +} + int AgentList::updateList(unsigned char *packetData, size_t dataBytes) { int readAgents = 0; char agentType; + uint16_t agentId; // assumes only IPv4 addresses sockaddr_in agentPublicSocket; @@ -125,17 +131,21 @@ int AgentList::updateList(unsigned char *packetData, size_t dataBytes) { while((readPtr - startPtr) < dataBytes) { agentType = *readPtr++; + readPtr += unpackAgentId(readPtr, (uint16_t *)&agentId); readPtr += unpackSocket(readPtr, (sockaddr *)&agentPublicSocket); readPtr += unpackSocket(readPtr, (sockaddr *)&agentLocalSocket); - addOrUpdateAgent((sockaddr *)&agentPublicSocket, (sockaddr *)&agentLocalSocket, agentType); + //syncClientAgentList(agentId, (sockaddr *)&agentPublicSocket, (sockaddr *)&agentLocalSocket, agentType); + + addOrUpdateAgent((sockaddr *)&agentPublicSocket, (sockaddr *)&agentLocalSocket, agentType, agentId); } return readAgents; } -bool AgentList::addOrUpdateAgent(sockaddr *publicSocket, sockaddr *localSocket, char agentType) { +bool AgentList::addOrUpdateAgent(sockaddr *publicSocket, sockaddr *localSocket, char agentType, uint16_t agentId = 0) { std::vector::iterator agent; + uint16_t thisAgentId; for(agent = agents.begin(); agent != agents.end(); agent++) { if (agent->matches(publicSocket, localSocket, agentType)) { @@ -146,8 +156,15 @@ bool AgentList::addOrUpdateAgent(sockaddr *publicSocket, sockaddr *localSocket, if (agent == agents.end()) { // we didn't have this agent, so add them - Agent newAgent = Agent(publicSocket, localSocket, agentType, this->lastAgentId); - ++this->lastAgentId; + + if (agentId == 0) { + thisAgentId = this->lastAgentId; + ++this->lastAgentId; + } else { + thisAgentId = agentId; + } + + Agent newAgent = Agent(publicSocket, localSocket, agentType, thisAgentId); if (socketMatch(publicSocket, localSocket)) { // likely debugging scenario with DS + agent on local network diff --git a/shared/src/AgentList.h b/shared/src/AgentList.h index e152815ad1..9d6e9991dc 100644 --- a/shared/src/AgentList.h +++ b/shared/src/AgentList.h @@ -35,9 +35,11 @@ class AgentList { std::vector& getAgents(); UDPSocket& getAgentSocket(); - + int updateList(unsigned char *packetData, size_t dataBytes); - bool addOrUpdateAgent(sockaddr *publicSocket, sockaddr *localSocket, char agentType); + bool addOrUpdateAgent(sockaddr *publicSocket, sockaddr *localSocket, char agentType, uint16_t agentId); + int unpackAgentId(unsigned char *packedData, uint16_t *agentId); + int packAgentId(unsigned char *packStore, uint16_t agentId); void processAgentData(sockaddr *senderAddress, void *packetData, size_t dataBytes); void updateAgentWithData(sockaddr *senderAddress, void *packetData, size_t dataBytes); void broadcastToAgents(char *broadcastData, size_t dataBytes); diff --git a/shared/src/UDPSocket.cpp b/shared/src/UDPSocket.cpp index f4bceb884f..436b627045 100644 --- a/shared/src/UDPSocket.cpp +++ b/shared/src/UDPSocket.cpp @@ -115,7 +115,7 @@ bool UDPSocket::receive(sockaddr *recvAddress, void *receivedData, ssize_t *rece socklen_t addressSize = sizeof(&recvAddress); *receivedBytes = recvfrom(handle, static_cast(receivedData), MAX_BUFFER_LENGTH_BYTES, - 0, recvAddress, reinterpret_cast(&addressSize)); + 0, recvAddress, &addressSize); return (*receivedBytes > 0); } From a1691f54a1fa574e76037c0f9f977a77d97b69d8 Mon Sep 17 00:00:00 2001 From: Leonardo Murillo Date: Fri, 22 Mar 2013 11:30:17 -0600 Subject: [PATCH 04/17] Starting agent count at 1 to allow for 0 to be non-id specifier and packing the id into boadcast packet --- shared/src/AgentList.cpp | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/shared/src/AgentList.cpp b/shared/src/AgentList.cpp index 134672e971..8cae9547bd 100644 --- a/shared/src/AgentList.cpp +++ b/shared/src/AgentList.cpp @@ -25,14 +25,14 @@ AgentList::AgentList() : agentSocket(AGENT_SOCKET_LISTEN_PORT) { linkedDataCreateCallback = NULL; audioMixerSocketUpdate = NULL; voxelServerAddCallback = NULL; - lastAgentId = 0; + lastAgentId = 1; } AgentList::AgentList(int socketListenPort) : agentSocket(socketListenPort) { linkedDataCreateCallback = NULL; audioMixerSocketUpdate = NULL; voxelServerAddCallback = NULL; - lastAgentId = 0; + lastAgentId = 1; } AgentList::~AgentList() { @@ -114,6 +114,11 @@ int AgentList::unpackAgentId(unsigned char *packedData, uint16_t *agentId) { return sizeof(uint16_t); } +int AgentList::packAgentId(unsigned char *packStore, uint16_t agentId) { + memcpy(&agentId, packStore, sizeof(uint16_t)); + return sizeof(uint16_t); +} + int AgentList::updateList(unsigned char *packetData, size_t dataBytes) { int readAgents = 0; From e3d842dff7b12a4b7af93fd96fe3107372f76265 Mon Sep 17 00:00:00 2001 From: Leonardo Murillo Date: Fri, 22 Mar 2013 11:43:16 -0600 Subject: [PATCH 05/17] Packing agent ID on domain broadcast packet --- domain/src/main.cpp | 4 +--- shared/src/AgentList.h | 5 +++-- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/domain/src/main.cpp b/domain/src/main.cpp index 05fb088329..d80864eaa0 100644 --- a/domain/src/main.cpp +++ b/domain/src/main.cpp @@ -50,9 +50,7 @@ AgentList agentList(DOMAIN_LISTEN_PORT); unsigned char * addAgentToBroadcastPacket(unsigned char *currentPosition, Agent *agentToAdd) { *currentPosition++ = agentToAdd->getType(); - // FIX THIS - NOT ONE BYTE - currentPosition += agentToAdd->getAgentId(); - // --- + currentPosition += packAgentId(currentPosition, agentToAdd->getAgentId()); currentPosition += packSocket(currentPosition, agentToAdd->getPublicSocket()); currentPosition += packSocket(currentPosition, agentToAdd->getLocalSocket()); diff --git a/shared/src/AgentList.h b/shared/src/AgentList.h index 9d6e9991dc..e376f29af7 100644 --- a/shared/src/AgentList.h +++ b/shared/src/AgentList.h @@ -38,8 +38,6 @@ class AgentList { int updateList(unsigned char *packetData, size_t dataBytes); bool addOrUpdateAgent(sockaddr *publicSocket, sockaddr *localSocket, char agentType, uint16_t agentId); - int unpackAgentId(unsigned char *packedData, uint16_t *agentId); - int packAgentId(unsigned char *packStore, uint16_t agentId); void processAgentData(sockaddr *senderAddress, void *packetData, size_t dataBytes); void updateAgentWithData(sockaddr *senderAddress, void *packetData, size_t dataBytes); void broadcastToAgents(char *broadcastData, size_t dataBytes); @@ -56,4 +54,7 @@ class AgentList { void handlePingReply(sockaddr *agentAddress); }; +int unpackAgentId(unsigned char *packedData, uint16_t *agentId); +int packAgentId(unsigned char *packStore, uint16_t agentId); + #endif /* defined(__hifi__AgentList__) */ From a4f6d8567607cf61dbf440ea2f76ff5773cb9b4d Mon Sep 17 00:00:00 2001 From: Leonardo Murillo Date: Fri, 22 Mar 2013 11:45:19 -0600 Subject: [PATCH 06/17] Fixing definitions --- shared/src/AgentList.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/shared/src/AgentList.cpp b/shared/src/AgentList.cpp index 8cae9547bd..7d71f1da2c 100644 --- a/shared/src/AgentList.cpp +++ b/shared/src/AgentList.cpp @@ -109,12 +109,12 @@ int AgentList::indexOfMatchingAgent(sockaddr *senderAddress) { return -1; } -int AgentList::unpackAgentId(unsigned char *packedData, uint16_t *agentId) { +int unpackAgentId(unsigned char *packedData, uint16_t *agentId) { memcpy(packedData, agentId, sizeof(uint16_t)); return sizeof(uint16_t); } -int AgentList::packAgentId(unsigned char *packStore, uint16_t agentId) { +int packAgentId(unsigned char *packStore, uint16_t agentId) { memcpy(&agentId, packStore, sizeof(uint16_t)); return sizeof(uint16_t); } From dc92f0be4233221f962472c67c2fd99113586afd Mon Sep 17 00:00:00 2001 From: Leonardo Murillo Date: Fri, 22 Mar 2013 13:34:35 -0600 Subject: [PATCH 07/17] More agent IDing around the code --- domain/src/main.cpp | 6 ++++-- shared/src/AgentList.cpp | 2 -- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/domain/src/main.cpp b/domain/src/main.cpp index d80864eaa0..bd49ca0ee9 100644 --- a/domain/src/main.cpp +++ b/domain/src/main.cpp @@ -73,6 +73,7 @@ int main(int argc, const char * argv[]) int packetBytesWithoutLeadingChar; sockaddr_in agentPublicAddress, agentLocalAddress; + uint16_t agentId = 0; agentLocalAddress.sin_family = AF_INET; agentList.startSilentAgentRemovalThread(); @@ -82,9 +83,10 @@ int main(int argc, const char * argv[]) std::map newestSoloAgents; agentType = packetData[0]; - unpackSocket(&packetData[1], (sockaddr *)&agentLocalAddress); + unpackAgentId(&packetData[1], (uint16_t *)&agentId); + unpackSocket(&packetData[2], (sockaddr *)&agentLocalAddress); - agentList.addOrUpdateAgent((sockaddr *)&agentPublicAddress, (sockaddr *)&agentLocalAddress, agentType); + agentList.addOrUpdateAgent((sockaddr *)&agentPublicAddress, (sockaddr *)&agentLocalAddress, agentType, agentId); currentBufferPos = broadcastPacket + 1; startPointer = currentBufferPos; diff --git a/shared/src/AgentList.cpp b/shared/src/AgentList.cpp index 7d71f1da2c..eab053e5d0 100644 --- a/shared/src/AgentList.cpp +++ b/shared/src/AgentList.cpp @@ -140,8 +140,6 @@ int AgentList::updateList(unsigned char *packetData, size_t dataBytes) { readPtr += unpackSocket(readPtr, (sockaddr *)&agentPublicSocket); readPtr += unpackSocket(readPtr, (sockaddr *)&agentLocalSocket); - //syncClientAgentList(agentId, (sockaddr *)&agentPublicSocket, (sockaddr *)&agentLocalSocket, agentType); - addOrUpdateAgent((sockaddr *)&agentPublicSocket, (sockaddr *)&agentLocalSocket, agentType, agentId); } From ed7234d56aae7987a851090126caa7ce0182a917 Mon Sep 17 00:00:00 2001 From: Leonardo Murillo Date: Fri, 22 Mar 2013 13:50:32 -0600 Subject: [PATCH 08/17] Fixing index --- domain/src/main.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/domain/src/main.cpp b/domain/src/main.cpp index e26332f9e9..99aa1db05e 100644 --- a/domain/src/main.cpp +++ b/domain/src/main.cpp @@ -84,7 +84,7 @@ int main(int argc, const char * argv[]) agentType = packetData[0]; unpackAgentId(&packetData[1], (uint16_t *)&agentId); - unpackSocket(&packetData[2], (sockaddr *)&agentLocalAddress); + unpackSocket(&packetData[3], (sockaddr *)&agentLocalAddress); agentList.addOrUpdateAgent((sockaddr *)&agentPublicAddress, (sockaddr *)&agentLocalAddress, agentType, agentId); From 61c866a34caf5e195106d056ee508afa37cf2560 Mon Sep 17 00:00:00 2001 From: Leonardo Murillo Date: Fri, 22 Mar 2013 16:57:30 -0600 Subject: [PATCH 09/17] Handling increase of id smarter --- domain/src/main.cpp | 8 ++++---- interface/src/main.cpp | 4 ++-- shared/src/AgentList.cpp | 31 ++++++++++++++++--------------- shared/src/AgentList.h | 2 ++ 4 files changed, 24 insertions(+), 21 deletions(-) diff --git a/domain/src/main.cpp b/domain/src/main.cpp index 99aa1db05e..fad3f21130 100644 --- a/domain/src/main.cpp +++ b/domain/src/main.cpp @@ -73,7 +73,6 @@ int main(int argc, const char * argv[]) int packetBytesWithoutLeadingChar; sockaddr_in agentPublicAddress, agentLocalAddress; - uint16_t agentId = 0; agentLocalAddress.sin_family = AF_INET; agentList.startSilentAgentRemovalThread(); @@ -83,10 +82,11 @@ int main(int argc, const char * argv[]) std::map newestSoloAgents; agentType = packetData[0]; - unpackAgentId(&packetData[1], (uint16_t *)&agentId); - unpackSocket(&packetData[3], (sockaddr *)&agentLocalAddress); + unpackSocket(&packetData[1], (sockaddr *)&agentLocalAddress); - agentList.addOrUpdateAgent((sockaddr *)&agentPublicAddress, (sockaddr *)&agentLocalAddress, agentType, agentId); + if (agentList.addOrUpdateAgent((sockaddr *)&agentPublicAddress, (sockaddr *)&agentLocalAddress, agentType, agentList.getLastAgentId())) { + agentList.increaseAgentId(); + } currentBufferPos = broadcastPacket + 1; startPointer = currentBufferPos; diff --git a/interface/src/main.cpp b/interface/src/main.cpp index 5759aa3c8a..e23ad8ff6a 100644 --- a/interface/src/main.cpp +++ b/interface/src/main.cpp @@ -228,9 +228,9 @@ void Timer(int extra) // // Send a message to the domainserver telling it we are ALIVE - // + // unsigned char output[7]; - output[0] = 'I'; + output[0] = 'I'; packSocket(output + 1, localAddress, htons(AGENT_SOCKET_LISTEN_PORT)); agentList.getAgentSocket().send(DOMAIN_IP, DOMAINSERVER_PORT, output, 7); diff --git a/shared/src/AgentList.cpp b/shared/src/AgentList.cpp index ddfc619315..a32c1e1fd6 100644 --- a/shared/src/AgentList.cpp +++ b/shared/src/AgentList.cpp @@ -108,14 +108,12 @@ int AgentList::indexOfMatchingAgent(sockaddr *senderAddress) { return -1; } -int unpackAgentId(unsigned char *packedData, uint16_t *agentId) { - memcpy(packedData, agentId, sizeof(uint16_t)); - return sizeof(uint16_t); +uint16_t AgentList::getLastAgentId() { + return lastAgentId; } -int packAgentId(unsigned char *packStore, uint16_t agentId) { - memcpy(&agentId, packStore, sizeof(uint16_t)); - return sizeof(uint16_t); +void AgentList::increaseAgentId() { + ++lastAgentId; } int AgentList::updateList(unsigned char *packetData, size_t dataBytes) { @@ -149,7 +147,7 @@ bool AgentList::addOrUpdateAgent(sockaddr *publicSocket, sockaddr *localSocket, std::vector::iterator agent; uint16_t thisAgentId; - for(agent = agents.begin(); agent != agents.end(); agent++) { + for (agent = agents.begin(); agent != agents.end(); agent++) { if (agent->matches(publicSocket, localSocket, agentType)) { // we already have this agent, stop checking break; @@ -159,14 +157,7 @@ bool AgentList::addOrUpdateAgent(sockaddr *publicSocket, sockaddr *localSocket, if (agent == agents.end()) { // we didn't have this agent, so add them - if (agentId == 0) { - thisAgentId = this->lastAgentId; - ++this->lastAgentId; - } else { - thisAgentId = agentId; - } - - Agent newAgent = Agent(publicSocket, localSocket, agentType, thisAgentId); + Agent newAgent = Agent(publicSocket, localSocket, agentType, agentId); if (socketMatch(publicSocket, localSocket)) { // likely debugging scenario with DS + agent on local network @@ -285,4 +276,14 @@ void AgentList::startSilentAgentRemovalThread() { void AgentList::stopSilentAgentRemovalThread() { stopAgentRemovalThread = true; pthread_join(removeSilentAgentsThread, NULL); +} + +int unpackAgentId(unsigned char *packedData, uint16_t *agentId) { + memcpy(packedData, agentId, sizeof(uint16_t)); + return sizeof(uint16_t); +} + +int packAgentId(unsigned char *packStore, uint16_t agentId) { + memcpy(&agentId, packStore, sizeof(uint16_t)); + return sizeof(uint16_t); } \ No newline at end of file diff --git a/shared/src/AgentList.h b/shared/src/AgentList.h index 5edb2ca620..8aabf88476 100644 --- a/shared/src/AgentList.h +++ b/shared/src/AgentList.h @@ -37,6 +37,8 @@ class AgentList { int updateList(unsigned char *packetData, size_t dataBytes); int indexOfMatchingAgent(sockaddr *senderAddress); + uint16_t getLastAgentId(); + void increaseAgentId(); bool addOrUpdateAgent(sockaddr *publicSocket, sockaddr *localSocket, char agentType, uint16_t agentId); void processAgentData(sockaddr *senderAddress, void *packetData, size_t dataBytes); void updateAgentWithData(sockaddr *senderAddress, void *packetData, size_t dataBytes); From be96f1cdd058d5113647f148f35da12130324621 Mon Sep 17 00:00:00 2001 From: Leonardo Murillo Date: Fri, 22 Mar 2013 16:58:52 -0600 Subject: [PATCH 10/17] No longer need exists --- shared/src/Agent.cpp | 4 ---- shared/src/Agent.h | 1 - 2 files changed, 5 deletions(-) diff --git a/shared/src/Agent.cpp b/shared/src/Agent.cpp index 269703260f..b95c3e7dbd 100644 --- a/shared/src/Agent.cpp +++ b/shared/src/Agent.cpp @@ -162,10 +162,6 @@ bool Agent::matches(sockaddr *otherPublicSocket, sockaddr *otherLocalSocket, cha && socketMatch(localSocket, otherLocalSocket); } -bool Agent::exists(uint16_t *otherAgentId) { - return agentId == *otherAgentId; -} - std::ostream& operator<<(std::ostream& os, const Agent* agent) { sockaddr_in *agentPublicSocket = (sockaddr_in *)agent->publicSocket; sockaddr_in *agentLocalSocket = (sockaddr_in *)agent->localSocket; diff --git a/shared/src/Agent.h b/shared/src/Agent.h index e5fa583f20..087d828b49 100644 --- a/shared/src/Agent.h +++ b/shared/src/Agent.h @@ -28,7 +28,6 @@ class Agent { bool operator==(const Agent& otherAgent); bool matches(sockaddr *otherPublicSocket, sockaddr *otherLocalSocket, char otherAgentType); - bool exists(uint16_t *agentId); char getType(); void setType(char newType); uint16_t getAgentId(); From bd18aeefe4e093f57f64ae17d911807352c668bc Mon Sep 17 00:00:00 2001 From: Leonardo Murillo Date: Fri, 22 Mar 2013 17:39:07 -0600 Subject: [PATCH 11/17] Couple of quick changes --- shared/src/AgentList.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/shared/src/AgentList.cpp b/shared/src/AgentList.cpp index a32c1e1fd6..b91ad2a487 100644 --- a/shared/src/AgentList.cpp +++ b/shared/src/AgentList.cpp @@ -24,13 +24,13 @@ pthread_mutex_t vectorChangeMutex = PTHREAD_MUTEX_INITIALIZER; AgentList::AgentList() : agentSocket(AGENT_SOCKET_LISTEN_PORT) { linkedDataCreateCallback = NULL; audioMixerSocketUpdate = NULL; - lastAgentId = 1; + lastAgentId = 0; } AgentList::AgentList(int socketListenPort) : agentSocket(socketListenPort) { linkedDataCreateCallback = NULL; audioMixerSocketUpdate = NULL; - lastAgentId = 1; + lastAgentId = 0; } @@ -143,7 +143,7 @@ int AgentList::updateList(unsigned char *packetData, size_t dataBytes) { return readAgents; } -bool AgentList::addOrUpdateAgent(sockaddr *publicSocket, sockaddr *localSocket, char agentType, uint16_t agentId = 0) { +bool AgentList::addOrUpdateAgent(sockaddr *publicSocket, sockaddr *localSocket, char agentType, uint16_t agentId) { std::vector::iterator agent; uint16_t thisAgentId; From 8ecc0d53adba5e0c0c9035b3607b790ec723b336 Mon Sep 17 00:00:00 2001 From: ZappoMan Date: Sat, 23 Mar 2013 21:59:27 -0700 Subject: [PATCH 12/17] Added a utility to VoxelSystem for loading voxels from a local file Also added a couple shared utility functions for reading command line options (since that's how you can load a local file at this point). --- interface/src/VoxelSystem.cpp | 56 +++++++++++++++++++++++++++++++++-- interface/src/VoxelSystem.h | 2 ++ interface/src/main.cpp | 10 ++++++- shared/src/SharedUtil.cpp | 32 +++++++++++++++++++- shared/src/SharedUtil.h | 3 ++ 5 files changed, 99 insertions(+), 4 deletions(-) diff --git a/interface/src/VoxelSystem.cpp b/interface/src/VoxelSystem.cpp index ebb29a8e5f..4aaee09ed7 100644 --- a/interface/src/VoxelSystem.cpp +++ b/interface/src/VoxelSystem.cpp @@ -8,6 +8,8 @@ #include #include +#include // to load voxels from file +#include // to load voxels from file #include #include #include @@ -46,6 +48,56 @@ 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); +} + void VoxelSystem::parseData(void *data, int size) { // output the bits received from the voxel server unsigned char *voxelData = (unsigned char *) data + 1; @@ -68,7 +120,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 +147,7 @@ int VoxelSystem::treeToArrays(VoxelNode *currentNode) { delete [] startVertex; } - + return voxelsAdded; } diff --git a/interface/src/VoxelSystem.h b/interface/src/VoxelSystem.h index e13ca27b3e..adb73808de 100644 --- a/interface/src/VoxelSystem.h +++ b/interface/src/VoxelSystem.h @@ -33,6 +33,8 @@ public: void render(); void setVoxelsRendered(int v) {voxelsRendered = v;}; int getVoxelsRendered() {return voxelsRendered;}; + void loadVoxelsFile(char* fileName); + private: int voxelsRendered; VoxelTree *tree; diff --git a/interface/src/main.cpp b/interface/src/main.cpp index 5759aa3c8a..e933169e1d 100644 --- a/interface/src/main.cpp +++ b/interface/src/main.cpp @@ -53,6 +53,7 @@ #include "Oscilloscope.h" #include "UDPSocket.h" #include "SerialInterface.h" +#include using namespace std; @@ -324,7 +325,6 @@ void initDisplay(void) void init(void) { voxels.init(); - myHead.setRenderYaw(start_yaw); head_mouse_x = WIDTH/2; @@ -969,6 +969,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); diff --git a/shared/src/SharedUtil.cpp b/shared/src/SharedUtil.cpp index 740e6f6cfb..3340241fdd 100644 --- a/shared/src/SharedUtil.cpp +++ b/shared/src/SharedUtil.cpp @@ -73,4 +73,34 @@ void switchToResourcesIfRequired() { chdir(path); #endif -} \ No newline at end of file +} + +////////////////////////////////////////////////////////////////////////////////////////// +// 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; +} diff --git a/shared/src/SharedUtil.h b/shared/src/SharedUtil.h index 3c53721deb..1424fa77a5 100644 --- a/shared/src/SharedUtil.h +++ b/shared/src/SharedUtil.h @@ -31,4 +31,7 @@ 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); + #endif /* defined(__hifi__SharedUtil__) */ From ef8694dd609878abb3546c405e1ff6ff8e09152b Mon Sep 17 00:00:00 2001 From: ZappoMan Date: Sun, 24 Mar 2013 10:06:43 -0700 Subject: [PATCH 13/17] 1) Added createSphere() to VoxelSystem to create a sphere 2) Added pointToVoxel() to SharedUtils, this will give you a voxel code given x,y,z,s,r,g,b 3) Added '.' keyboard interface to create a random sphere in the local voxel system --- interface/src/VoxelSystem.cpp | 83 +++++++++++++++++ interface/src/VoxelSystem.h | 2 +- interface/src/main.cpp | 46 ++++++++++ shared/src/SharedUtil.cpp | 163 ++++++++++++++++++++++++++++++++++ shared/src/SharedUtil.h | 4 + 5 files changed, 297 insertions(+), 1 deletion(-) diff --git a/interface/src/VoxelSystem.cpp b/interface/src/VoxelSystem.cpp index 4aaee09ed7..3b5f6a267b 100644 --- a/interface/src/VoxelSystem.cpp +++ b/interface/src/VoxelSystem.cpp @@ -98,6 +98,88 @@ void VoxelSystem::loadVoxelsFile(char* fileName) { 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) +{ + // 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; + */ + + // random color data + unsigned char red = randomColorValue(65); + unsigned char green = randomColorValue(65); + unsigned char blue = randomColorValue(65); + + 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; @@ -243,3 +325,4 @@ void VoxelSystem::simulate(float deltaTime) { } + diff --git a/interface/src/VoxelSystem.h b/interface/src/VoxelSystem.h index adb73808de..6755886225 100644 --- a/interface/src/VoxelSystem.h +++ b/interface/src/VoxelSystem.h @@ -34,7 +34,7 @@ public: 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; diff --git a/interface/src/main.cpp b/interface/src/main.cpp index e933169e1d..e572942823 100644 --- a/interface/src/main.cpp +++ b/interface/src/main.cpp @@ -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.002; // 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(); + } } // diff --git a/shared/src/SharedUtil.cpp b/shared/src/SharedUtil.cpp index 3340241fdd..3e9e75e2bb 100644 --- a/shared/src/SharedUtil.cpp +++ b/shared/src/SharedUtil.cpp @@ -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)); } @@ -104,3 +108,162 @@ 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) { + // + byte = (byte << 1) | true; + xTest += sTest/2.0; + } + else { + // + 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) { + // + byte = (byte << 1) | true; + yTest += sTest/2.0; + } + else { + // + 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) { + // + byte = (byte << 1) | true; + zTest += sTest/2.0; + } + else { + // + 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 Date: Sun, 24 Mar 2013 13:00:23 -0700 Subject: [PATCH 14/17] Changed colors of spheres to be a gradient along phi between to random colors this gives the spheres a little bit more natural look. Also increased the level of detail (smaller voxels) --- interface/src/VoxelSystem.cpp | 37 +++++++++++++++++++++++++++++++---- interface/src/main.cpp | 2 +- 2 files changed, 34 insertions(+), 5 deletions(-) diff --git a/interface/src/VoxelSystem.cpp b/interface/src/VoxelSystem.cpp index 3b5f6a267b..4e88d01ddf 100644 --- a/interface/src/VoxelSystem.cpp +++ b/interface/src/VoxelSystem.cpp @@ -107,6 +107,34 @@ void VoxelSystem::loadVoxelsFile(char* fileName) { // 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): @@ -157,10 +185,11 @@ void VoxelSystem::createSphere(float r,float xc, float yc, float zc, float s, bo std::cout << std::endl; */ - // random color data - unsigned char red = randomColorValue(65); - unsigned char green = randomColorValue(65); - unsigned char blue = randomColorValue(65); + // 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); diff --git a/interface/src/main.cpp b/interface/src/main.cpp index e572942823..3df7672a2f 100644 --- a/interface/src/main.cpp +++ b/interface/src/main.cpp @@ -718,7 +718,7 @@ void addRandomSphere() float xc = randFloatInRange(r,(1-r)); float yc = randFloatInRange(r,(1-r)); float zc = randFloatInRange(r,(1-r)); - float s = 0.002; // size of voxels to make up surface of sphere + float s = 0.001; // size of voxels to make up surface of sphere bool solid = false; printf("random sphere\n"); From 54a946ee80111c6a62c711b7629b5b84f54faddc Mon Sep 17 00:00:00 2001 From: Leonardo Murillo Date: Mon, 25 Mar 2013 15:54:08 -0600 Subject: [PATCH 15/17] Splitting line, adding agentId to copy constructor and swap --- domain/src/main.cpp | 7 ++++++- shared/src/Agent.cpp | 4 ++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/domain/src/main.cpp b/domain/src/main.cpp index fad3f21130..940ec5f578 100644 --- a/domain/src/main.cpp +++ b/domain/src/main.cpp @@ -84,8 +84,13 @@ int main(int argc, const char * argv[]) agentType = packetData[0]; unpackSocket(&packetData[1], (sockaddr *)&agentLocalAddress); - if (agentList.addOrUpdateAgent((sockaddr *)&agentPublicAddress, (sockaddr *)&agentLocalAddress, agentType, agentList.getLastAgentId())) { + if (agentList.addOrUpdateAgent((sockaddr *)&agentPublicAddress, + (sockaddr *)&agentLocalAddress, + agentType, + agentList.getLastAgentId())) { + agentList.increaseAgentId(); + } currentBufferPos = broadcastPacket + 1; diff --git a/shared/src/Agent.cpp b/shared/src/Agent.cpp index b95c3e7dbd..9b64fc26e1 100644 --- a/shared/src/Agent.cpp +++ b/shared/src/Agent.cpp @@ -43,6 +43,9 @@ Agent::Agent(const Agent &otherAgent) { localSocket = new sockaddr; memcpy(localSocket, otherAgent.localSocket, sizeof(sockaddr)); + agentId = *new uint16_t; + memcpy(&agentId, &otherAgent.agentId, sizeof(uint16_t)); + if (otherAgent.activeSocket == otherAgent.publicSocket) { activeSocket = publicSocket; } else if (otherAgent.activeSocket == otherAgent.localSocket) { @@ -153,6 +156,7 @@ void Agent::swap(Agent &first, Agent &second) { swap(first.activeSocket, second.activeSocket); swap(first.type, second.type); swap(first.linkedData, second.linkedData); + swap(first.agentId, second.agentId); } bool Agent::matches(sockaddr *otherPublicSocket, sockaddr *otherLocalSocket, char otherAgentType) { From cb84b7573be51e32a6121c288dbc47ecfe0bdc54 Mon Sep 17 00:00:00 2001 From: Leonardo Murillo Date: Mon, 25 Mar 2013 15:56:22 -0600 Subject: [PATCH 16/17] Getting rid of unused declaration --- shared/src/AgentList.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/shared/src/AgentList.cpp b/shared/src/AgentList.cpp index b91ad2a487..d92e894079 100644 --- a/shared/src/AgentList.cpp +++ b/shared/src/AgentList.cpp @@ -145,7 +145,6 @@ int AgentList::updateList(unsigned char *packetData, size_t dataBytes) { bool AgentList::addOrUpdateAgent(sockaddr *publicSocket, sockaddr *localSocket, char agentType, uint16_t agentId) { std::vector::iterator agent; - uint16_t thisAgentId; for (agent = agents.begin(); agent != agents.end(); agent++) { if (agent->matches(publicSocket, localSocket, agentType)) { From 63589985f4b2262ad994c9020a84a79019071aa0 Mon Sep 17 00:00:00 2001 From: Leonardo Murillo Date: Mon, 25 Mar 2013 16:12:51 -0600 Subject: [PATCH 17/17] Removing unneeded memcpy --- shared/src/Agent.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/shared/src/Agent.cpp b/shared/src/Agent.cpp index 9b64fc26e1..8ec12c9020 100644 --- a/shared/src/Agent.cpp +++ b/shared/src/Agent.cpp @@ -43,8 +43,7 @@ Agent::Agent(const Agent &otherAgent) { localSocket = new sockaddr; memcpy(localSocket, otherAgent.localSocket, sizeof(sockaddr)); - agentId = *new uint16_t; - memcpy(&agentId, &otherAgent.agentId, sizeof(uint16_t)); + agentId = otherAgent.agentId; if (otherAgent.activeSocket == otherAgent.publicSocket) { activeSocket = publicSocket;