1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
|
// Copyright (c) 2007-2008 INRIA (France).
// All rights reserved.
//
// This file is part of CGAL (www.cgal.org).
//
// $URL: https://github.com/CGAL/cgal/blob/v6.1.1/Poisson_surface_reconstruction_3/include/CGAL/Lightweight_vector_3.h $
// $Id: include/CGAL/Lightweight_vector_3.h 08b27d3db14 $
// SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-Commercial
//
//
// Author(s) : Laurent Saboret, Pierre Alliez
#ifndef CGAL_LIGHTWEIGHT_VECTOR_3_H
#define CGAL_LIGHTWEIGHT_VECTOR_3_H
#include <CGAL/license/Poisson_surface_reconstruction_3.h>
#include <CGAL/Vector_3.h>
#include <CGAL/Origin.h>
namespace CGAL {
/// \internal
/// The Lightweight_vector_3 class represents a 3D vector (oriented).
/// The purpose of this class is to save memory as the actual vector
/// is allocated only when needed.
///
/// \cgalModels{Kernel::Vector_3}
///
/// @tparam Gt Geometric traits class.
template<class Gt>
class Lightweight_vector_3
{
// Public types
public:
typedef Gt Geom_traits; ///< Geometric traits class
typedef typename Geom_traits::FT FT;
typedef typename Geom_traits::RT RT;
typedef typename Geom_traits::Vector_3 Vector; ///< Kernel's Vector_3 class.
// Public methods
public:
/// Vector is (0,0,0) by default.
Lightweight_vector_3(Null_vector = NULL_VECTOR)
{
m_pVector = nullptr;
}
Lightweight_vector_3(const Vector& vector)
{
m_pVector = new Vector(vector);
}
Lightweight_vector_3(FT x, FT y, FT z)
{
m_pVector = new Vector(x,y,z);
}
Lightweight_vector_3(RT hx, RT hy, RT hz, RT hw)
{
m_pVector = new Vector(hx,hy,hz,hw);
}
/// Copy constructor
Lightweight_vector_3(const Lightweight_vector_3& that)
{
m_pVector = (that.m_pVector == nullptr) ? nullptr : new Vector(*that.m_pVector);
}
template <class K>
Lightweight_vector_3(const Lightweight_vector_3<K>& that)
{
Vector vector = that.get_vector();
m_pVector = (vector == NULL_VECTOR) ? nullptr : new Vector(vector);
}
/// Operator =()
Lightweight_vector_3& operator=(const Lightweight_vector_3& that)
{
if (m_pVector != nullptr && that.m_pVector != nullptr)
{
*m_pVector = *that.m_pVector;
}
else
{
delete m_pVector;
m_pVector = (that.m_pVector == nullptr) ? nullptr : new Vector(*that.m_pVector);
}
return *this;
}
/// Destructor
~Lightweight_vector_3()
{
delete m_pVector; m_pVector = nullptr;
}
/// Compare vectors
bool operator==(const Lightweight_vector_3& that)
{
return Vector(*this) == Vector(that);
}
bool operator!=(const Lightweight_vector_3& that)
{
return ! (*this == that);
}
/// Gets (a copy of) the actual vector.
operator Vector() const
{
if (m_pVector != nullptr)
return *m_pVector;
else
return NULL_VECTOR;
}
Vector get_vector() const
{
return *this;
}
FT x() const { return (m_pVector != nullptr) ? m_pVector->x() : 0; }
FT y() const { return (m_pVector != nullptr) ? m_pVector->y() : 0; }
FT z() const { return (m_pVector != nullptr) ? m_pVector->z() : 0; }
RT hx() const { return (m_pVector != nullptr) ? m_pVector->hx() : 0; }
RT hy() const { return (m_pVector != nullptr) ? m_pVector->hy() : 0; }
RT hz() const { return (m_pVector != nullptr) ? m_pVector->hz() : 0; }
RT hw() const { return (m_pVector != nullptr) ? m_pVector->hw() : 1; }
FT cartesian(int i) const
{
if (m_pVector != nullptr)
return m_pVector->cartesian(i);
else
return 0;
}
FT operator[](int i) const
{
if (m_pVector != nullptr)
return (*m_pVector)[i];
else if (i != 3)
return 0;
else
return 1;
}
RT homogeneous(int i) const
{
if (m_pVector != nullptr)
return m_pVector->homogeneous();
else if (i != 3)
return 0;
else
return 1;
}
int dimension() const { return 3; }
Vector operator+(const Vector& that) const
{
return Vector(*this) + Vector(that);
}
Vector operator-(const Vector& that) const
{
return Vector(*this) - Vector(that);
}
FT operator*(const Vector& that) const
{
return Vector(*this) * Vector(that);
}
Vector operator-() const
{
if (m_pVector != nullptr)
return -(*m_pVector);
else
return NULL_VECTOR;
}
Vector operator/(RT c) const
{
if (m_pVector != nullptr)
return (*m_pVector) / c;
else
return NULL_VECTOR;
}
Vector operator*(FT c) const
{
if (m_pVector != nullptr)
return (*m_pVector) * c;
else
return NULL_VECTOR;
}
friend Vector operator*(FT c, const Lightweight_vector_3& vector)
{
return vector * c;
}
FT squared_length() const
{
if (m_pVector != nullptr)
return m_pVector->squared_length();
else
return 0;
}
// Data
private:
Vector* m_pVector; // Vector (optional to save memory)
};
} //namespace CGAL
#endif //CGAL_LIGHTWEIGHT_VECTOR_3_H
|