vcglib/apps/nexus/pintersect.h

150 lines
4.4 KiB
C
Raw Normal View History

2004-07-01 23:46:36 +02:00
/****************************************************************************
* VCGLib o o *
* Visual and Computer Graphics Library o o *
* _ O _ *
* Copyright(C) 2004 \/)\/ *
* 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. *
* *
****************************************************************************/
/****************************************************************************
History
$Log: not supported by cvs2svn $
2004-07-20 16:11:07 +02:00
Revision 1.2 2004/07/01 21:46:36 ponchio
Added header.
2004-07-01 23:46:36 +02:00
****************************************************************************/
2004-07-01 23:40:25 +02:00
#ifndef PARTITION_INTERSECT_H
#define PARTITION_INTERSECT_H
#include <map>
2004-07-20 16:11:07 +02:00
#include <set>
#include <string>
2004-07-01 23:40:25 +02:00
#include <vcg/space/point3.h>
namespace nxs {
2004-07-20 16:11:07 +02:00
struct IPair {
unsigned int fine;
unsigned int coarse;
bool operator<(const IPair &p) const {
2004-07-01 23:40:25 +02:00
if(fine == p.fine) return coarse < p.coarse;
return fine < p.fine;
}
};
2004-07-20 16:11:07 +02:00
struct ICell {
2004-07-01 23:40:25 +02:00
unsigned int index;
unsigned int count;
};
2004-07-20 16:11:07 +02:00
template <class Partition> class PIntersect {
public:
Partition *fine;
Partition *coarse;
// typedef typename Partition::Key Key;
2004-07-01 23:40:25 +02:00
unsigned int last_cell;
2004-07-20 16:11:07 +02:00
std::map<IPair, ICell> pairs;
2004-07-01 23:40:25 +02:00
std::set<unsigned int> cells;
2004-07-20 16:11:07 +02:00
PIntersect(Partition *f = NULL, Partition *c = NULL):
2004-07-01 23:40:25 +02:00
fine(f), coarse(c), last_cell(0) {}
2004-07-20 16:11:07 +02:00
void Init(Partition *f, Partition *c, unsigned int of_cell) {
fine = f;
coarse = c;
last_cell = of_cell;
}
2004-07-01 23:40:25 +02:00
unsigned int Locate(const vcg::Point3f &p) {
2004-07-20 16:11:07 +02:00
IPair pp;
pp.fine = fine->Locate(p);
pp.coarse = coarse->Locate(p);
2004-07-01 23:40:25 +02:00
if(pairs.count(pp)) { //already there
2004-07-20 16:11:07 +02:00
ICell &cell = pairs[pp];
2004-07-01 23:40:25 +02:00
cell.count++;
return cell.index;
} else {
2004-07-20 16:11:07 +02:00
ICell &cell = pairs[pp];
2004-07-01 23:40:25 +02:00
cell.index = last_cell++;
cell.count = 1;
cells.insert(cell.index);
return cell.index;
}
}
unsigned int Relocate(const vcg::Point3f &p) {
//TODO!!!!!!
}
void Prune(unsigned int threshold) {
//TODO!!!!!!
}
2004-07-20 16:11:07 +02:00
2004-07-01 23:40:25 +02:00
unsigned int Count(unsigned int cell) {
return cells.count(cell);
}
2004-07-20 16:11:07 +02:00
bool Save(const std::string &file) {
FILE *fp = fopen((file + ".chi").c_str(), "wb+");
if(!fp)
return false;
unsigned int n = pairs.size();
fwrite(&n, sizeof(unsigned int), 1, fp);
std::map<IPair, ICell>::iterator i;
for(i = pairs.begin(); i != pairs.end(); i++) {
IPair pair = (*i).first;
ICell cell = (*i).second;
fwrite(&pair, sizeof(IPair), 1, fp);
fwrite(&cell, sizeof(ICell), 1, fp);
}
fclose(fp);
return true;
}
bool Load(const std::string &file) {
pairs.clear();
cells.clear();
FILE *fp = fopen((file + ".chi").c_str(), "rb");
if(!fp)
return false;
unsigned int n;
fread(&n, sizeof(unsigned int), 1, fp);
IPair pair;
ICell cell;
for(unsigned int i = 0; i < n; i++) {
fread(&pair, sizeof(IPair), 1, fp);
fread(&cell, sizeof(ICell), 1, fp);
pairs[pair] = cell;
cells.insert(cell.index);
}
fclose(fp);
return true;
}
};
}//namespace
2004-07-01 23:40:25 +02:00
#endif