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 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
|
/****************************************************************************
* VCGLib o o *
* Visual and Computer Graphics Library o o *
* _ O _ *
* Copyright(C) 2004-2016 \/)\/ *
* Visual Computing Lab /\/| *
* ISTI - Italian National Research Council | *
* \ *
* All rights reserved. *
* *
* This program is free software; 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 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License (http://www.gnu.org/licenses/gpl.txt) *
* for more details. *
* *
****************************************************************************/
#ifndef __VCGLIB_ZONOHEDRON
#define __VCGLIB_ZONOHEDRON
namespace vcg {
namespace tri {
/** \addtogroup trimesh */
//@{
/**
A class to build a Zonohedron.
Given a set of input vectors, a zonohedron is defined
as the convex hull of all the points which can be costructed by summing
together any subset of input vectors.
The surface closing this solid is composed only of flat parallelograms,
(which have the input vectors as sides).
It is always point-symmetric.
Mesh created by this class are pure-quad meshes (triangular bit-quad),
(when coplanar vectors are fed, then planar groups of quads can be seen as
forming planar faces with more than 4 vertices).
USAGE:
1) Instantiate a Zonohedron.
2) Add input vectors at will to it, with addVector(s)
3) When you are done, call createMesh.
*/
template <class Scalar>
class Zonohedron{
public:
typedef Point3<Scalar> Vec3;
Zonohedron(){}
void addVector(Scalar x, Scalar y, Scalar z);
void addVector(Vec3 v);
void addVectors(const std::vector< Vec3 > );
const std::vector< Vec3 >& vectors() const {
return vec;
}
template<class MeshType>
void createMesh( MeshType& output );
private:
/* classes for internal use */
/****************************/
typedef int VecIndex; // a number in [0..n)
/* the signature of a vertex (a 0 or 1 per input vector) */
struct Signature {
std::vector< bool > v;
Signature(){}
Signature(int n){ v.resize(n,false); }
bool operator == (const Signature & b) const {
return (b.v == v);
}
bool operator < (const Signature & b) const {
return (b.v < v);
}
Signature& set(VecIndex i, bool value){
v[i] = value;
return *this;
}
Signature& set(VecIndex i, bool valueI, VecIndex j, bool valueJ){
v[i] = valueI;
v[j] = valueJ;
return *this;
}
};
struct Face {
int vert[4]; // index to vertex array
};
/* precomputed cross products for all pairs of vectors */
std::vector< Vec3 > precomputedCross;
void precompteAllCrosses(){
precomputedCross.resize(n*n);
for (int i=0; i<n; i++) for (int j=0; j<n; j++) {
precomputedCross[i*n+j] = vec[i] ^ vec[j] ;
}
}
Vec3 cross(VecIndex i, VecIndex j){
return precomputedCross[i*n+j];
}
// given a vector, returns a copy pointing a unique verse
static Vec3 uniqueVerse(Vec3 v){
if (v.X()>0) return v;
else if (v.X()<0) return -v;
else if (v.Y()>0) return v;
else if (v.Y()<0) return -v;
else if (v.Z()>0) return v;
return -v;
}
static Vec3 altVec(int i) {
return Vec3(1, i, i*i);
}
static Scalar tripleProduct( const Vec3 &a, const Vec3 &b, const Vec3 & c){
return ( a ^ b ) * c;
}
// returns signof: (i x j) * k
bool signOf_IxJoK(VecIndex i, VecIndex j, VecIndex k){
const float EPSILON_SQUARED = 1e-12;
bool invert = false;
// sort i,j,k
if (i<j) { std::swap(i,j); invert = !invert; }
if (j<k) { std::swap(j,k); invert = !invert;
if (i<j) { std::swap(i,j); invert = !invert; }
}
//Scalar res = Vec3::dot( Vec3::cross( vec[i] , vec[j] ) , vec[k] );
Scalar res = cross( i , j ) * vec[k] ;
if (res*res<=EPSILON_SQUARED) {
// three coplanar vectors!
// use derivative...
//res = uniqueVerse( cross(i,j) ) * cross(j,k) ;
res = tripleProduct( altVec(i), vec[j], vec[k]) +
tripleProduct( vec[i], altVec(j), vec[k]) +
tripleProduct( vec[i], vec[j], altVec(k)) ;
if (res*res<=EPSILON_SQUARED) {
// zero derivative (happens, if three colinear vectors, or...)
res = tripleProduct( vec[i], altVec(j), altVec(k)) +
tripleProduct( altVec(i), vec[j], altVec(k)) +
tripleProduct( altVec(i), altVec(j), vec[k]) ;
}
if (res*res<=EPSILON_SQUARED) {
// zero second derivative (happens if three zero-vectors, i.e. never? or...)
res = tripleProduct( altVec(i), altVec(j), altVec(k) );
}
}
return ( (res>=0) != invert ); // XOR
}
int n; // number of input vectors
std::vector<Vec3> vec; // input vectors
int vertCount;
std::vector<Face> _face;
typedef std::map< Signature, int > VertexMap;
VertexMap vertexMap;
// given a vertex signature, returns index of vert (newly created or not)
VecIndex vertexIndex(const Signature &s){
typename VertexMap::iterator i;
//Vec3 pos = s; //toPos(s);
i = vertexMap.find( s );
if (i!= vertexMap.end() ) return i->second;
else {
int newVertex = vertCount++;
//vertexMap.insert(s)
vertexMap[s] = newVertex;
return newVertex;
}
}
// given two index of vectors, returns face
Face& face(VecIndex i, VecIndex j){
assert(i!=j);
assert( i*n + j < (int) _face.size() );
return _face[i*n + j];
}
Vec3 toPos(const Signature &s) const{
Vec3 res(0,0,0);
for (int i=0; i<n; i++)
if (s.v[i]) res += vec[i];
return res;
}
void createInternalMesh() {
n = vec.size();
precompteAllCrosses();
// allocate faces
_face.resize( n*n );
vertCount = 0;
vertexMap.clear();
for (int i=0; i<n; i++) {
//::showProgress(i,n);
for (int j=0; j<n; j++) if(i!=j) {
Signature s(n);
for (int k=0; k<n; k++) if ((k!=j) && (k!=i))
{
s.set( k , signOf_IxJoK( i,j,k ) );
}
face(i,j).vert[0] = vertexIndex( s.set(i,false, j,false) );
face(i,j).vert[1] = vertexIndex( s.set(i,false, j,true ) );
face(i,j).vert[2] = vertexIndex( s.set(i,true, j,true ) );
face(i,j).vert[3] = vertexIndex( s.set(i,true, j,false) );
}
}
}
};
template<class Scalar>
void Zonohedron<Scalar>::addVectors(std::vector< Zonohedron<Scalar>::Vec3 > input){
for (size_t i=0; i<input.size(); i++) {
addVector( input[i]);
}
}
template<class Scalar>
void Zonohedron<Scalar>::addVector(Scalar x, Scalar y, Scalar z) {
addVector( Vec3(x,y,z) );
}
template<class Scalar>
void Zonohedron<Scalar>::addVector(Zonohedron<Scalar>::Vec3 v){
vec.push_back(v);
}
template<class Scalar>
template<class MeshType>
void Zonohedron<Scalar>::createMesh(MeshType &m){
typedef MeshType Mesh;
typedef typename Mesh::VertexIterator MeshVertexIterator;
typedef typename Mesh::FaceIterator MeshFaceIterator;
createInternalMesh();
m.Clear();
Allocator<MeshType>::AddVertices(m,vertexMap.size());
Allocator<MeshType>::AddFaces(m,n*(n-1) * 2);
// assign vertex positions
MeshVertexIterator vi=m.vert.begin();
for (typename VertexMap::iterator i=vertexMap.begin(); i!=vertexMap.end(); i++){
(vi + i->second )->P() = toPos( i->first );
}
// assegn FV connectivity
MeshFaceIterator fi=m.face.begin();
for (int i=0; i<n; i++) {
for (int j=0; j<n; j++) if (i!=j) {
const Face &f( face(i,j) );
for (int k=0; k<2; k++) { // two tri faces per quad
for (int w=0; w<3; w++) {
fi->V(w) = &* (vi + f.vert[(w+k*2)%4] );
}
if (tri::HasPerFaceNormal(m)) {
fi->N() = cross(i,j).normalized();
}
if (tri::HasPerFaceFlags(m)) {
fi->SetF(2); // quad diagonals are faux
}
fi++;
}
}
}
}
//@}
} // End Namespace TriMesh
} // End Namespace vcg
#endif // __VCGLIB_ZONOHEDRON
|