Mercurial > hg > orthanc-stl
comparison Sources/Vector3D.cpp @ 32:976da5476810
reorganization
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Thu, 04 Apr 2024 18:35:54 +0200 |
parents | |
children | 2460b376d3f7 |
comparison
equal
deleted
inserted
replaced
31:ab231760799d | 32:976da5476810 |
---|---|
1 /** | |
2 * SPDX-FileCopyrightText: 2023-2024 Sebastien Jodogne, UCLouvain, Belgium | |
3 * SPDX-License-Identifier: GPL-3.0-or-later | |
4 */ | |
5 | |
6 /** | |
7 * STL plugin for Orthanc | |
8 * Copyright (C) 2023-2024 Sebastien Jodogne, UCLouvain, Belgium | |
9 * | |
10 * This program is free software: you can redistribute it and/or | |
11 * modify it under the terms of the GNU General Public License as | |
12 * published by the Free Software Foundation, either version 3 of the | |
13 * License, or (at your option) any later version. | |
14 * | |
15 * This program is distributed in the hope that it will be useful, but | |
16 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
18 * General Public License for more details. | |
19 * | |
20 * You should have received a copy of the GNU General Public License | |
21 * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
22 **/ | |
23 | |
24 | |
25 #include "Vector3D.h" | |
26 | |
27 #include "Toolbox.h" | |
28 | |
29 #include <OrthancException.h> | |
30 | |
31 #include <cmath> | |
32 | |
33 | |
34 Vector3D::Vector3D() : | |
35 x_(0), | |
36 y_(0), | |
37 z_(0) | |
38 { | |
39 } | |
40 | |
41 | |
42 Vector3D::Vector3D(double x, | |
43 double y, | |
44 double z) : | |
45 x_(x), | |
46 y_(y), | |
47 z_(z) | |
48 { | |
49 } | |
50 | |
51 | |
52 Vector3D::Vector3D(const Vector3D& from, | |
53 const Vector3D& to) : | |
54 x_(to.x_ - from.x_), | |
55 y_(to.y_ - from.y_), | |
56 z_(to.z_ - from.z_) | |
57 { | |
58 } | |
59 | |
60 | |
61 double Vector3D::ComputeSquaredNorm() const | |
62 { | |
63 return x_ * x_ + y_ * y_ + z_ * z_; | |
64 } | |
65 | |
66 | |
67 double Vector3D::ComputeNorm() const | |
68 { | |
69 return sqrt(ComputeSquaredNorm()); | |
70 } | |
71 | |
72 | |
73 void Vector3D::Normalize() | |
74 { | |
75 double norm = ComputeNorm(); | |
76 if (!Toolbox::IsNear(norm, 0)) | |
77 { | |
78 x_ /= norm; | |
79 y_ /= norm; | |
80 z_ /= norm; | |
81 } | |
82 } | |
83 | |
84 | |
85 Vector3D Vector3D::CrossProduct(const Vector3D& u, | |
86 const Vector3D& v) | |
87 { | |
88 return Vector3D(u.GetY() * v.GetZ() - u.GetZ() * v.GetY(), | |
89 u.GetZ() * v.GetX() - u.GetX() * v.GetZ(), | |
90 u.GetX() * v.GetY() - u.GetY() * v.GetX()); | |
91 } | |
92 | |
93 | |
94 double Vector3D::DotProduct(const Vector3D& a, | |
95 const Vector3D& b) | |
96 { | |
97 return a.GetX() * b.GetX() + a.GetY() * b.GetY() + a.GetZ() * b.GetZ(); | |
98 } | |
99 | |
100 | |
101 bool Vector3D::AreParallel(const Vector3D& a, | |
102 const Vector3D& b) | |
103 { | |
104 if (Toolbox::IsNear(a.ComputeSquaredNorm(), 1) && | |
105 Toolbox::IsNear(b.ComputeSquaredNorm(), 1)) | |
106 { | |
107 return Toolbox::IsNear(std::abs(Vector3D::DotProduct(a, b)), 1); | |
108 } | |
109 else | |
110 { | |
111 throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange, | |
112 "Only applicable to normalized vectors"); | |
113 } | |
114 } |