2021-10-02 20:36:07 +03:00
|
|
|
//
|
|
|
|
// Created by Иван Ильин on 15.03.2021.
|
|
|
|
//
|
|
|
|
|
|
|
|
#include "Object.h"
|
2021-10-12 17:12:47 +03:00
|
|
|
#include "Matrix4x4.h"
|
2021-10-02 20:36:07 +03:00
|
|
|
#include "utils/Log.h"
|
|
|
|
|
2021-10-12 17:12:47 +03:00
|
|
|
void Object::translate(const Vec3D &dv) {
|
|
|
|
_position = std::make_unique<Vec3D>(*_position + dv);
|
2021-10-02 20:36:07 +03:00
|
|
|
|
2021-10-17 10:21:10 +03:00
|
|
|
for(auto &[attachedName, attachedObject] : _attachedObjects)
|
|
|
|
attachedObject->translate(dv);
|
2021-10-02 20:36:07 +03:00
|
|
|
}
|
|
|
|
|
2021-10-12 17:12:47 +03:00
|
|
|
void Object::scale(const Vec3D &s) {
|
2021-10-17 10:21:10 +03:00
|
|
|
for(auto &[attachedName, attachedObject] : _attachedObjects)
|
|
|
|
attachedObject->scale(s);
|
2021-10-02 20:36:07 +03:00
|
|
|
}
|
|
|
|
|
2021-10-12 17:12:47 +03:00
|
|
|
void Object::rotate(const Vec3D &r) {
|
|
|
|
_angle = std::make_unique<Vec3D>(*_angle + r);
|
2021-10-02 20:36:07 +03:00
|
|
|
|
|
|
|
Matrix4x4 rotationMatrix = Matrix4x4::RotationZ(r.z())*Matrix4x4::RotationY(r.y())*Matrix4x4::RotationX(r.z());
|
|
|
|
|
2021-10-12 17:12:47 +03:00
|
|
|
_left = std::make_unique<Vec3D>(rotationMatrix * *_left);
|
|
|
|
_up = std::make_unique<Vec3D>(rotationMatrix * *_up);
|
|
|
|
_lookAt = std::make_unique<Vec3D>(rotationMatrix * *_lookAt);
|
2021-10-02 20:36:07 +03:00
|
|
|
|
2021-10-17 10:21:10 +03:00
|
|
|
for(auto &[attachedName, attachedObject] : _attachedObjects)
|
|
|
|
attachedObject->rotateRelativePoint(position(), r);
|
2021-10-02 20:36:07 +03:00
|
|
|
}
|
|
|
|
|
2021-10-12 17:12:47 +03:00
|
|
|
void Object::rotate(const Vec3D &v, double rv) {
|
2021-10-02 20:36:07 +03:00
|
|
|
Matrix4x4 rotationMatrix = Matrix4x4::Rotation(v, rv);
|
2021-10-12 17:12:47 +03:00
|
|
|
|
|
|
|
_left = std::make_unique<Vec3D>(rotationMatrix * *_left);
|
|
|
|
_up = std::make_unique<Vec3D>(rotationMatrix * *_up);
|
|
|
|
_lookAt = std::make_unique<Vec3D>(rotationMatrix * *_lookAt);
|
2021-10-02 20:36:07 +03:00
|
|
|
|
2021-10-17 10:21:10 +03:00
|
|
|
for(auto &[attachedName, attachedObject] : _attachedObjects)
|
|
|
|
attachedObject->rotateRelativePoint(position(), v, rv);
|
2021-10-02 20:36:07 +03:00
|
|
|
}
|
|
|
|
|
2021-10-12 17:12:47 +03:00
|
|
|
void Object::rotateRelativePoint(const Vec3D &s, const Vec3D &r) {
|
|
|
|
_angle = std::make_unique<Vec3D>(*_angle + r);
|
2021-10-02 20:36:07 +03:00
|
|
|
|
|
|
|
// Translate XYZ by vector r1
|
2021-10-12 17:12:47 +03:00
|
|
|
Vec3D r1(*_position - s);
|
2021-10-02 20:36:07 +03:00
|
|
|
|
|
|
|
// In translated coordinate system we rotate body and position
|
|
|
|
Matrix4x4 rotationMatrix = Matrix4x4::Rotation(r);
|
2021-10-12 17:12:47 +03:00
|
|
|
Vec3D r2(rotationMatrix*r1);
|
2021-10-02 20:36:07 +03:00
|
|
|
|
2021-10-12 17:12:47 +03:00
|
|
|
_left = std::make_unique<Vec3D>(rotationMatrix * *_left);
|
|
|
|
_up = std::make_unique<Vec3D>(rotationMatrix * *_up);
|
|
|
|
_lookAt = std::make_unique<Vec3D>(rotationMatrix * *_lookAt);
|
2021-10-02 20:36:07 +03:00
|
|
|
|
|
|
|
// After rotation we translate XYZ by vector -r2 and recalculate position
|
2021-10-12 17:12:47 +03:00
|
|
|
_position = std::make_unique<Vec3D>(s + r2);
|
2021-10-02 20:36:07 +03:00
|
|
|
|
2021-10-17 10:21:10 +03:00
|
|
|
for(auto &[attachedName, attachedObject] : _attachedObjects)
|
|
|
|
attachedObject->rotateRelativePoint(s, r);
|
2021-10-02 20:36:07 +03:00
|
|
|
}
|
|
|
|
|
2021-10-12 17:12:47 +03:00
|
|
|
void Object::rotateRelativePoint(const Vec3D &s, const Vec3D &v, double r) {
|
2021-10-02 20:36:07 +03:00
|
|
|
// Translate XYZ by vector r1
|
2021-10-12 17:12:47 +03:00
|
|
|
Vec3D r1(*_position - s);
|
2021-10-02 20:36:07 +03:00
|
|
|
// In translated coordinate system we rotate body and position
|
|
|
|
Matrix4x4 rotationMatrix = Matrix4x4::Rotation(v, r);
|
2021-10-12 17:12:47 +03:00
|
|
|
Vec3D r2 = rotationMatrix*r1;
|
2021-10-02 20:36:07 +03:00
|
|
|
|
2021-10-12 17:12:47 +03:00
|
|
|
_left = std::make_unique<Vec3D>(rotationMatrix * *_left);
|
|
|
|
_up = std::make_unique<Vec3D>(rotationMatrix * *_up);
|
|
|
|
_lookAt = std::make_unique<Vec3D>(rotationMatrix * *_lookAt);
|
2021-10-02 20:36:07 +03:00
|
|
|
|
|
|
|
// After rotation we translate XYZ by vector -r2 and recalculate position
|
2021-10-12 17:12:47 +03:00
|
|
|
_position = std::make_unique<Vec3D>(s + r2);
|
2021-10-02 20:36:07 +03:00
|
|
|
|
2021-10-17 10:21:10 +03:00
|
|
|
for(auto &[attachedName, attachedObject] : _attachedObjects)
|
|
|
|
attachedObject->rotateRelativePoint(s, v, r);
|
2021-10-02 20:36:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void Object::rotateLeft(double rl) {
|
2021-10-12 17:12:47 +03:00
|
|
|
_angleLeftUpLookAt = std::make_unique<Vec3D>(Vec3D{_angleLeftUpLookAt->x() + rl,
|
2021-10-18 18:30:02 +03:00
|
|
|
_angleLeftUpLookAt->y(),
|
|
|
|
_angleLeftUpLookAt->z()});
|
2021-10-02 20:36:07 +03:00
|
|
|
|
2021-10-18 18:30:02 +03:00
|
|
|
rotate(Vec3D(*_left), rl);
|
2021-10-02 20:36:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void Object::rotateUp(double ru) {
|
2021-10-12 17:12:47 +03:00
|
|
|
_angleLeftUpLookAt = std::make_unique<Vec3D>(Vec3D{_angleLeftUpLookAt->x(),
|
2021-10-18 18:30:02 +03:00
|
|
|
_angleLeftUpLookAt->y() + ru,
|
|
|
|
_angleLeftUpLookAt->z()});
|
2021-10-02 20:36:07 +03:00
|
|
|
|
2021-10-18 18:30:02 +03:00
|
|
|
rotate(Vec3D(*_up), ru);
|
2021-10-02 20:36:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void Object::rotateLookAt(double rlAt) {
|
2021-10-12 17:12:47 +03:00
|
|
|
_angleLeftUpLookAt = std::make_unique<Vec3D>(Vec3D{_angleLeftUpLookAt->x(),
|
|
|
|
_angleLeftUpLookAt->y(),
|
|
|
|
_angleLeftUpLookAt->z() + rlAt});
|
2021-10-18 18:30:02 +03:00
|
|
|
rotate(Vec3D(*_lookAt), rlAt);
|
2021-10-02 20:36:07 +03:00
|
|
|
}
|
|
|
|
|
2021-10-12 17:12:47 +03:00
|
|
|
void Object::translateToPoint(const Vec3D &point) {
|
|
|
|
translate(point - *_position);
|
2021-10-02 20:36:07 +03:00
|
|
|
}
|
|
|
|
|
2021-10-12 17:12:47 +03:00
|
|
|
void Object::rotateToAngle(const Vec3D &v) {
|
|
|
|
rotate(v - *_angle);
|
2021-10-02 20:36:07 +03:00
|
|
|
}
|
|
|
|
|
2021-10-17 10:21:10 +03:00
|
|
|
std::shared_ptr<Object> Object::attached(const ObjectNameTag& tag) {
|
|
|
|
if(_attachedObjects.count(tag) == 0)
|
2021-10-16 16:14:51 +03:00
|
|
|
return nullptr;
|
2021-10-17 10:21:10 +03:00
|
|
|
return _attachedObjects.find(tag)->second;
|
2021-10-02 20:36:07 +03:00
|
|
|
}
|
|
|
|
|
2021-10-17 10:21:10 +03:00
|
|
|
void Object::attach(std::shared_ptr<Object> object, const ObjectNameTag& tag) {
|
2021-10-12 17:12:47 +03:00
|
|
|
// TODO: solve problem with possible infinite recursive call chains
|
|
|
|
if(this != object.get())
|
2021-10-17 10:21:10 +03:00
|
|
|
_attachedObjects.emplace(tag, object);
|
2021-10-12 17:12:47 +03:00
|
|
|
else
|
|
|
|
throw std::invalid_argument{"Object::attach: You cannot attach object to itself"};
|
2021-10-02 20:36:07 +03:00
|
|
|
}
|
|
|
|
|
2021-10-17 10:21:10 +03:00
|
|
|
void Object::unattach(const ObjectNameTag& tag) {
|
|
|
|
_attachedObjects.erase(tag);
|
2021-10-02 20:36:07 +03:00
|
|
|
}
|
2021-10-16 20:22:55 +03:00
|
|
|
|
|
|
|
Object::~Object() {
|
|
|
|
_attachedObjects.clear();
|
|
|
|
}
|