| 1 |
/*************************************************************************** |
| 2 |
* Copyright (c) 2010 Werner Mayer <wmayer[at]users.sourceforge.net> * |
| 3 |
* * |
| 4 |
* This file is part of the FreeCAD CAx development system. * |
| 5 |
* * |
| 6 |
* This library is free software; you can redistribute it and/or * |
| 7 |
* modify it under the terms of the GNU Library General Public * |
| 8 |
* License as published by the Free Software Foundation; either * |
| 9 |
* version 2 of the License, or (at your option) any later version. * |
| 10 |
* * |
| 11 |
* This library is distributed in the hope that it will be useful, * |
| 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 14 |
* GNU Library General Public License for more details. * |
| 15 |
* * |
| 16 |
* You should have received a copy of the GNU Library General Public * |
| 17 |
* License along with this library; see the file COPYING.LIB. If not, * |
| 18 |
* write to the Free Software Foundation, Inc., 59 Temple Place, * |
| 19 |
* Suite 330, Boston, MA 02111-1307, USA * |
| 20 |
* * |
| 21 |
***************************************************************************/ |
| 22 |
|
| 23 |
#ifndef GUI_UTILITIES_H |
| 24 |
#define GUI_UTILITIES_H |
| 25 |
|
| 26 |
#include <vector> |
| 27 |
#include <QColor> |
| 28 |
#include <App/Material.h> |
| 29 |
#include <Base/Converter.h> |
| 30 |
#include <Base/ViewProj.h> |
| 31 |
#include <Inventor/SbColor.h> |
| 32 |
#include <Inventor/SbColor4f.h> |
| 33 |
#include <Inventor/SbMatrix.h> |
| 34 |
#include <Inventor/SbRotation.h> |
| 35 |
#include <Inventor/SbVec2f.h> |
| 36 |
#include <Inventor/SbViewVolume.h> |
| 37 |
|
| 38 |
class SbViewVolume; |
| 39 |
class QAbstractItemView; |
| 40 |
|
| 41 |
namespace Base |
| 42 |
{ |
| 43 |
// Specialization for SbVec3f |
| 44 |
template<> |
| 45 |
struct vec_traits<SbVec3f> |
| 46 |
{ |
| 47 |
using vec_type = SbVec3f; |
| 48 |
using float_type = float; |
| 49 |
explicit vec_traits(const vec_type& v) |
| 50 |
: v(v) |
| 51 |
{} |
| 52 |
inline std::tuple<float_type, float_type, float_type> get() const |
| 53 |
{ |
| 54 |
return std::make_tuple(v[0], v[1], v[2]); |
| 55 |
} |
| 56 |
|
| 57 |
private: |
| 58 |
const vec_type& v; |
| 59 |
}; |
| 60 |
|
| 61 |
// Specialization for SbVec3d |
| 62 |
template<> |
| 63 |
struct vec_traits<SbVec3d> |
| 64 |
{ |
| 65 |
using vec_type = SbVec3d; |
| 66 |
using float_type = double; |
| 67 |
explicit vec_traits(const vec_type& v) |
| 68 |
: v(v) |
| 69 |
{} |
| 70 |
inline std::tuple<float_type, float_type, float_type> get() const |
| 71 |
{ |
| 72 |
return std::make_tuple(v[0], v[1], v[2]); |
| 73 |
} |
| 74 |
|
| 75 |
private: |
| 76 |
const vec_type& v; |
| 77 |
}; |
| 78 |
|
| 79 |
// Specialization for SbRotation |
| 80 |
template<> |
| 81 |
struct vec_traits<SbRotation> |
| 82 |
{ |
| 83 |
using vec_type = SbRotation; |
| 84 |
using float_type = float; |
| 85 |
explicit vec_traits(const vec_type& v) |
| 86 |
: v(v) |
| 87 |
{} |
| 88 |
inline std::tuple<float_type, float_type, float_type, float_type> get() const |
| 89 |
{ |
| 90 |
float_type q1, q2, q3, q4; |
| 91 |
v.getValue(q1, q2, q3, q4); |
| 92 |
return std::make_tuple(q1, q2, q3, q4); |
| 93 |
} |
| 94 |
|
| 95 |
private: |
| 96 |
const vec_type& v; |
| 97 |
}; |
| 98 |
|
| 99 |
// Specialization for SbColor |
| 100 |
template<> |
| 101 |
struct vec_traits<SbColor> |
| 102 |
{ |
| 103 |
using vec_type = SbColor; |
| 104 |
using float_type = float; |
| 105 |
explicit vec_traits(const vec_type& v) |
| 106 |
: v(v) |
| 107 |
{} |
| 108 |
inline std::tuple<float_type, float_type, float_type> get() const |
| 109 |
{ |
| 110 |
return std::make_tuple(v[0], v[1], v[2]); |
| 111 |
} |
| 112 |
|
| 113 |
private: |
| 114 |
const vec_type& v; |
| 115 |
}; |
| 116 |
|
| 117 |
// Specialization for Color |
| 118 |
template<> |
| 119 |
struct vec_traits<Base::Color> |
| 120 |
{ |
| 121 |
using vec_type = Base::Color; |
| 122 |
using float_type = float; |
| 123 |
explicit vec_traits(const vec_type& v) |
| 124 |
: v(v) |
| 125 |
{} |
| 126 |
inline std::tuple<float_type, float_type, float_type> get() const |
| 127 |
{ |
| 128 |
return std::make_tuple(v.r, v.g, v.b); |
| 129 |
} |
| 130 |
|
| 131 |
private: |
| 132 |
const vec_type& v; |
| 133 |
}; |
| 134 |
|
| 135 |
template<> |
| 136 |
struct vec_traits<QColor> |
| 137 |
{ |
| 138 |
using vec_type = QColor; |
| 139 |
using float_type = float; |
| 140 |
explicit vec_traits(const vec_type& v) |
| 141 |
: v(v) |
| 142 |
{} |
| 143 |
inline std::tuple<float_type, float_type, float_type> get() const |
| 144 |
{ |
| 145 |
return std::make_tuple(v.redF(), v.greenF(), v.blueF()); |
| 146 |
} |
| 147 |
|
| 148 |
private: |
| 149 |
const vec_type& v; |
| 150 |
}; |
| 151 |
|
| 152 |
// Specialization for SbColor |
| 153 |
template<> |
| 154 |
struct color_traits<SbColor> |
| 155 |
{ |
| 156 |
using color_type = SbColor; |
| 157 |
color_traits() = default; |
| 158 |
explicit color_traits(const color_type& ct) |
| 159 |
: ct(ct) |
| 160 |
{} |
| 161 |
float redF() const |
| 162 |
{ |
| 163 |
return ct[0]; |
| 164 |
} |
| 165 |
float greenF() const |
| 166 |
{ |
| 167 |
return ct[1]; |
| 168 |
} |
| 169 |
float blueF() const |
| 170 |
{ |
| 171 |
return ct[2]; |
| 172 |
} |
| 173 |
float alphaF() const |
| 174 |
{ |
| 175 |
return 1.0F; |
| 176 |
} |
| 177 |
void setRedF(float red) |
| 178 |
{ |
| 179 |
ct[0] = red; |
| 180 |
} |
| 181 |
void setGreenF(float green) |
| 182 |
{ |
| 183 |
ct[1] = green; |
| 184 |
} |
| 185 |
void setBlueF(float blue) |
| 186 |
{ |
| 187 |
ct[2] = blue; |
| 188 |
} |
| 189 |
void setAlphaF(float alpha) |
| 190 |
{ |
| 191 |
(void)alpha; |
| 192 |
} |
| 193 |
int red() const |
| 194 |
{ |
| 195 |
return int(std::lround(ct[0] * 255.0F)); |
| 196 |
} |
| 197 |
int green() const |
| 198 |
{ |
| 199 |
return int(std::lround(ct[1] * 255.0F)); |
| 200 |
} |
| 201 |
int blue() const |
| 202 |
{ |
| 203 |
return int(std::lround(ct[2] * 255.0F)); |
| 204 |
} |
| 205 |
int alpha() const |
| 206 |
{ |
| 207 |
return 255; |
| 208 |
} |
| 209 |
void setRed(int red) |
| 210 |
{ |
| 211 |
ct[0] = static_cast<float>(red) / 255.0F; |
| 212 |
} |
| 213 |
void setGreen(int green) |
| 214 |
{ |
| 215 |
ct[1] = static_cast<float>(green) / 255.0F; |
| 216 |
} |
| 217 |
void setBlue(int blue) |
| 218 |
{ |
| 219 |
ct[2] = static_cast<float>(blue) / 255.0F; |
| 220 |
} |
| 221 |
void setAlpha(int alpha) |
| 222 |
{ |
| 223 |
(void)alpha; |
| 224 |
} |
| 225 |
static color_type makeColor(int red, int green, int blue, int alpha = 255) |
| 226 |
{ |
| 227 |
(void)alpha; |
| 228 |
return color_type { |
| 229 |
static_cast<float>(red) / 255.0F, |
| 230 |
static_cast<float>(green) / 255.0F, |
| 231 |
static_cast<float>(blue) / 255.0F |
| 232 |
}; |
| 233 |
} |
| 234 |
|
| 235 |
private: |
| 236 |
color_type ct; |
| 237 |
}; |
| 238 |
|
| 239 |
// Specialization for SbColor4f |
| 240 |
template<> |
| 241 |
struct color_traits<SbColor4f> |
| 242 |
{ |
| 243 |
using color_type = SbColor4f; |
| 244 |
color_traits() = default; |
| 245 |
explicit color_traits(const color_type& ct) |
| 246 |
: ct(ct) |
| 247 |
{} |
| 248 |
float redF() const |
| 249 |
{ |
| 250 |
return ct[0]; |
| 251 |
} |
| 252 |
float greenF() const |
| 253 |
{ |
| 254 |
return ct[1]; |
| 255 |
} |
| 256 |
float blueF() const |
| 257 |
{ |
| 258 |
return ct[2]; |
| 259 |
} |
| 260 |
float alphaF() const |
| 261 |
{ |
| 262 |
return ct[3]; |
| 263 |
} |
| 264 |
void setRedF(float red) |
| 265 |
{ |
| 266 |
ct[0] = red; |
| 267 |
} |
| 268 |
void setGreenF(float green) |
| 269 |
{ |
| 270 |
ct[1] = green; |
| 271 |
} |
| 272 |
void setBlueF(float blue) |
| 273 |
{ |
| 274 |
ct[2] = blue; |
| 275 |
} |
| 276 |
void setAlphaF(float alpha) |
| 277 |
{ |
| 278 |
ct[3] = alpha; |
| 279 |
} |
| 280 |
int red() const |
| 281 |
{ |
| 282 |
return int(std::lround(ct[0] * 255.0F)); |
| 283 |
} |
| 284 |
int green() const |
| 285 |
{ |
| 286 |
return int(std::lround(ct[1] * 255.0F)); |
| 287 |
} |
| 288 |
int blue() const |
| 289 |
{ |
| 290 |
return int(std::lround(ct[2] * 255.0F)); |
| 291 |
} |
| 292 |
int alpha() const |
| 293 |
{ |
| 294 |
return int(std::lround(ct[3] * 255.0F)); |
| 295 |
} |
| 296 |
void setRed(int red) |
| 297 |
{ |
| 298 |
ct[0] = static_cast<float>(red) / 255.0F; |
| 299 |
} |
| 300 |
void setGreen(int green) |
| 301 |
{ |
| 302 |
ct[1] = static_cast<float>(green) / 255.0F; |
| 303 |
} |
| 304 |
void setBlue(int blue) |
| 305 |
{ |
| 306 |
ct[2] = static_cast<float>(blue) / 255.0F; |
| 307 |
} |
| 308 |
void setAlpha(int alpha) |
| 309 |
{ |
| 310 |
ct[3] = static_cast<float>(alpha) / 255.0F; |
| 311 |
} |
| 312 |
static color_type makeColor(int red, int green, int blue, int alpha = 255) |
| 313 |
{ |
| 314 |
return color_type { |
| 315 |
static_cast<float>(red) / 255.0F, |
| 316 |
static_cast<float>(green) / 255.0F, |
| 317 |
static_cast<float>(blue) / 255.0F, |
| 318 |
static_cast<float>(alpha) / 255.0F |
| 319 |
}; |
| 320 |
} |
| 321 |
|
| 322 |
private: |
| 323 |
color_type ct; |
| 324 |
}; |
| 325 |
|
| 326 |
// Specialization for QColor |
| 327 |
template<> |
| 328 |
struct color_traits<QColor> |
| 329 |
{ |
| 330 |
using color_type = QColor; |
| 331 |
color_traits() = default; |
| 332 |
explicit color_traits(const color_type& ct) |
| 333 |
: ct(ct) |
| 334 |
{} |
| 335 |
float redF() const |
| 336 |
{ |
| 337 |
return static_cast<float>(ct.redF()); |
| 338 |
} |
| 339 |
float greenF() const |
| 340 |
{ |
| 341 |
return static_cast<float>(ct.greenF()); |
| 342 |
} |
| 343 |
float blueF() const |
| 344 |
{ |
| 345 |
return static_cast<float>(ct.blueF()); |
| 346 |
} |
| 347 |
float alphaF() const |
| 348 |
{ |
| 349 |
return static_cast<float>(ct.alphaF()); |
| 350 |
} |
| 351 |
void setRedF(float red) |
| 352 |
{ |
| 353 |
ct.setRedF(red); |
| 354 |
} |
| 355 |
void setGreenF(float green) |
| 356 |
{ |
| 357 |
ct.setGreenF(green); |
| 358 |
} |
| 359 |
void setBlueF(float blue) |
| 360 |
{ |
| 361 |
ct.setBlueF(blue); |
| 362 |
} |
| 363 |
void setAlphaF(float alpha) |
| 364 |
{ |
| 365 |
ct.setAlphaF(alpha); |
| 366 |
} |
| 367 |
int red() const |
| 368 |
{ |
| 369 |
return ct.red(); |
| 370 |
} |
| 371 |
int green() const |
| 372 |
{ |
| 373 |
return ct.green(); |
| 374 |
} |
| 375 |
int blue() const |
| 376 |
{ |
| 377 |
return ct.blue(); |
| 378 |
} |
| 379 |
int alpha() const |
| 380 |
{ |
| 381 |
return ct.alpha(); |
| 382 |
} |
| 383 |
void setRed(int red) |
| 384 |
{ |
| 385 |
ct.setRed(red); |
| 386 |
} |
| 387 |
void setGreen(int green) |
| 388 |
{ |
| 389 |
ct.setGreen(green); |
| 390 |
} |
| 391 |
void setBlue(int blue) |
| 392 |
{ |
| 393 |
ct.setBlue(blue); |
| 394 |
} |
| 395 |
void setAlpha(int alpha) |
| 396 |
{ |
| 397 |
ct.setAlpha(alpha); |
| 398 |
} |
| 399 |
static color_type makeColor(int red, int green, int blue, int alpha = 255) |
| 400 |
{ |
| 401 |
return color_type {red, green, blue, alpha}; |
| 402 |
} |
| 403 |
|
| 404 |
private: |
| 405 |
color_type ct; |
| 406 |
}; |
| 407 |
|
| 408 |
template<> |
| 409 |
inline SbMatrix convertTo<SbMatrix, Base::Matrix4D>(const Base::Matrix4D& vec2) |
| 410 |
{ |
| 411 |
double dMtrx[16]; |
| 412 |
vec2.getGLMatrix(dMtrx); |
| 413 |
return SbMatrix( |
| 414 |
dMtrx[0], |
| 415 |
dMtrx[1], |
| 416 |
dMtrx[2], |
| 417 |
dMtrx[3], // clazy:exclude=rule-of-two-soft |
| 418 |
dMtrx[4], |
| 419 |
dMtrx[5], |
| 420 |
dMtrx[6], |
| 421 |
dMtrx[7], |
| 422 |
dMtrx[8], |
| 423 |
dMtrx[9], |
| 424 |
dMtrx[10], |
| 425 |
dMtrx[11], |
| 426 |
dMtrx[12], |
| 427 |
dMtrx[13], |
| 428 |
dMtrx[14], |
| 429 |
dMtrx[15] |
| 430 |
); |
| 431 |
} |
| 432 |
|
| 433 |
template<> |
| 434 |
inline Base::Matrix4D convertTo<Base::Matrix4D, SbMatrix>(const SbMatrix& vec2) |
| 435 |
{ |
| 436 |
Base::Matrix4D mat; |
| 437 |
for (int i = 0; i < 4; ++i) { |
| 438 |
for (int j = 0; j < 4; ++j) { |
| 439 |
mat[i][j] = vec2[j][i]; |
| 440 |
} |
| 441 |
} |
| 442 |
return mat; |
| 443 |
} |
| 444 |
} // namespace Base |
| 445 |
|
| 446 |
namespace App |
| 447 |
{ |
| 448 |
class DocumentObject; |
| 449 |
} |
| 450 |
namespace Gui |
| 451 |
{ |
| 452 |
|
| 453 |
/** |
| 454 |
*/ |
| 455 |
class GuiExport ViewVolumeProjection: public Base::ViewProjMethod |
| 456 |
{ |
| 457 |
public: |
| 458 |
explicit ViewVolumeProjection(const SbViewVolume& vv); |
| 459 |
~ViewVolumeProjection() override = default; |
| 460 |
|
| 461 |
Base::Vector3f operator()(const Base::Vector3f& rclPt) const override; |
| 462 |
Base::Vector3d operator()(const Base::Vector3d& rclPt) const override; |
| 463 |
Base::Vector3f inverse(const Base::Vector3f& rclPt) const override; |
| 464 |
Base::Vector3d inverse(const Base::Vector3d& rclPt) const override; |
| 465 |
|
| 466 |
Base::Matrix4D getProjectionMatrix() const override; |
| 467 |
|
| 468 |
protected: |
| 469 |
SbViewVolume viewVolume; |
| 470 |
SbMatrix matrix; |
| 471 |
SbMatrix invert; |
| 472 |
}; |
| 473 |
|
| 474 |
class GuiExport Tessellator |
| 475 |
{ |
| 476 |
public: |
| 477 |
explicit Tessellator(const std::vector<SbVec2f>&); |
| 478 |
std::vector<int> tessellate() const; |
| 479 |
|
| 480 |
private: |
| 481 |
static void tessCB(void* v0, void* v1, void* v2, void* cbdata); |
| 482 |
|
| 483 |
private: |
| 484 |
std::vector<SbVec2f> polygon; |
| 485 |
}; |
| 486 |
|
| 487 |
class GuiExport ItemViewSelection |
| 488 |
{ |
| 489 |
public: |
| 490 |
explicit ItemViewSelection(QAbstractItemView* view); |
| 491 |
void applyFrom(const std::vector<App::DocumentObject*> objs); |
| 492 |
|
| 493 |
private: |
| 494 |
QAbstractItemView* view; |
| 495 |
class MatchName; |
| 496 |
}; |
| 497 |
|
| 498 |
#define FC_ADD_CATALOG_ENTRY(__part__, __partclass__, __parent__) \ |
| 499 |
SO_KIT_ADD_CATALOG_ENTRY(__part__, __partclass__, TRUE, __parent__, "", TRUE); |
| 500 |
|
| 501 |
#define FC_SET_SWITCH(__name__, __state__) \ |
| 502 |
do { \ |
| 503 |
SoSwitch* sw = SO_GET_ANY_PART(this, __name__, SoSwitch); \ |
| 504 |
assert(sw); \ |
| 505 |
sw->whichChild = __state__; \ |
| 506 |
} while (0) |
| 507 |
|
| 508 |
#define FC_SET_TOGGLE_SWITCH(__name__, __state__) \ |
| 509 |
do { \ |
| 510 |
SoToggleSwitch* sw = SO_GET_ANY_PART(this, __name__, SoToggleSwitch); \ |
| 511 |
assert(sw); \ |
| 512 |
sw->on = __state__; \ |
| 513 |
} while (0) |
| 514 |
|
| 515 |
struct RotationComponents |
| 516 |
{ |
| 517 |
float angle; |
| 518 |
SbVec3f axis; |
| 519 |
}; |
| 520 |
|
| 521 |
[[nodiscard]] inline RotationComponents getRotationComponents(const SbRotation& rotation) |
| 522 |
{ |
| 523 |
RotationComponents comps; |
| 524 |
rotation.getValue(comps.axis, comps.angle); |
| 525 |
|
| 526 |
return comps; |
| 527 |
} |
| 528 |
|
| 529 |
struct TransformComponents |
| 530 |
{ |
| 531 |
SbVec3f translation; |
| 532 |
SbVec3f scale; |
| 533 |
SbRotation rotation; |
| 534 |
SbRotation scaleOrientation; |
| 535 |
}; |
| 536 |
|
| 537 |
[[nodiscard]] inline TransformComponents getMatrixTransform(const SbMatrix& matrix) |
| 538 |
{ |
| 539 |
TransformComponents comps; |
| 540 |
matrix.getTransform(comps.translation, comps.rotation, comps.scale, comps.scaleOrientation); |
| 541 |
|
| 542 |
return comps; |
| 543 |
} |
| 544 |
|
| 545 |
} // namespace Gui |
| 546 |
|
| 547 |
#endif // GUI_UTILITIES_H |