RoboDK Plug-In Interface
Loading...
Searching...
No Matches
vector3.cpp
1/****************************************************************************
2**
3** Copyright (c) 2015-2025 RoboDK Inc.
4** Contact: https://robodk.com/
5**
6** This file is part of the RoboDK API.
7**
8** Permission is hereby granted, free of charge, to any person obtaining a copy
9** of this software and associated documentation files (the "Software"), to deal
10** in the Software without restriction, including without limitation the rights
11** to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12** copies of the Software, and to permit persons to whom the Software is
13** furnished to do so, subject to the following conditions:
14**
15** The above copyright notice and this permission notice shall be included in all
16** copies or substantial portions of the Software.
17**
18** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19** IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20** FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21** AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22** LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23** OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24** SOFTWARE.
25**
26** RoboDK is a registered trademark of RoboDK Inc.
27**
28****************************************************************************/
29
30#include "vector3.h"
31
32#include <cmath>
33
34
35namespace robodk
36{
37
38Vector3::Vector3(double x, double y, double z)
39{
40 _v[0] = x;
41 _v[1] = y;
42 _v[2] = z;
43}
44
45double Vector3::Length() const
46{
47 return std::sqrt(_v[0] * _v[0] + _v[1] * _v[1] + _v[2] * _v[2]);
48}
49
51{
52 double len = Length();
53 if (std::abs(len) <= 0.000000000001)
54 return;
55
56 _v[0] /= len;
57 _v[1] /= len;
58 _v[2] /= len;
59}
60
61double Vector3::DotProduct(const Vector3& v1, const Vector3& v2)
62{
63 return v1._v[0] * v2._v[0] + v1._v[1] * v2._v[1] + v1._v[2] * v2._v[2];
64}
65
67{
68 return Vector3(
69 v1._v[1] * v2._v[2] - v1._v[2] * v2._v[1],
70 v1._v[2] * v2._v[0] - v1._v[0] * v2._v[2],
71 v1._v[0] * v2._v[1] - v1._v[1] * v2._v[0]);
72}
73
74} // namespace robodk
The Vector3 class represents a vector or vertex in 3D space.
Definition vector3.h:49
static double DotProduct(const Vector3 &v1, const Vector3 &v2)
Returns the dot product of v1 and v2.
Definition vector3.cpp:61
Vector3()=default
Constructs a null vector, i.e.
double Length() const
Returns the length of the vector from the origin.
Definition vector3.cpp:45
static Vector3 CrossProduct(const Vector3 &v1, const Vector3 &v2)
Returns the cross-product of vectors v1 and v2, which corresponds to the normal vector of a plane def...
Definition vector3.cpp:66
void Normalize()
Normalizes the currect vector in place.
Definition vector3.cpp:50