Unsolved Rotating an object in 3D quaternions
-
I'm writing a program with 3D qt 5.9, I need to write a function that will create a line (cylinder) from point A (x, y, z) to point B (x, y, z), I could create a cylinder, line (The cylinder is created parallel to the Y axis), set the length and specify the initial coordinates but do not get it to the desired point, I found that it needs to do a U-turn using quaternions but I did not understand how to correctly apply it, can anyone know how in qt using Quaternions to deploy the object to the desired point.
#include <QGuiApplication>
#include <Qt3DCore/QEntity>
#include <Qt3DRender/QCamera>
#include <Qt3DRender/QCameraLens>
#include <Qt3DCore/QTransform>
#include <Qt3DCore/QAspectEngine>#include <Qt3DInput/QInputAspect>
#include <Qt3DRender/QRenderAspect>
#include <Qt3DExtras/QForwardRenderer>
#include <Qt3DExtras/QPhongMaterial>
#include <Qt3DExtras/QCylinderMesh>
#include <Qt3DExtras/QSphereMesh>
#include <Qt3DExtras/QTorusMesh>#include <QPropertyAnimation>
#include "qt3dwindow.h"
#include "orbittransformcontroller.h"
#include "qorbitcameracontroller.h"
#include <QDebug>
#include <math.h>//Перевод из радиан в градусы
double Deg_Rad(double grad)
{
return (3.14/180)*grad;
}//Перевод из радиан в градусы
double Rad_Deg(double radian)
{
return (180/3.14)*radian;
}
//создает сцену и оси
Qt3DCore::QEntity *createScene()
{
// Root entity
Qt3DCore::QEntity *rootEntity = new Qt3DCore::QEntity;Qt3DCore::QEntity *xaxis = new Qt3DCore::QEntity(rootEntity); Qt3DCore::QEntity *yaxis = new Qt3DCore::QEntity(rootEntity); Qt3DCore::QEntity *zaxis = new Qt3DCore::QEntity(rootEntity); Qt3DExtras::QCylinderMesh* cylinderMesh = new Qt3DExtras::QCylinderMesh; cylinderMesh->setRadius(0.2); cylinderMesh->setLength(90); cylinderMesh->setRings(100); cylinderMesh->setSlices(70); Qt3DExtras::QPhongMaterial* blueMaterial = new Qt3DExtras::QPhongMaterial; blueMaterial->setSpecular(Qt::white); blueMaterial->setShininess(10); blueMaterial->setAmbient(Qt::blue); Qt3DCore::QTransform* xTransform = new Qt3DCore::QTransform(); xTransform->setTranslation(QVector3D(45,0,0)); xTransform->setRotation(QQuaternion::fromAxisAndAngle(QVector3D(0,0,1),90)); Qt3DCore::QTransform* yTransform = new Qt3DCore::QTransform(); yTransform->setTranslation(QVector3D(0,45,0)); Qt3DCore::QTransform* zTransform = new Qt3DCore::QTransform(); zTransform->setTranslation(QVector3D(0,0,45)); zTransform->setRotation(QQuaternion::fromAxisAndAngle(QVector3D(1,0,0),90)); Qt3DExtras::QPhongMaterial* redMaterial = new Qt3DExtras::QPhongMaterial; redMaterial->setSpecular(Qt::white); redMaterial->setShininess(5); redMaterial->setAmbient(Qt::red); Qt3DExtras::QPhongMaterial* greenMaterial = new Qt3DExtras::QPhongMaterial; greenMaterial->setSpecular(Qt::white); greenMaterial->setShininess(10); greenMaterial->setAmbient(Qt::green); Qt3DExtras::QPhongMaterial* blueMateria1l = new Qt3DExtras::QPhongMaterial; blueMateria1l->setSpecular(Qt::white); blueMateria1l->setShininess(10); blueMateria1l->setAmbient(Qt::blue); xaxis->addComponent(cylinderMesh); xaxis->addComponent(redMaterial); xaxis->addComponent(xTransform); yaxis->addComponent(cylinderMesh); yaxis->addComponent(greenMaterial); yaxis->addComponent(yTransform); zaxis->addComponent(cylinderMesh); zaxis->addComponent(blueMaterial); zaxis->addComponent(zTransform); return rootEntity;
}
//Функция создания линни
void creatline(Qt3DCore::QEntity *scene, double x1, double y1,double z1,double x2,double y2 , double z2){// Материал
Qt3DExtras::QPhongMaterial* redMaterial = new Qt3DExtras::QPhongMaterial;
redMaterial->setSpecular(Qt::white);
redMaterial->setShininess(5);
redMaterial->setAmbient(Qt::red);Qt3DExtras::QCylinderMesh* cylinderMesh = new Qt3DExtras::QCylinderMesh; cylinderMesh->setRadius(0.2); cylinderMesh->setLength(90); cylinderMesh->setRings(100); cylinderMesh->setSlices(70); // шар для наглядности вращения // Material Qt3DRender::QMaterial *material = new Qt3DExtras::QPhongMaterial(scene); // Sphere Qt3DCore::QEntity *sphereEntity = new Qt3DCore::QEntity(scene); Qt3DExtras::QSphereMesh *sphereMesh = new Qt3DExtras::QSphereMesh; sphereMesh->setRadius(1); Qt3DCore::QTransform *sphereTransform = new Qt3DCore::QTransform; OrbitTransformController *controller = new OrbitTransformController(sphereTransform); controller->setTarget(sphereTransform); sphereTransform->setTranslation(QVector3D(x2,y2,z2)); sphereEntity->addComponent(sphereMesh); sphereEntity->addComponent(sphereTransform); sphereEntity->addComponent(material); // Qt3DCore::QEntity *test = new Qt3DCore::QEntity(scene); test->addComponent(redMaterial); test->addComponent(cylinderMesh); Qt3DCore::QTransform* Transform = new Qt3DCore::QTransform(); // Установка координат Transform->setTranslation(QVector3D(x1,y1,z1)); //Вращение линнии Transform->setRotation(QQuaternion::rotationTo(QVector3D(x1,y1,z1),QVector3D(x2,y2,y2))); qDebug()<<Transform->rotationX(); qDebug()<<Transform->rotationY(); qDebug()<<Transform->rotationZ(); test->addComponent(Transform);
}
int main(int argc, char* argv[])
{
QGuiApplication app(argc, argv);
Qt3DExtras::Qt3DWindow view;Qt3DCore::QEntity *scene = createScene(); //функция создания линии creatline(scene,0,0,0,0,0,20); // Camera Qt3DRender::QCamera *camera = view.camera(); camera->lens()->setPerspectiveProjection(45.0f, 16.0f/9.0f, 0.1f, 1000.0f); camera->setPosition(QVector3D(0, 0, 40.0f)); camera->setViewCenter(QVector3D(0, 0, 0)); // For camera controls Qt3DExtras::QOrbitCameraController *camController = new Qt3DExtras::QOrbitCameraController(scene); camController->setLinearSpeed( 50.0f ); camController->setLookSpeed( 180.0f ); camController->setCamera(camera); view.setRootEntity(scene); view.show(); return app.exec();
}
-
I use this code to connect cilinders to spheres:
QListQt3DExtras::QCylinderMesh* cylinderListMesh;
QListQt3DCore::QTransform* cylinderListTransform;
QListQt3DCore::QTransform* sphereListTransform;void ShowMolecule::atomsConnections(int atomA, int atomB, int bondI)
{
QVector3D rAB = sphereListTransform[atomA]->translation()-sphereListTransform[atomB]->translation();
cylinderListTransform[bondI]->setRotation(QQuaternion::rotationTo(QVector3D(0,1,0), rAB));
cylinderListMesh[bondI]->setLength(rAB.length());
cylinderListTransform[bondI]->setTranslation(QVector3D(
sphereListTransform[atomB]->translation()+rAB/2.0e0));
}