FreeCAD

Форк
0
/
Camera.cpp 
201 строка · 5.5 Кб
1
// SPDX-License-Identifier: LGPL-2.1-or-later
2

3
/***************************************************************************
4
 *   Copyright (c) 2023 Werner Mayer <wmayer[at]users.sourceforge.net>     *
5
 *                                                                         *
6
 *   This file is part of FreeCAD.                                         *
7
 *                                                                         *
8
 *   FreeCAD is free software: you can redistribute it and/or modify it    *
9
 *   under the terms of the GNU Lesser General Public License as           *
10
 *   published by the Free Software Foundation, either version 2.1 of the  *
11
 *   License, or (at your option) any later version.                       *
12
 *                                                                         *
13
 *   FreeCAD is distributed in the hope that it will be useful, but        *
14
 *   WITHOUT ANY WARRANTY; without even the implied warranty of            *
15
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      *
16
 *   Lesser General Public License for more details.                       *
17
 *                                                                         *
18
 *   You should have received a copy of the GNU Lesser General Public      *
19
 *   License along with FreeCAD. If not, see                               *
20
 *   <https://www.gnu.org/licenses/>.                                      *
21
 *                                                                         *
22
 **************************************************************************/
23

24
#include "PreCompiled.h"
25

26
#include "Camera.h"
27
#include "Utilities.h"
28

29
using namespace Gui;
30

31

32
/**
33
 Formulas to get quaternion for axonometric views:
34

35
 \code
36
from math import sqrt, degrees, asin, atan
37
p1=App.Rotation(App.Vector(1,0,0),90)
38
p2=App.Rotation(App.Vector(0,0,1),alpha)
39
p3=App.Rotation(p2.multVec(App.Vector(1,0,0)),beta)
40
p4=p3.multiply(p2).multiply(p1)
41

42
from pivy import coin
43
c=Gui.ActiveDocument.ActiveView.getCameraNode()
44
c.orientation.setValue(*p4.Q)
45
 \endcode
46

47
 The angles alpha and beta depend on the type of axonometry
48
 Isometric:
49
 \code
50
alpha=45
51
beta=degrees(asin(-sqrt(1.0/3.0)))
52
 \endcode
53

54
 Dimetric:
55
 \code
56
alpha=degrees(asin(sqrt(1.0/8.0)))
57
beta=degrees(-asin(1.0/3.0))
58
 \endcode
59

60
 Trimetric:
61
 \code
62
alpha=30.0
63
beta=-35.0
64
 \endcode
65

66
 Verification code that the axonomtries are correct:
67

68
 \code
69
from pivy import coin
70
c=Gui.ActiveDocument.ActiveView.getCameraNode()
71
vo=App.Vector(c.getViewVolume().getMatrix().multVecMatrix(coin.SbVec3f(0,0,0)).getValue())
72
vx=App.Vector(c.getViewVolume().getMatrix().multVecMatrix(coin.SbVec3f(10,0,0)).getValue())
73
vy=App.Vector(c.getViewVolume().getMatrix().multVecMatrix(coin.SbVec3f(0,10,0)).getValue())
74
vz=App.Vector(c.getViewVolume().getMatrix().multVecMatrix(coin.SbVec3f(0,0,10)).getValue())
75
(vx-vo).Length
76
(vy-vo).Length
77
(vz-vo).Length
78

79
# Projection
80
vo.z=0
81
vx.z=0
82
vy.z=0
83
vz.z=0
84

85
(vx-vo).Length
86
(vy-vo).Length
87
(vz-vo).Length
88
 \endcode
89

90
 See also:
91
 http://www.mathematik.uni-marburg.de/~thormae/lectures/graphics1/graphics_6_2_ger_web.html#1
92
 http://www.mathematik.uni-marburg.de/~thormae/lectures/graphics1/code_v2/Axonometric/qt/Axonometric.cpp
93
 https://de.wikipedia.org/wiki/Arkussinus_und_Arkuskosinus
94
*/
95

96
SbRotation Camera::top()
97
{
98
    return {0, 0, 0, 1};
99
}
100

101
SbRotation Camera::bottom()
102
{
103
    return {1, 0, 0, 0};
104
}
105

106
SbRotation Camera::front()
107
{
108
    auto root = sqrtf(2.0)/2.0f;
109
    return {root, 0, 0, root};
110
}
111

112
SbRotation Camera::rear()
113
{
114
    auto root = sqrtf(2.0)/2.0f;
115
    return {0, root, root, 0};
116
}
117

118
SbRotation Camera::right()
119
{
120
    return {0.5, 0.5, 0.5, 0.5};
121
}
122

123
SbRotation Camera::left()
124
{
125
    return {-0.5, 0.5, 0.5, -0.5};
126
}
127

128
SbRotation Camera::isometric()
129
{
130
    //from math import sqrt, degrees, asin
131
    //p1=App.Rotation(App.Vector(1,0,0),45)
132
    //p2=App.Rotation(App.Vector(0,0,1),-45)
133
    //p3=p2.multiply(p1)
134
    //return SbRotation(0.353553f, -0.146447f, -0.353553f, 0.853553f);
135

136
    //from math import sqrt, degrees, asin
137
    //p1=App.Rotation(App.Vector(1,0,0),90)
138
    //p2=App.Rotation(App.Vector(0,0,1),135)
139
    //p3=App.Rotation(App.Vector(-1,1,0),degrees(asin(-sqrt(1.0/3.0))))
140
    //p4=p3.multiply(p2).multiply(p1)
141
    //return SbRotation(0.17592, 0.424708, 0.820473, 0.339851);
142

143
    //from math import sqrt, degrees, asin
144
    //p1=App.Rotation(App.Vector(1,0,0),90)
145
    //p2=App.Rotation(App.Vector(0,0,1),45)
146
    //#p3=App.Rotation(App.Vector(1,1,0),45)
147
    //p3=App.Rotation(App.Vector(1,1,0),degrees(asin(-sqrt(1.0/3.0))))
148
    //p4=p3.multiply(p2).multiply(p1)
149
    return {0.424708F, 0.17592F, 0.339851F, 0.820473F};
150
}
151

152
SbRotation Camera::dimetric()
153
{
154
    return {0.567952F, 0.103751F, 0.146726F, 0.803205F};
155
}
156

157
SbRotation Camera::trimetric()
158
{
159
    return {0.446015F, 0.119509F, 0.229575F, 0.856787F};
160
}
161

162
SbRotation Camera::rotation(Camera::Orientation view)
163
{
164
    switch (view) {
165
    case Top:
166
        return top();
167
    case Bottom:
168
        return bottom();
169
    case Front:
170
        return front();
171
    case Rear:
172
        return rear();
173
    case Right:
174
        return right();
175
    case Left:
176
        return left();
177
    case Isometric:
178
        return isometric();
179
    case Dimetric:
180
        return dimetric();
181
    case Trimetric:
182
        return trimetric();
183
    default:
184
        return top();
185
    }
186
}
187

188
Base::Rotation Camera::convert(Camera::Orientation view)
189
{
190
    return convert(Camera::rotation(view));
191
}
192

193
Base::Rotation Camera::convert(const SbRotation& rot)
194
{
195
    return Base::convertTo<Base::Rotation>(rot);
196
}
197

198
SbRotation Camera::convert(const Base::Rotation& rot)
199
{
200
    return Base::convertTo<SbRotation>(rot);
201
}
202

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.