File size: 7,792 Bytes
985c397 | 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 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 | // SPDX-License-Identifier: LGPL-2.1-or-later
// Wild Magic Source Code
// David Eberly
// http://www.geometrictools.com
// Copyright (c) 1998-2007
//
// This library is free software; you can redistribute it and/or modify it
// under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation; either version 2.1 of the License, or (at
// your option) any later version. The license is available for reading at
// either of the locations:
// http://www.gnu.org/copyleft/lgpl.html
// http://www.geometrictools.com/License/WildMagicLicense.pdf
// The license applies to versions 0 through 4 of Wild Magic.
//
// Version: 4.0.0 (2006/06/28)
#include "Wm4FoundationPCH.h"
#include "Wm4Delaunay1.h"
namespace Wm4
{
//----------------------------------------------------------------------------
template <class Real>
Delaunay1<Real>::Delaunay1 (int iVertexQuantity, Real* afVertex,
Real fEpsilon, bool bOwner, Query::Type eQueryType)
:
Delaunay<Real>(iVertexQuantity,fEpsilon,bOwner,eQueryType)
{
assert(afVertex);
m_afVertex = afVertex;
std::vector<SortedVertex> kArray(m_iVertexQuantity);
int i;
for (i = 0; i < m_iVertexQuantity; i++)
{
kArray[i].Value = m_afVertex[i];
kArray[i].Index = i;
}
std::sort(kArray.begin(),kArray.end());
Real fRange = kArray[m_iVertexQuantity-1].Value - kArray[0].Value;
if (fRange >= m_fEpsilon)
{
m_iDimension = 1;
m_iSimplexQuantity = m_iVertexQuantity - 1;
m_aiIndex = WM4_NEW int[2*m_iSimplexQuantity];
for (i = 0; i < m_iSimplexQuantity; i++)
{
m_aiIndex[2*i] = kArray[i].Index;
m_aiIndex[2*i+1] = kArray[i+1].Index;
}
m_aiAdjacent = WM4_NEW int[2*m_iSimplexQuantity];
for (i = 0; i < m_iSimplexQuantity; i++)
{
m_aiAdjacent[2*i] = i-1;
m_aiAdjacent[2*i+1] = i+1;
}
m_aiAdjacent[2*m_iSimplexQuantity-1] = -1;
}
}
//----------------------------------------------------------------------------
template <class Real>
Delaunay1<Real>::~Delaunay1 ()
{
if (m_bOwner)
{
WM4_DELETE[] m_afVertex;
}
}
//----------------------------------------------------------------------------
template <class Real>
const Real* Delaunay1<Real>::GetVertices () const
{
return m_afVertex;
}
//----------------------------------------------------------------------------
template <class Real>
bool Delaunay1<Real>::GetHull (int aiIndex[2])
{
assert(m_iDimension == 1);
if (m_iDimension != 1)
{
return false;
}
aiIndex[0] = m_aiIndex[0];
aiIndex[1] = m_aiIndex[2*m_iSimplexQuantity-1];
return true;
}
//----------------------------------------------------------------------------
template <class Real>
int Delaunay1<Real>::GetContainingSegment (const Real fP) const
{
assert(m_iDimension == 1);
if (m_iDimension != 1)
{
return -1;
}
if (fP < m_afVertex[m_aiIndex[0]])
{
return -1;
}
if (fP > m_afVertex[m_aiIndex[2*m_iSimplexQuantity-1]])
{
return -1;
}
int i;
for (i = 0; i < m_iSimplexQuantity; i++)
{
if (fP < m_afVertex[m_aiIndex[2*i+1]])
{
break;
}
}
assert(i < m_iSimplexQuantity);
return i;
}
//----------------------------------------------------------------------------
template <class Real>
bool Delaunay1<Real>::GetVertexSet (int i, Real afV[2]) const
{
assert(m_iDimension == 1);
if (m_iDimension != 1)
{
return false;
}
if (0 <= i && i < m_iSimplexQuantity)
{
afV[0] = m_afVertex[m_aiIndex[2*i]];
afV[1] = m_afVertex[m_aiIndex[2*i+1]];
return true;
}
return false;
}
//----------------------------------------------------------------------------
template <class Real>
bool Delaunay1<Real>::GetIndexSet (int i, int aiIndex[2]) const
{
assert(m_iDimension == 1);
if (m_iDimension != 1)
{
return false;
}
if (0 <= i && i < m_iSimplexQuantity)
{
aiIndex[0] = m_aiIndex[2*i];
aiIndex[1] = m_aiIndex[2*i+1];
return true;
}
return false;
}
//----------------------------------------------------------------------------
template <class Real>
bool Delaunay1<Real>::GetAdjacentSet (int i, int aiAdjacent[2]) const
{
assert(m_iDimension == 1);
if (m_iDimension != 1)
{
return false;
}
if (0 <= i && i < m_iSimplexQuantity)
{
aiAdjacent[0] = m_aiAdjacent[2*i];
aiAdjacent[1] = m_aiAdjacent[2*i+1];
return true;
}
return false;
}
//----------------------------------------------------------------------------
template <class Real>
bool Delaunay1<Real>::GetBarycentricSet (int i, const Real fP, Real afBary[2])
const
{
assert(m_iDimension == 1);
if (m_iDimension != 1)
{
return false;
}
if (0 <= i && i < m_iSimplexQuantity)
{
Real fV0 = m_afVertex[m_aiIndex[2*i]];
Real fV1 = m_afVertex[m_aiIndex[2*i+1]];
Real fDenom = fV1 - fV0;
if (fDenom > m_fEpsilon)
{
afBary[0] = (fV1 - fP)/fDenom;
}
else
{
afBary[0] = (Real)1.0;
}
afBary[1] = (Real)1.0 - afBary[0];
return true;
}
return false;
}
//----------------------------------------------------------------------------
template <class Real>
Delaunay1<Real>::Delaunay1 (const char* acFilename)
:
Delaunay<Real>(0,(Real)0.0,false,Query::QT_REAL)
{
m_afVertex = nullptr;
bool bLoaded = Load(acFilename);
assert(bLoaded);
(void)bLoaded; // avoid warning in Release build
}
//----------------------------------------------------------------------------
template <class Real>
bool Delaunay1<Real>::Load (const char* acFilename)
{
FILE* pkIFile = System::Fopen(acFilename,"rb");
if (!pkIFile)
{
return false;
}
Delaunay<Real>::Load(pkIFile);
if (m_bOwner)
{
WM4_DELETE[] m_afVertex;
}
m_bOwner = true;
m_afVertex = WM4_NEW Real[m_iVertexQuantity];
size_t uiSize = sizeof(Real);
if (uiSize == 4)
{
System::Read4le(pkIFile,m_iVertexQuantity,m_afVertex);
}
else // uiSize == 8
{
System::Read8le(pkIFile,m_iVertexQuantity,m_afVertex);
}
System::Fclose(pkIFile);
return true;
}
//----------------------------------------------------------------------------
template <class Real>
bool Delaunay1<Real>::Save (const char* acFilename) const
{
FILE* pkOFile = System::Fopen(acFilename,"wb");
if (!pkOFile)
{
return false;
}
Delaunay<Real>::Save(pkOFile);
size_t uiSize = sizeof(Real);
if (uiSize == 4)
{
System::Write4le(pkOFile,m_iVertexQuantity,m_afVertex);
}
else // uiSize == 8
{
System::Write8le(pkOFile,m_iVertexQuantity,m_afVertex);
}
System::Fclose(pkOFile);
return true;
}
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
// explicit instantiation
//----------------------------------------------------------------------------
template WM4_FOUNDATION_ITEM
class Delaunay1<float>;
template WM4_FOUNDATION_ITEM
class Delaunay1<double>;
//----------------------------------------------------------------------------
}
|