File size: 6,635 Bytes
985c397 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 | // SPDX-License-Identifier: LGPL-2.1-or-later
#include <gtest/gtest.h>
#include <array>
#include <cmath>
#include <Inventor/SbMatrix.h>
#include <Inventor/SbRotation.h>
#include <Inventor/SbViewVolume.h>
#include <Base/Converter.h>
#include <Base/Tools.h>
#include <Gui/Camera.h>
#include <Gui/Utilities.h>
#include <src/App/InitApplication.h>
/*
This comment was previously used to get the hard coded axonometric view quaternions
in the Camera class.
This has since been replaced with unit tests that verify the correctness of the
quaternion calculations.
The old code is kept for reference and to show how the quaternions were calculated.
---
Formulas to get quaternion for axonometric views:
\code
from math import sqrt, degrees, asin, atan
p1=App.Rotation(App.Vector(1,0,0),90)
p2=App.Rotation(App.Vector(0,0,1),alpha)
p3=App.Rotation(p2.multVec(App.Vector(1,0,0)),beta)
p4=p3.multiply(p2).multiply(p1)
from pivy import coin
c=Gui.ActiveDocument.ActiveView.getCameraNode()
c.orientation.setValue(*p4.Q)
\endcode
The angles alpha and beta depend on the type of axonometry
Isometric:
\code
alpha=45
beta=degrees(asin(-sqrt(1.0/3.0)))
\endcode
Dimetric:
\code
alpha=degrees(asin(sqrt(1.0/8.0)))
beta=degrees(-asin(1.0/3.0))
\endcode
Trimetric:
\code
alpha=30.0
beta=-35.0
\endcode
Verification code that the axonomtries are correct:
\code
from pivy import coin
c=Gui.ActiveDocument.ActiveView.getCameraNode()
vo=App.Vector(c.getViewVolume().getMatrix().multVecMatrix(coin.SbVec3f(0,0,0)).getValue())
vx=App.Vector(c.getViewVolume().getMatrix().multVecMatrix(coin.SbVec3f(10,0,0)).getValue())
vy=App.Vector(c.getViewVolume().getMatrix().multVecMatrix(coin.SbVec3f(0,10,0)).getValue())
vz=App.Vector(c.getViewVolume().getMatrix().multVecMatrix(coin.SbVec3f(0,0,10)).getValue())
(vx-vo).Length
(vy-vo).Length
(vz-vo).Length
# Projection
vo.z=0
vx.z=0
vy.z=0
vz.z=0
(vx-vo).Length
(vy-vo).Length
(vz-vo).Length
\endcode
See also:
http://www.mathematik.uni-marburg.de/~thormae/lectures/graphics1/graphics_6_2_ger_web.html#1
http://www.mathematik.uni-marburg.de/~thormae/lectures/graphics1/code_v2/Axonometric/qt/Axonometric.cpp
https://de.wikipedia.org/wiki/Arkussinus_und_Arkuskosinus
*/
using Base::convertTo;
using Base::Rotation;
using Base::toRadians;
using Base::Vector3d;
namespace
{
Rotation buildAxonometricRotation(double alphaRad, double betaRad)
{
const auto p1 = Rotation(Vector3d::UnitX, toRadians<float>(90.0));
const auto p2 = Rotation(Vector3d::UnitZ, alphaRad);
const auto p3 = Rotation(p2.multVec(Vector3d::UnitX), betaRad);
const auto p4 = p3 * p2 * p1;
return p4;
}
// Returns a tuple of 2D lengths of X, Y, Z unit vectors after applying rotation
std::array<double, 3> getProjectedLengths(const SbRotation& rot)
{
// Set up a simple view volume to test the projection of the unit vectors.
// The actual values don't matter much, as we are only interested in the
// relative lengths of the projected vectors.
SbViewVolume volume;
// left, right, bottom, top, near, far
volume.ortho(-10, 10, -10, 10, -10, 10);
volume.rotateCamera(rot);
const auto matrix = volume.getMatrix();
// Get the transformed unit vectors
SbVec3f vo, vx, vy, vz;
matrix.multVecMatrix(SbVec3f(0, 0, 0), vo);
matrix.multVecMatrix(SbVec3f(10, 0, 0), vx);
matrix.multVecMatrix(SbVec3f(0, 10, 0), vy);
matrix.multVecMatrix(SbVec3f(0, 0, 10), vz);
// Project to XY plane by setting Z to 0
vo[2] = 0;
vx[2] = 0;
vy[2] = 0;
vz[2] = 0;
// Return the lengths of the projected vectors
return {(vx - vo).length(), (vy - vo).length(), (vz - vo).length()};
}
} // namespace
class CameraPrecalculatedQuaternions: public ::testing::Test
{
protected:
static void SetUpTestSuite()
{
tests::initApplication();
}
};
TEST_F(CameraPrecalculatedQuaternions, testIsometric)
{
// Use the formula to get the isometric rotation
double alpha = toRadians(45.0f);
double beta = std::asin(-std::sqrt(1.0 / 3.0));
const Rotation actual = buildAxonometricRotation(alpha, beta);
const Rotation expected = convertTo<Rotation>(Gui::Camera::isometric());
EXPECT_TRUE(actual.isSame(expected, 1e-6));
}
TEST_F(CameraPrecalculatedQuaternions, testDimetric)
{
// Use the formula to get the dimetric rotation
double alpha = std::asin(std::sqrt(1.0 / 8.0));
double beta = -std::asin(1.0 / 3.0);
const Rotation actual = buildAxonometricRotation(alpha, beta);
const Rotation expected = convertTo<Rotation>(Gui::Camera::dimetric());
EXPECT_TRUE(actual.isSame(expected, 1e-6));
}
TEST_F(CameraPrecalculatedQuaternions, testTrimetric)
{
// Use the formula to get the trimetric rotation
double alpha = toRadians(30.0);
double beta = toRadians(-35.0);
const Rotation actual = buildAxonometricRotation(alpha, beta);
const Rotation expected = convertTo<Rotation>(Gui::Camera::trimetric());
EXPECT_TRUE(actual.isSame(expected, 1e-6));
}
class CameraRotation: public ::testing::Test
{
protected:
static void SetUpTestSuite()
{
tests::initApplication();
}
};
TEST_F(CameraRotation, testIsometricProjection)
{
auto rot = Gui::Camera::isometric();
auto lengths = getProjectedLengths(rot);
// In isometric, expect all lengths to be roughly equal
EXPECT_NEAR(lengths[0], lengths[1], 1e-6); // X == Y
EXPECT_NEAR(lengths[0], lengths[2], 1e-6); // X == Z
EXPECT_NEAR(lengths[1], lengths[2], 1e-6); // Y == Z
}
TEST_F(CameraRotation, testDimetricProjection)
{
const auto rot = Gui::Camera::dimetric();
const auto lengths = getProjectedLengths(rot);
// In dimetric, expect two lengths to be roughly equal, one different
const std::initializer_list<std::pair<double, double>> pairs = {
{lengths[0], lengths[1]},
{lengths[1], lengths[2]},
{lengths[0], lengths[2]},
};
constexpr double tolerance = 1e-6;
const auto isSimilar = [&](std::pair<double, double> lengths) -> bool {
return std::abs(lengths.first - lengths.second) < tolerance;
};
unsigned similarCount = std::ranges::count_if(pairs, isSimilar);
EXPECT_EQ(similarCount, 1); // Exactly two are equal
}
TEST_F(CameraRotation, testTrimetricProjection)
{
auto rot = Gui::Camera::trimetric();
auto lengths = getProjectedLengths(rot);
// In trimetric, all should differ significantly
EXPECT_GT(std::abs(lengths[0] - lengths[1]), 1e-3);
EXPECT_GT(std::abs(lengths[1] - lengths[2]), 1e-3);
EXPECT_GT(std::abs(lengths[0] - lengths[2]), 1e-3);
}
|