Port left unported cel commands to eigen

pull/110/head
Hleb Valoshka 2018-07-28 21:40:23 +03:00
parent 5f05bd711a
commit f09115b34c
3 changed files with 205 additions and 4 deletions

View File

@ -22,6 +22,7 @@
#include <celestia/celx_internal.h>
#include <algorithm>
#include <cstdio>
#include <Eigen/Geometry>
// Older gcc versions used <strstream> instead of <sstream>.
// This has been corrected in GCC 3.2, but name clashing must
@ -224,12 +225,20 @@ Command* CommandParser::parseCommand()
if (paramList->getString("upframe", frameString))
upFrame = parseCoordinateSystem(frameString);
#ifdef __CELVEC__
Vec3d up(0, 1, 0);
#else
Eigen::Vector3d up(Eigen::Vector3d::UnitY());
#endif
paramList->getVector("up", up);
cmd = new CommandGoto(t,
distance,
#ifdef __CELVEC__
Vec3f((float) up.x, (float) up.y, (float) up.z),
#else
up.cast<float>(),
#endif
upFrame);
}
else if (commandName == "gotolonglat")
@ -238,7 +247,11 @@ Command* CommandParser::parseCommand()
paramList->getNumber("time", t);
double distance = 5.0;
paramList->getNumber("distance", distance);
#ifdef __CELVEC__
Vec3d up(0, 1, 0);
#else
Eigen::Vector3d up(Eigen::Vector3d::UnitY());
#endif
paramList->getVector("up", up);
double longitude;
paramList->getNumber("longitude", longitude);
@ -249,13 +262,21 @@ Command* CommandParser::parseCommand()
distance,
(float) degToRad(longitude),
(float) degToRad(latitude),
#ifdef __CELVEC__
Vec3f((float) up.x, (float) up.y, (float) up.z));
#else
up.cast<float>());
#endif
}
else if (commandName == "gotoloc")
{
double t = 1.0;
paramList->getNumber("time", t);
#ifdef __CELVEC__
Vec3d pos(0, 1, 0);
#else
Eigen::Vector3d pos(Eigen::Vector3d::UnitY());
#endif
if (paramList->getVector("position", pos))
{
pos = pos * astro::kilometersToMicroLightYears(1.0);
@ -266,11 +287,18 @@ Command* CommandParser::parseCommand()
double zrot = 0.0;
paramList->getNumber("zrot", zrot);
zrot = degToRad(zrot);
#ifdef __CELVEC__
Quatf rotation = Quatf::xrotation((float) degToRad(xrot)) *
Quatf::yrotation((float) degToRad(yrot)) *
Quatf::zrotation((float) degToRad(zrot));
cmd = new CommandGotoLocation(t, Point3d(0.0, 0.0, 0.0) + pos,
rotation);
#else
auto rotation = Eigen::Quaterniond(Eigen::AngleAxisd(degToRad(xrot), Eigen::Vector3d::UnitX()) *
Eigen::AngleAxisd(degToRad(yrot), Eigen::Vector3d::UnitY()) *
Eigen::AngleAxisd(degToRad(zrot), Eigen::Vector3d::UnitZ()));
cmd = new CommandGotoLocation(t, pos, rotation);
#endif
}
else
{
@ -283,9 +311,15 @@ Command* CommandParser::parseCommand()
paramList->getNumber("ox", ox);
paramList->getNumber("oy", oy);
paramList->getNumber("oz", oz);
#ifdef __CELVEC__
Quatf orientation((float)ow, (float)ox, (float)oy, (float)oz);
cmd = new CommandGotoLocation(t, Point3d((double)BigFix(x), (double)BigFix(y), (double)BigFix(z)),
orientation);
#else
Eigen::Quaterniond orientation(ow, ox, oy, oz);
cmd = new CommandGotoLocation(t, Eigen::Vector3d((double)BigFix(x), (double)BigFix(y), (double)BigFix(z)),
orientation);
#endif
}
}
else if (commandName == "seturl")
@ -434,29 +468,49 @@ Command* CommandParser::parseCommand()
{
double rate = 0.0;
double duration = 1.0;
#ifdef __CELVEC__
Vec3d axis;
#else
Eigen::Vector3d axis(Eigen::Vector3d::Zero());
#endif
paramList->getNumber("duration", duration);
paramList->getNumber("rate", rate);
paramList->getVector("axis", axis);
cmd = new CommandOrbit(duration,
#ifdef __CELVEC__
Vec3f((float) axis.x, (float) axis.y, (float) axis.z),
#else
axis.cast<float>(),
#endif
(float) degToRad(rate));
}
else if (commandName == "rotate")
{
double rate = 0.0;
double duration = 1.0;
#ifdef __CELVEC__
Vec3d axis;
#else
Eigen::Vector3d axis(Eigen::Vector3d::Zero());
#endif
paramList->getNumber("duration", duration);
paramList->getNumber("rate", rate);
paramList->getVector("axis", axis);
cmd = new CommandRotate(duration,
#ifdef __CELVEC__
Vec3f((float) axis.x, (float) axis.y, (float) axis.z),
#else
axis.cast<float>(),
#endif
(float) degToRad(rate));
}
else if (commandName == "move")
{
#ifdef __CELVEC__
Vec3d velocity;
#else
Eigen::Vector3d velocity(Eigen::Vector3d::Zero());
#endif
double duration;
paramList->getNumber("duration", duration);
paramList->getVector("velocity", velocity);
@ -488,13 +542,23 @@ Command* CommandParser::parseCommand()
}
else if (commandName == "setorientation")
{
#ifdef __CELVEC__
Vec3d axis;
#else
Eigen::Vector3d axis(Eigen::Vector3d::Zero());
#endif
double angle;
if (paramList->getNumber("angle", angle))
{
paramList->getVector("axis", axis);
#ifdef __CELVEC__
cmd = new CommandSetOrientation(Vec3f((float) axis.x, (float) axis.y, (float) axis.z),
(float) degToRad(angle));
#else
auto orientation = Eigen::Quaternionf(Eigen::AngleAxisf((float) degToRad(angle),
axis.cast<float>().normalized()));
cmd = new CommandSetOrientation(orientation);
#endif
}
else
{
@ -503,11 +567,16 @@ Command* CommandParser::parseCommand()
paramList->getNumber("ox", ox);
paramList->getNumber("oy", oy);
paramList->getNumber("oz", oz);
#ifdef __CELVEC__
Quatf orientation((float)ow, (float)ox, (float)oy, (float)oz);
Vec3f axis;
float angle;
orientation.getAxisAngle(axis, angle);
cmd = new CommandSetOrientation(axis, angle);
#else
Eigen::Quaternionf orientation(ow, ox, oy, oz);
cmd = new CommandSetOrientation(orientation);
#endif
}
}
else if (commandName == "lookback")

View File

@ -18,10 +18,14 @@
#include <celutil/util.h>
#include <iostream>
#include <utility>
#include "eigenport.h"
#include <algorithm>
#include <Eigen/Geometry>
#ifdef __CELVEC__
#include "eigenport.h"
#endif
using namespace std;
using namespace Eigen;
////////////////
@ -56,7 +60,11 @@ void CommandSelect::process(ExecutionEnvironment& env)
CommandGoto::CommandGoto(double t,
double dist,
#ifdef __CELVEC__
Vec3f _up,
#else
Eigen::Vector3f _up,
#endif
ObserverFrame::CoordinateSystem _upFrame) :
gotoTime(t), distance(dist), up(_up), upFrame(_upFrame)
{
@ -67,7 +75,11 @@ void CommandGoto::process(ExecutionEnvironment& env)
Selection sel = env.getSimulation()->getSelection();
env.getSimulation()->gotoSelection(gotoTime,
sel.radius() * distance,
#ifdef __CELVEC__
toEigen(up), upFrame);
#else
up, upFrame);
#endif
}
@ -78,7 +90,11 @@ CommandGotoLongLat::CommandGotoLongLat(double t,
double dist,
float _longitude,
float _latitude,
#ifdef __CELVEC__
Vec3f _up) :
#else
Eigen::Vector3f _up) :
#endif
gotoTime(t),
distance(dist),
longitude(_longitude),
@ -93,7 +109,11 @@ void CommandGotoLongLat::process(ExecutionEnvironment& env)
env.getSimulation()->gotoSelectionLongLat(gotoTime,
sel.radius() * distance,
longitude, latitude,
#ifdef __CELVEC__
toEigen(up));
#else
up);
#endif
}
@ -101,17 +121,27 @@ void CommandGotoLongLat::process(ExecutionEnvironment& env)
// GotoLocation
CommandGotoLocation::CommandGotoLocation(double t,
#ifdef __CELVEC__
const Point3d& _translation,
const Quatf& _rotation) :
#else
const Eigen::Vector3d& _translation,
const Eigen::Quaterniond& _rotation) :
#endif
gotoTime(t), translation(_translation), rotation(_rotation)
{
}
void CommandGotoLocation::process(ExecutionEnvironment& env)
{
#ifdef __CELVEC__
Quatd toOrientation = Quatd(rotation.w, rotation.x, rotation.y, rotation.z);
UniversalCoord toPosition = UniversalCoord::CreateUly(toEigen(translation));
env.getSimulation()->gotoLocation(toPosition, toEigen(toOrientation), gotoTime);
#else
UniversalCoord toPosition = UniversalCoord::CreateUly(translation);
env.getSimulation()->gotoLocation(toPosition, rotation, gotoTime);
#endif
}
/////////////////////////////
@ -312,9 +342,13 @@ void CommandChangeDistance::process(ExecutionEnvironment& env, double /*unused*/
////////////////
// Oribt command: rotate about the selected object
// Orbit command: rotate about the selected object
#ifdef __CELVEC__
CommandOrbit::CommandOrbit(double _duration, const Vec3f& axis, float rate) :
#else
CommandOrbit::CommandOrbit(double _duration, const Eigen::Vector3f& axis, float rate) :
#endif
TimedCommand(_duration),
spin(axis * rate)
{
@ -322,17 +356,29 @@ CommandOrbit::CommandOrbit(double _duration, const Vec3f& axis, float rate) :
void CommandOrbit::process(ExecutionEnvironment& env, double /*unused*/, double dt)
{
#ifdef __CELVEC__
float v = spin.length();
#else
float v = spin.norm();
#endif
if (v != 0.0f)
{
#ifdef __CELVEC__
Quatf q;
q.setAxisAngle(spin / v, (float) (v * dt));
env.getSimulation()->orbit(toEigen(q));
#else
auto q = Quaternionf(AngleAxisf((float) (v * dt), (spin / v).normalized()));
env.getSimulation()->orbit(q);
#endif
}
}
#ifdef __CELVEC__
CommandRotate::CommandRotate(double _duration, const Vec3f& axis, float rate) :
#else
CommandRotate::CommandRotate(double _duration, const Eigen::Vector3f& axis, float rate) :
#endif
TimedCommand(_duration),
spin(axis * rate)
{
@ -340,17 +386,30 @@ CommandRotate::CommandRotate(double _duration, const Vec3f& axis, float rate) :
void CommandRotate::process(ExecutionEnvironment& env, double /*unused*/, double dt)
{
#ifdef __CELVEC__
float v = spin.length();
#else
float v = spin.norm();
#endif
if (v != 0.0f)
{
#ifdef __CELVEC__
Quatf q;
q.setAxisAngle(spin / v, (float) (v * dt));
env.getSimulation()->rotate(toEigen(q));
#else
auto q = Quaternionf(AngleAxisf((float) (v * dt), (spin / v).normalized()));
env.getSimulation()->rotate(q);
#endif
}
}
#ifdef __CELVEC__
CommandMove::CommandMove(double _duration, const Vec3d& _velocity) :
#else
CommandMove::CommandMove(double _duration, const Eigen::Vector3d& _velocity) :
#endif
TimedCommand(_duration),
velocity(_velocity)
{
@ -358,7 +417,11 @@ CommandMove::CommandMove(double _duration, const Vec3d& _velocity) :
void CommandMove::process(ExecutionEnvironment& env, double /*unused*/, double dt)
{
#ifdef __CELVEC__
Eigen::Vector3d velocityKm = toEigen(velocity) * dt * astro::microLightYearsToKilometers(1.0);
#else
Eigen::Vector3d velocityKm = velocity * dt * astro::microLightYearsToKilometers(1.0);
#endif
env.getSimulation()->setObserverPosition(env.getSimulation()->getObserver().getPosition().offsetKm(velocityKm));
}
@ -379,16 +442,25 @@ void CommandSetPosition::process(ExecutionEnvironment& env)
////////////////
// Set orientation command: set the orientation of the camera
#ifdef __CELVEC__
CommandSetOrientation::CommandSetOrientation(const Vec3f& _axis, float _angle) :
axis(_axis), angle(_angle)
#else
CommandSetOrientation::CommandSetOrientation(const Quaternionf& _orientation) :
orientation(_orientation)
#endif
{
}
void CommandSetOrientation::process(ExecutionEnvironment& env)
{
#ifdef __CELVEC__
Quatf q(1);
q.setAxisAngle(axis, angle);
env.getSimulation()->setObserverOrientation(toEigen(q));
#else
env.getSimulation()->setObserverOrientation(orientation);
#endif
}
////////////////

View File

@ -81,14 +81,23 @@ class CommandGoto : public InstantaneousCommand
{
public:
CommandGoto(double t, double dist,
Vec3f _up, ObserverFrame::CoordinateSystem _upFrame);
#ifdef __CELVEC__
Vec3f _up,
#else
Eigen::Vector3f _up,
#endif
ObserverFrame::CoordinateSystem _upFrame);
~CommandGoto() = default;
void process(ExecutionEnvironment&);
private:
double gotoTime;
double distance;
#ifdef __CELVEC__
Vec3f up;
#else
Eigen::Vector3f up;
#endif
ObserverFrame::CoordinateSystem upFrame;
};
@ -99,7 +108,11 @@ class CommandGotoLongLat : public InstantaneousCommand
CommandGotoLongLat(double t,
double dist,
float _longitude, float _latitude,
#ifdef __CELVEC__
Vec3f _up);
#else
Eigen::Vector3f _up);
#endif
~CommandGotoLongLat() = default;
void process(ExecutionEnvironment&);
@ -108,7 +121,11 @@ class CommandGotoLongLat : public InstantaneousCommand
double distance;
float longitude;
float latitude;
#ifdef __CELVEC__
Vec3f up;
#else
Eigen::Vector3f up;
#endif
};
@ -116,14 +133,25 @@ class CommandGotoLocation : public InstantaneousCommand
{
public:
CommandGotoLocation(double t,
#ifdef __CELVEC__
const Point3d& translation, const Quatf& rotation);
#else
const Eigen::Vector3d& translation,
const Eigen::Quaterniond& rotation);
#endif
~CommandGotoLocation() = default;
void process(ExecutionEnvironment&);
private:
double gotoTime;
#ifdef __CELVEC__
Point3d translation;
Quatf rotation;
#else
Eigen::Vector3d translation;
Eigen::Quaterniond rotation;
#endif
};
class CommandSetUrl : public InstantaneousCommand
@ -315,33 +343,57 @@ class CommandChangeDistance : public TimedCommand
class CommandOrbit : public TimedCommand
{
public:
#ifdef __CELVEC__
CommandOrbit(double _duration, const Vec3f& axis, float rate);
#else
CommandOrbit(double _duration, const Eigen::Vector3f& axis, float rate);
#endif
void process(ExecutionEnvironment&, double t, double dt);
private:
#ifdef __CELVEC__
Vec3f spin;
#else
Eigen::Vector3f spin;
#endif
};
class CommandRotate : public TimedCommand
{
public:
#ifdef __CELVEC__
CommandRotate(double _duration, const Vec3f& axis, float rate);
#else
CommandRotate(double _duration, const Eigen::Vector3f& axis, float rate);
#endif
void process(ExecutionEnvironment&, double t, double dt);
private:
#ifdef __CELVEC__
Vec3f spin;
#else
Eigen::Vector3f spin;
#endif
};
class CommandMove : public TimedCommand
{
public:
#ifdef __CELVEC__
CommandMove(double _duration, const Vec3d& _velocity);
#else
CommandMove(double _duration, const Eigen::Vector3d& _velocity);
#endif
void process(ExecutionEnvironment&, double t, double dt);
private:
#ifdef __CELVEC__
Vec3d velocity;
#else
Eigen::Vector3d velocity;
#endif
};
@ -359,12 +411,20 @@ class CommandSetPosition : public InstantaneousCommand
class CommandSetOrientation : public InstantaneousCommand
{
public:
#ifdef __CELVEC__
CommandSetOrientation(const Vec3f&, float);
#else
CommandSetOrientation(const Eigen::Quaternionf&);
#endif
void process(ExecutionEnvironment&);
private:
#ifdef __CELVEC__
Vec3f axis;
float angle;
#else
Eigen::Quaternionf orientation;
#endif
};
class CommandLookBack : public InstantaneousCommand