Skip to content

Commit

Permalink
added a unit test and tidy up
Browse files Browse the repository at this point in the history
  • Loading branch information
peeld committed Aug 4, 2023
1 parent 42cebb1 commit de1f804
Show file tree
Hide file tree
Showing 9 changed files with 170 additions and 14 deletions.
4 changes: 3 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
cmake_minimum_required(VERSION 3.13)

project(Open3DStream VERSION 0.1.0)
project(Open3DStream VERSION 0.1.1)

#if (CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT)
# set(CMAKE_INSTALL_PREFIX ${CMAKE_CURRENT_LIST_DIR}/usr )
Expand All @@ -16,3 +16,5 @@ add_subdirectory(src)
add_subdirectory(plugins/mobu)
add_subdirectory(plugins/maya)

add_subdirectory(apps/Test1)

4 changes: 3 additions & 1 deletion apps/FbxStream/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ int main(int argc, char *argv[])

int frame = 0;

size_t count = 0;

O3DS::SubjectList subjectTest;


Expand Down Expand Up @@ -121,7 +123,7 @@ int main(int argc, char *argv[])
}
else
{
ret = subjects.SerializeUpdate(buffer, zerof + t.GetSecondDouble());
ret = subjects.SerializeUpdate(buffer, count, zerof + t.GetSecondDouble());
}

// Send
Expand Down
13 changes: 13 additions & 0 deletions apps/Test1/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
SET(CMAKE_MIN_REQ_VERSION 3.13)
cmake_minimum_required(VERSION ${CMAKE_MIN_REQ_VERSION})

set(AppName Test1)
project(${AppName}Project)

find_package(nng CONFIG REQUIRED)

add_executable(${AppName} main.cpp)
target_link_libraries(${AppName} open3dstreamstatic ${NNG_LIBRARY})

target_include_directories(${AppName} PUBLIC ${CMAKE_CURRENT_LIST_DIR}/spdlog/include)

138 changes: 138 additions & 0 deletions apps/Test1/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
#pragma once


#include "o3ds/async_publisher.h"
#include "o3ds/model.h"
#include "o3ds/o3ds.h"
#include <chrono>
#include <iostream>

void verify(std::vector<char> buffer, double rotate, O3DS::SubjectList &subjectList)
{



if (!subjectList.Parse(buffer.data(), buffer.size()))
{
std::cout << "Could not parse data" << std::endl;
return;
}

O3DS::Subject* subjectCopy = subjectList.findSubject("Test1");
if (subjectCopy == nullptr)
{
std::cout << "Verify failed on find subject" << std::endl;
return;
}

if (subjectCopy->mTransforms.size() != 2)
{
std::cout << "Verify failed on transform count" << std::endl;
return;
}

auto rot_mat = O3DS::Matrix<double>::RotateX(rotate);
auto value = rot_mat.GetQuaternion();

O3DS::Transform* bone = subjectCopy->mTransforms.find("Root");
if (bone == nullptr)
{
std::cout << "Verify failed on finding bone" << std::endl;
return;
}

for (int i = 0; i < 4; i++)
{
// Do a float precition test
float a = bone->rotation.value.v[i];
float b = value.v[i];
if (a != b)
{
std::cout << "Verify failed on bone channel " << i << " " << a - b << std::endl;
return;
}
}


}

int main(int arc, char** argv)
{
O3DS::AsyncPublisher publisher;

if (publisher.start("INVALIDTEXT")) {
std::cout << "Able to start invalid openStream server" << std::endl;
return 1;
}


if (!publisher.start("tcp://127.0.0.1:3001")) {
std::cout << "Unable to start OpenStream Publish Server: " << publisher.getError() << std::endl;
return 1;
}

O3DS::SubjectList subjectList;

O3DS::SubjectList subjectListCopy;


O3DS::Subject *subject = subjectList.addSubject("Test1");

O3DS::Transform* root = subject->addTransform(std::string("Root"), -1);
O3DS::Transform* bone = subject->addTransform(std::string("Bone"), 0);

root->transformOrder.push_back(O3DS::ComponentType::TTranslation);
root->transformOrder.push_back(O3DS::ComponentType::TRotation);
root->transformOrder.push_back(O3DS::ComponentType::TScale);

bone->transformOrder.push_back(O3DS::ComponentType::TTranslation);
bone->transformOrder.push_back(O3DS::ComponentType::TRotation);
bone->transformOrder.push_back(O3DS::ComponentType::TScale);

bone->translation.value.v[0] = 10;

double rotate = 1;
size_t frame = 0;

while (1)
{
auto rot_mat = O3DS::Matrix<double>::RotateX(rotate);
root->rotation.value = rot_mat.GetQuaternion();

std::vector<char> buffer;
std::chrono::time_point< std::chrono::system_clock > now = std::chrono::system_clock::now();
auto duration = now.time_since_epoch();

int ret;
if (frame % 100 != 0) {
size_t count = 0;
ret = subjectList.SerializeUpdate(buffer, count, (double)std::chrono::duration_cast<std::chrono::nanoseconds>(duration).count());
}
else {
ret = subjectList.Serialize(buffer, (double)std::chrono::duration_cast<std::chrono::nanoseconds>(duration).count());
}

if (ret > 0) {
auto ptr = (const char*)&buffer[0];
if (!publisher.write(ptr, ret)) {
std::cout << "Could not write frame" << std::endl;
}
}
else
{
std::cout << "No data created" << std::endl;
}

verify(buffer, rotate, subjectListCopy);

std::this_thread::sleep_for(std::chrono::milliseconds(100));

frame++;
rotate += 0.01;

}



return 0;
}
2 changes: 2 additions & 0 deletions build.bat
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ EXIT /B 1

IF EXIST %~DP0usr GOTO USROK

IF EXIST D:\bin\Python310\python.exe SET PATH=%PATH%;D:\bin\Python310\

cd thirdparty
build.bat
if %errorlevel% neq 0 exit /b %errorlevel%
Expand Down
6 changes: 2 additions & 4 deletions src/o3ds/async_publisher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,8 @@ namespace O3DS
ret = nng_pub0_open(&mSocket);
NNG_ERROR("Creating publish scocket");

if ((ret = nng_listen(mSocket, url, NULL, 0)) < 0) {
setError("Could not listen", ret);
return false;
}
ret = nng_listen(mSocket, url, NULL, 0);
NNG_ERROR("Could not listen");

mState = Connector::STARTED;

Expand Down
2 changes: 2 additions & 0 deletions src/o3ds/async_subscriber.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,12 @@ SOFTWARE.
namespace O3DS
{
// The client pulls data down from a listen server
//! Implementation of nng sub pattern, to connect to a publisher.
class AsyncSubscriber : public AsyncNngConnector
{
public:

//! Start the server using the provided url - usually starts with tcp://
bool start(const char*url);

void callback_()
Expand Down
14 changes: 6 additions & 8 deletions src/o3ds/publisher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,12 @@ bool O3DS::Publisher::start(const char *url)
{
int ret;

if ((ret = nng_pub0_open(&mSocket)) != 0) {
setError("Could not open socket", ret);
return false;
}
if ((ret = nng_listen(mSocket, url, NULL, 0)) != 0) {
setError("Could not listen", ret);
return false;
}
ret = nng_pub0_open(&mSocket);
NNG_ERROR("Could not open socket");

ret = nng_listen(mSocket, url, NULL, 0);
NNG_ERROR("Could not listen");

return true;
}

1 change: 1 addition & 0 deletions src/o3ds/publisher.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ namespace O3DS
{
class Publisher : public BlockingNngConnector
{
//! Implemetation of nng publish pattern
public:
bool start(const char*url);

Expand Down

0 comments on commit de1f804

Please sign in to comment.