| 1 |
// SPDX-License-Identifier: LGPL-2.1-or-later |
| 2 |
|
| 3 |
/*************************************************************************** |
| 4 |
* Copyright (c) 2014 Werner Mayer <wmayer[at]users.sourceforge.net> * |
| 5 |
* * |
| 6 |
* This file is part of the FreeCAD CAx development system. * |
| 7 |
* * |
| 8 |
* This library is free software; you can redistribute it and/or * |
| 9 |
* modify it under the terms of the GNU Library General Public * |
| 10 |
* License as published by the Free Software Foundation; either * |
| 11 |
* version 2 of the License, or (at your option) any later version. * |
| 12 |
* * |
| 13 |
* This library is distributed in the hope that it will be useful, * |
| 14 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 16 |
* GNU Library General Public License for more details. * |
| 17 |
* * |
| 18 |
* You should have received a copy of the GNU Library General Public * |
| 19 |
* License along with this library; see the file COPYING.LIB. If not, * |
| 20 |
* write to the Free Software Foundation, Inc., 51 Franklin Street, * |
| 21 |
* Fifth Floor, Boston, MA 02110-1301, USA * |
| 22 |
* * |
| 23 |
***************************************************************************/ |
| 24 |
|
| 25 |
|
| 26 |
#include "CoordinateSystem.h" |
| 27 |
#include "Exception.h" |
| 28 |
#include "Matrix.h" |
| 29 |
#include "Placement.h" |
| 30 |
#include "Rotation.h" |
| 31 |
|
| 32 |
|
| 33 |
using namespace Base; |
| 34 |
|
| 35 |
CoordinateSystem::CoordinateSystem() |
| 36 |
: axis(Vector3d(), Vector3d(0, 0, 1)) |
| 37 |
, xdir(1, 0, 0) |
| 38 |
, ydir(0, 1, 0) |
| 39 |
{} |
| 40 |
|
| 41 |
void CoordinateSystem::setAxes(const Axis& vec, const Vector3d& xd) |
| 42 |
{ |
| 43 |
if (xd.Sqr() < Base::Vector3d::epsilon()) { |
| 44 |
throw Base::ValueError("Direction is null vector"); |
| 45 |
} |
| 46 |
Vector3d yd = vec.getDirection() % xd; |
| 47 |
if (yd.Sqr() < Base::Vector3d::epsilon()) { |
| 48 |
throw Base::ValueError("Direction is parallel to Z-direction"); |
| 49 |
} |
| 50 |
ydir = yd; |
| 51 |
ydir.Normalize(); |
| 52 |
xdir = ydir % vec.getDirection(); |
| 53 |
xdir.Normalize(); |
| 54 |
axis.setBase(vec.getBase()); |
| 55 |
Base::Vector3d zdir = vec.getDirection(); |
| 56 |
zdir.Normalize(); |
| 57 |
axis.setDirection(zdir); |
| 58 |
} |
| 59 |
|
| 60 |
void CoordinateSystem::setAxes(const Vector3d& n, const Vector3d& xd) |
| 61 |
{ |
| 62 |
if (xd.Sqr() < Base::Vector3d::epsilon()) { |
| 63 |
throw Base::ValueError("Direction is null vector"); |
| 64 |
} |
| 65 |
Vector3d yd = n % xd; |
| 66 |
if (yd.Sqr() < Base::Vector3d::epsilon()) { |
| 67 |
throw Base::ValueError("Direction is parallel to Z-direction"); |
| 68 |
} |
| 69 |
ydir = yd; |
| 70 |
ydir.Normalize(); |
| 71 |
xdir = ydir % n; |
| 72 |
xdir.Normalize(); |
| 73 |
Base::Vector3d zdir = n; |
| 74 |
zdir.Normalize(); |
| 75 |
axis.setDirection(zdir); |
| 76 |
} |
| 77 |
|
| 78 |
void CoordinateSystem::setAxis(const Axis& axis) |
| 79 |
{ |
| 80 |
setAxes(axis, xdir); |
| 81 |
} |
| 82 |
|
| 83 |
void CoordinateSystem::setXDirection(const Vector3d& dir) |
| 84 |
{ |
| 85 |
Vector3d yd = axis.getDirection() % dir; |
| 86 |
if (yd.Sqr() < Base::Vector3d::epsilon()) { |
| 87 |
throw Base::ValueError("Direction is parallel to Z-direction"); |
| 88 |
} |
| 89 |
ydir = yd; |
| 90 |
ydir.Normalize(); |
| 91 |
xdir = ydir % axis.getDirection(); |
| 92 |
xdir.Normalize(); |
| 93 |
} |
| 94 |
|
| 95 |
void CoordinateSystem::setYDirection(const Vector3d& dir) |
| 96 |
{ |
| 97 |
Vector3d xd = dir % axis.getDirection(); |
| 98 |
if (xd.Sqr() < Base::Vector3d::epsilon()) { |
| 99 |
throw Base::ValueError("Direction is parallel to Z-direction"); |
| 100 |
} |
| 101 |
xdir = xd; |
| 102 |
xdir.Normalize(); |
| 103 |
ydir = axis.getDirection() % xdir; |
| 104 |
ydir.Normalize(); |
| 105 |
} |
| 106 |
|
| 107 |
void CoordinateSystem::setZDirection(const Vector3d& dir) |
| 108 |
{ |
| 109 |
setAxes(dir, xdir); |
| 110 |
} |
| 111 |
|
| 112 |
Placement CoordinateSystem::displacement(const CoordinateSystem& cs) const |
| 113 |
{ |
| 114 |
// NOLINTBEGIN |
| 115 |
const Base::Vector3d& a = axis.getBase(); |
| 116 |
const Base::Vector3d& zdir = axis.getDirection(); |
| 117 |
Base::Matrix4D At; |
| 118 |
At[0][0] = xdir.x; |
| 119 |
At[1][0] = ydir.x; |
| 120 |
At[2][0] = zdir.x; |
| 121 |
At[0][1] = xdir.y; |
| 122 |
At[1][1] = ydir.y; |
| 123 |
At[2][1] = zdir.y; |
| 124 |
At[0][2] = xdir.z; |
| 125 |
At[1][2] = ydir.z; |
| 126 |
At[2][2] = zdir.z; |
| 127 |
Base::Vector3d at = At * a; |
| 128 |
At[0][3] = -at.x; |
| 129 |
At[1][3] = -at.y; |
| 130 |
At[2][3] = -at.z; |
| 131 |
|
| 132 |
const Base::Vector3d& b = cs.axis.getBase(); |
| 133 |
const Base::Vector3d& cszdir = cs.axis.getDirection(); |
| 134 |
Base::Matrix4D B; |
| 135 |
B[0][0] = cs.xdir.x; |
| 136 |
B[0][1] = cs.ydir.x; |
| 137 |
B[0][2] = cszdir.x; |
| 138 |
B[0][3] = b.x; |
| 139 |
B[1][0] = cs.xdir.y; |
| 140 |
B[1][1] = cs.ydir.y; |
| 141 |
B[1][2] = cszdir.y; |
| 142 |
B[1][3] = b.y; |
| 143 |
B[2][0] = cs.xdir.z; |
| 144 |
B[2][1] = cs.ydir.z; |
| 145 |
B[2][2] = cszdir.z; |
| 146 |
B[2][3] = b.z; |
| 147 |
|
| 148 |
Placement PAt(At); |
| 149 |
Placement PB(B); |
| 150 |
Placement C = PB * PAt; |
| 151 |
return C; |
| 152 |
// NOLINTEND |
| 153 |
} |
| 154 |
|
| 155 |
void CoordinateSystem::transformTo(Vector3d& pnt) |
| 156 |
{ |
| 157 |
return pnt.TransformToCoordinateSystem(axis.getBase(), xdir, ydir); |
| 158 |
} |
| 159 |
|
| 160 |
void CoordinateSystem::transform(const Placement& plm) |
| 161 |
{ |
| 162 |
axis *= plm; |
| 163 |
plm.getRotation().multVec(this->xdir, this->xdir); |
| 164 |
plm.getRotation().multVec(this->ydir, this->ydir); |
| 165 |
} |
| 166 |
|
| 167 |
void CoordinateSystem::transform(const Rotation& rot) |
| 168 |
{ |
| 169 |
Vector3d zdir = axis.getDirection(); |
| 170 |
rot.multVec(zdir, zdir); |
| 171 |
axis.setDirection(zdir); |
| 172 |
rot.multVec(this->xdir, this->xdir); |
| 173 |
rot.multVec(this->ydir, this->ydir); |
| 174 |
} |
| 175 |
|
| 176 |
void CoordinateSystem::setPlacement(const Placement& plm) |
| 177 |
{ |
| 178 |
Vector3d zdir(0, 0, 1); |
| 179 |
plm.getRotation().multVec(zdir, zdir); |
| 180 |
axis.setBase(plm.getPosition()); |
| 181 |
axis.setDirection(zdir); |
| 182 |
|
| 183 |
plm.getRotation().multVec(Vector3d(1, 0, 0), this->xdir); |
| 184 |
plm.getRotation().multVec(Vector3d(0, 1, 0), this->ydir); |
| 185 |
} |