本文整理汇总了C++中KDTree::build方法的典型用法代码示例。如果您正苦于以下问题:C++ KDTree::build方法的具体用法?C++ KDTree::build怎么用?C++ KDTree::build使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类KDTree
的用法示例。
在下文中一共展示了KDTree::build方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: box
TEST(KDTree, shouldSearchInTree) {
KDTree tree;
vector<RTShape*> shapes;
RTSphere sphere0(Vector(2.5, 2.5, 2.5), 2.4);
RTSphere sphere1(Vector(2.5, 2.5, 7.5), 2.4);
RTSphere sphere2(Vector(2.5, 7.5, 7.5), 2.4);
RTSphere sphere3(Vector(2.5, 7.5, 2.5), 2.4);
RTSphere sphere4(Vector(7.5, 2.5, 2.5), 2.4);
RTSphere sphere5(Vector(7.5, 2.5, 7.5), 2.4);
RTSphere sphere6(Vector(7.5, 7.5, 7.5), 2.4);
RTSphere sphere7(Vector(7.5, 7.5, 2.5), 2.4);
shapes.push_back(&sphere0);
shapes.push_back(&sphere1);
shapes.push_back(&sphere2);
shapes.push_back(&sphere3);
shapes.push_back(&sphere4);
shapes.push_back(&sphere5);
shapes.push_back(&sphere6);
shapes.push_back(&sphere7);
BoundingBox box(Vector(0,0,0), Vector(10,10,10));
tree.setBoundingBox(box);
tree.setTerminationCondition(1);
tree.build(shapes, 0);
Ray ray(Vector(7.5, 7.5, -2 ), Vector(0,0,1));
IntersectionPtr intersection = tree.intersect(ray);
CHECK( intersection != nullptr );
CHECK( intersection->getShape() == &sphere7 );
}
示例2: triangle
TEST(KDTree, mixingTrianglesAndSphereIsNotAProblem) {
KDTree tree;
vector<RTShape*> shapes;
RTTriangle triangle(Vector(0.1,0.1,0.1), Vector(1,1,1), Vector(0.1,2,0.1));
RTSphere sphere(Vector(-1,-1,-1), 1);
shapes.push_back(&triangle);
shapes.push_back(&sphere);
BoundingBox box(Vector(-2,-2,-2), Vector(2,4,2));
tree.setBoundingBox(box);
tree.build(shapes, 0);
CHECK_EQUAL( 1, tree.getLeft()->size() );
CHECK_EQUAL( 1, tree.getRight()->size() );
}
示例3: kNearestItems
//=======================================================================//
void stitch::Vec3::relaxEquidistantVectorsII(std::vector<stitch::Vec3> &vectors, uint32_t numIterations)
{
const uint32_t numVectors = vectors.size();
for (uint32_t iterationsDone=0; iterationsDone<numIterations; ++iterationsDone)
{
float minResult=0.0f;
float maxResult=0.0f;
float sumResult=0.0f;
KDTree kdTree;
for (uint32_t acteeVectorNum=0; acteeVectorNum<numVectors; ++acteeVectorNum)
{
kdTree.addItem(new BoundingVolume(vectors[acteeVectorNum], 0.0f, acteeVectorNum));
}
std::vector<stitch::Vec3> splitAxisVec;
splitAxisVec.push_back(Vec3(1.0f, 0.0f, 0.0f));
splitAxisVec.push_back(Vec3(0.0f, 1.0f, 0.0f));
splitAxisVec.push_back(Vec3(0.0f, 0.0f, 1.0f));
kdTree.build(KDTREE_DEFAULT_CHUNK_SIZE, 0, 1000, splitAxisVec);
for (uint32_t acteeVectorNum=0; acteeVectorNum<numVectors; ++acteeVectorNum)
{
stitch::Vec3 relaxDelta;//Initialised to zero in the constructor.
float minActDistance=10.0;//Some large initial distance.
KNearestItems kNearestItems(vectors[acteeVectorNum], 1.0f, numVectors/100);
kdTree.getNearestK(&kNearestItems);
const uint32_t numNearestItems=kNearestItems.numItems_;
for (uint32_t i=0; i<numNearestItems; ++i)
{
uint32_t actingVectorNum=kNearestItems.heapArray_[i].second->userIndex_;
if (actingVectorNum!=acteeVectorNum)
{
const float actDistance=(kNearestItems.heapArray_[i].second->centre_-vectors[acteeVectorNum]).lengthSq() / kNearestItems.searchRadiusSq_;
const float actWeight=1.0f - actDistance;
stitch::Vec3 actNormal=(vectors[acteeVectorNum] - vectors[actingVectorNum]);
actNormal.normalise();
relaxDelta+=actNormal * (actWeight);
if (actDistance < minActDistance) minActDistance=actDistance;
}
}
vectors[acteeVectorNum] += relaxDelta*sqrtf(minActDistance*kNearestItems.searchRadiusSq_)*0.1;//+Vec3::randNorm()*0.005*minActDistance;
vectors[acteeVectorNum].normalise();
if (acteeVectorNum>0)
{
sumResult+=minActDistance;
if (minActDistance<minResult) minResult=minActDistance;
if (minActDistance>maxResult) maxResult=minActDistance;
} else
{
sumResult=minResult=maxResult=minActDistance;
}
}
//std::cout << "relaxActingDistance = [" << minResult << "|" << (sumResult/numVectors) << "|" << maxResult << "]\n";
//std::cout.flush();
}
}
示例4: main
int main(int argc, char** argv) {
std::ifstream imageFile;
std::ifstream labelFile;
if(argc != 5) {
std::cerr << "Invalid command." << std::endl;
return -1;
}
// Open training images and their labels for reading.
imageFile.open(argv[1], std::ios::binary);
labelFile.open(argv[2], std::ios::binary);
labelFile.seekg(8);
BitInputStream* input = new BitInputStream(imageFile);
BitInputStream* label = new BitInputStream(labelFile);
// Get the magic number, the rows, and the columns of each training image.
int magic = input->readInt();
int total = input->readInt();
std::cerr << "Loading training images" << std::endl;
std::cerr << "Total image: " << total << std::endl;
int rows = input->readInt();
std::cerr << "Each image contains " << rows << " rows." << std::endl;
int columns = input->readInt();
std::cerr << "Each image contains " << columns << " columns." << std::endl;
// Load the training data to memory.
Training* training = new Training(total, rows * columns);
for(int i = 0; i < total; i++) {
int lbl = (int)(label->readChar());
TRPoint* point = new TRPoint(lbl, rows * columns, i);
for(int j = 0; j < (rows * columns); j++) {
int pixel = (int)(input->readChar());
point->addPixel(pixel);
}
training->addElement(point);
}
imageFile.close(); labelFile.close();
std::ifstream testImageFile;
std::ifstream testLabelFile;
// Open testing images and their actual labels for reading.
testImageFile.open(argv[3], std::ios::binary);
testLabelFile.open(argv[4], std::ios::binary); testLabelFile.seekg(8);
input = new BitInputStream(testImageFile);
label = new BitInputStream(testLabelFile);
magic = input->readInt();
total = input->readInt();
std::cerr << "Loading testing images" << std::endl;
std::cerr << "Total image: " << total << std::endl;
rows = input->readInt();
std::cerr << "Each image contains " << rows << " rows." << std::endl;
columns = input->readInt();
std::cerr << "Each image contains " << columns << " columns." << std::endl;
// Construct the K-D for nearest neighbor search.
std::cerr << "Please enter the number of elements in each leaf for your K-D tree: ";
std::string numImages;
getline(std::cin, numImages);
std::cerr << "Ok, at least " << atoi(numImages.c_str()) << " images." << std::endl;
std::cerr << "Constructing K-D tree for training set." << std::endl;
KDTree* tree = new KDTree();
tree->root = tree->build(tree->root, training, training->size(), atoi(numImages.c_str()));
// Load the testing data to memory.
Training* testing = new Training(total, rows * columns);
for(int i = 0; i < total; i++) {
int lbl = (int)(label->readChar());
TRPoint* point = new TRPoint(lbl, rows * columns);
for(int j = 0; j < (rows * columns); j++) {
int pixel = (int)(input->readChar());
point->addPixel(pixel);
}
testing->addElement(point);
}
testImageFile.close(); testLabelFile.close();
// Loading the actual true nearest neighbor of each testing images to memory.
std::ifstream actualLabels;
actualLabels.open("actual");
std::string in;
int* trueLabels = new int[total];
int index = 0;
for(int i = 0; getline(actualLabels, in); i++) trueLabels[i] = atoi(in.c_str());
int errors = 0;
int notTrueNN = 0;
// Perform the nearest neighbor search.
for(int o = 0; o < testing->size(); o++) {
std::cerr << "classifying image " << o+1 << std::endl;
int currentDist = INT_MAX;
int currentLabel = -1;
//.........这里部分代码省略.........