#include "Thing.h" #include "LocalParticipant.h" #include #if defined(ARDUINO) #include "Arduino.h" #else #include #include #include #include #endif namespace RoboidControl { // LocalParticipant* Thing::CheckHiddenParticipant() { // if (isolatedParticipant == nullptr) // isolatedParticipant = new LocalParticipant(0); // return isolatedParticipant; // } Thing::Thing(int thingType) : Thing(LocalParticipant::Isolated(), thingType) {} Thing::Thing(Participant* owner, Type thingType) : Thing(owner, (unsigned char)thingType) {} Thing::Thing(Participant* owner, int thingType) { this->owner = owner; this->id = 0; this->type = thingType; this->networkId = 0; this->position = Spherical::zero; this->orientation = SwingTwist::identity; this->linearVelocity = Spherical::zero; this->angularVelocity = Spherical::zero; // std::cout << "add thing to participant\n"; owner->Add(this); } Thing::Thing(Participant* owner, unsigned char networkId, unsigned char thingId, Type thingType) { // no participant reference yet.. this->owner = owner; this->networkId = networkId; this->id = thingId; this->type = (unsigned char)thingType; this->linearVelocity = Spherical::zero; this->angularVelocity = Spherical::zero; // std::cout << "Created thing " << (int)this->networkId << "/" << // (int)this->id // << "\n"; owner->Add(this, false); } void Thing::Terminate() { // Thing::Remove(this); } Thing* Thing::FindThing(const char* name) { for (unsigned char childIx = 0; childIx < this->childCount; childIx++) { Thing* child = this->children[childIx]; if (child == nullptr || child->name == nullptr) continue; if (strcmp(child->name, name) == 0) return child; Thing* foundChild = child->FindThing(name); if (foundChild != nullptr) return foundChild; } return nullptr; } void Thing::SetParent(Thing* parent) { if (parent == nullptr) { Thing* parentThing = this->parent; if (parentThing != nullptr) parentThing->RemoveChild(this); this->parent = nullptr; } else parent->AddChild(this); } void Thing::SetParent(Thing* root, const char* name) { Thing* thing = root->FindThing(name); if (thing != nullptr) this->SetParent(thing); } Thing* Thing::GetParent() { return this->parent; } void Thing::AddChild(Thing* child) { unsigned char newChildCount = this->childCount + 1; Thing** newChildren = new Thing*[newChildCount]; for (unsigned char childIx = 0; childIx < this->childCount; childIx++) { newChildren[childIx] = this->children[childIx]; if (this->children[childIx] == child) { // child is already present, stop copying do not update the children delete[] newChildren; return; } } newChildren[this->childCount] = child; child->parent = this; if (this->children != nullptr) delete[] this->children; this->children = newChildren; this->childCount = newChildCount; } Thing* Thing::RemoveChild(Thing* child) { unsigned char newChildCount = this->childCount - 1; Thing** newChildren = new Thing*[newChildCount]; unsigned char newChildIx = 0; for (unsigned char childIx = 0; childIx < this->childCount; childIx++) { if (this->children[childIx] != child) { if (newChildIx == newChildCount) { // We did not find the child // stop copying and return nothing delete[] newChildren; return nullptr; } else newChildren[newChildIx++] = this->children[childIx]; } } child->parent = nullptr; delete[] this->children; this->children = newChildren; this->childCount = newChildCount; return child; } Thing* Thing::GetChild(unsigned char id, bool recursive) { for (unsigned char childIx = 0; childIx < this->childCount; childIx++) { Thing* child = this->children[childIx]; if (child == nullptr) continue; if (child->id == id) return child; if (recursive) { Thing* foundChild = child->GetChild(id, recursive); if (foundChild != nullptr) return foundChild; } } return nullptr; } Thing* Thing::GetChildByIndex(unsigned char ix) { return this->children[ix]; } void Thing::SetModel(const char* url) { this->modelUrl = url; } unsigned long Thing::GetTimeMs() { #if defined(ARDUINO) return millis(); #else auto now = std::chrono::steady_clock::now(); auto ms = std::chrono::duration_cast( now.time_since_epoch()); return static_cast(ms.count()); #endif } void Thing::Update(bool recursive) { Update(GetTimeMs(), recursive); } void Thing::Update(unsigned long currentTimeMs, bool recursive) { (void)currentTimeMs; if (recursive) { for (unsigned char childIx = 0; childIx < this->childCount; childIx++) { Thing* child = this->children[childIx]; if (child == nullptr) continue; child->Update(currentTimeMs, recursive); } } } void Thing::UpdateThings(unsigned long currentTimeMs) { LocalParticipant::Isolated()->Update(currentTimeMs); } void Thing::GenerateBinary(char* buffer, unsigned char* ix) { (void)buffer; (void)ix; } void Thing::ProcessBinary(char* bytes) { (void)bytes; }; void Thing::SetPosition(Spherical position) { this->position = position; this->positionUpdated = true; } Spherical Thing::GetPosition() { return this->position; } void Thing::SetOrientation(SwingTwist orientation) { this->orientation = orientation; this->orientationUpdated = true; } SwingTwist Thing::GetOrientation() { return this->orientation; } void Thing::SetLinearVelocity(Spherical linearVelocity) { this->linearVelocity = linearVelocity; this->linearVelocityUpdated = true; } Spherical Thing::GetLinearVelocity() { return this->linearVelocity; } void Thing::SetAngularVelocity(Spherical angularVelocity) { this->angularVelocity = angularVelocity; this->angularVelocityUpdated = true; } Spherical Thing::GetAngularVelocity() { return this->angularVelocity; } } // namespace RoboidControl