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 213 214 215 216 217 218
|
// Copyright (c) 2007-2008 INRIA (France).
// All rights reserved.
//
// This file is part of CGAL (www.cgal.org).
// You can redistribute it and/or modify it under the terms of the GNU
// General Public License as published by the Free Software Foundation,
// either version 3 of the License, or (at your option) any later version.
//
// Licensees holding a valid commercial license may use this file in
// accordance with the commercial license agreement provided with the software.
//
// This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
// WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
//
// $URL: svn+ssh://scm.gforge.inria.fr/svn/cgal/branches/next/Surface_reconstruction_points_3/include/CGAL/Lightweight_vector_3.h $
// $Id: Lightweight_vector_3.h 67117 2012-01-13 18:14:48Z lrineau $
//
//
// Author(s) : Laurent Saboret, Pierre Alliez
#ifndef CGAL_LIGHTWEIGHT_VECTOR_3_H
#define CGAL_LIGHTWEIGHT_VECTOR_3_H
#include <CGAL/Vector_3.h>
#include <CGAL/Origin.h>
namespace CGAL {
/// 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.
///
/// @heading Is Model for the Concepts: Model of the Kernel::Vector_3 concept.
///
/// @heading Parameters:
/// @param 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 = NULL;
}
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 == NULL) ? NULL : 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) ? NULL : new Vector(vector);
}
/// Operator =()
Lightweight_vector_3& operator=(const Lightweight_vector_3& that)
{
if (m_pVector != NULL && that.m_pVector != NULL)
{
*m_pVector = *that.m_pVector;
}
else
{
delete m_pVector;
m_pVector = (that.m_pVector == NULL) ? NULL : new Vector(*that.m_pVector);
}
return *this;
}
/// Destructor
~Lightweight_vector_3()
{
delete m_pVector; m_pVector = NULL;
}
/// 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 != NULL)
return *m_pVector;
else
return NULL_VECTOR;
}
Vector get_vector() const
{
return *this;
}
FT x() const { return (m_pVector != NULL) ? m_pVector->x() : 0; }
FT y() const { return (m_pVector != NULL) ? m_pVector->y() : 0; }
FT z() const { return (m_pVector != NULL) ? m_pVector->z() : 0; }
RT hx() const { return (m_pVector != NULL) ? m_pVector->hx() : 0; }
RT hy() const { return (m_pVector != NULL) ? m_pVector->hy() : 0; }
RT hz() const { return (m_pVector != NULL) ? m_pVector->hz() : 0; }
RT hw() const { return (m_pVector != NULL) ? m_pVector->hw() : 1; }
FT cartesian(int i) const
{
if (m_pVector != NULL)
return m_pVector->cartesian(i);
else
return 0;
}
FT operator[](int i) const
{
if (m_pVector != NULL)
return (*m_pVector)[i];
else if (i != 3)
return 0;
else
return 1;
}
RT homogeneous(int i) const
{
if (m_pVector != NULL)
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 != NULL)
return -(*m_pVector);
else
return NULL_VECTOR;
}
Vector operator/(RT c) const
{
if (m_pVector != NULL)
return (*m_pVector) / c;
else
return NULL_VECTOR;
}
Vector operator*(FT c) const
{
if (m_pVector != NULL)
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 != NULL)
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
|