VCG Library
aabb_binary_tree.cpp
1 /****************************************************************************
2 * VCGLib o o *
3 * Visual and Computer Graphics Library o o *
4 * _ O _ *
5 * Copyright(C) 2004-2016 \/)\/ *
6 * Visual Computing Lab /\/| *
7 * ISTI - Italian National Research Council | *
8 * \ *
9 * All rights reserved. *
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 * This program is distributed in the hope that it will be useful, *
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
19 * GNU General Public License (http://www.gnu.org/licenses/gpl.txt) *
20 * for more details. *
21 * *
22 ****************************************************************************/
23 #include <stdio.h>
24 
25 #include<vcg/complex/complex.h>
26 #include<vcg/simplex/face/distance.h>
27 #include<vcg/simplex/face/component_ep.h>
28 #include <vcg/complex/algorithms/create/platonic.h>
29 #include <vcg/complex/algorithms/update/normal.h>
30 #include <vcg/complex/algorithms/update/component_ep.h>
31 #include <vcg/complex/algorithms/update/flag.h>
32 #include <vcg/space/intersection3.h>
33 #include <vcg/space/index/aabb_binary_tree/aabb_binary_tree.h>
34 
35 typedef float AScalarType;
36 
37 using namespace vcg;
38 
39 class AVertex;
40 class AFace;
41 
42 struct MyUsedTypes : public vcg::UsedTypes< vcg::Use<AVertex> ::AsVertexType,
43  vcg::Use<AFace> ::AsFaceType>{};
44 
45 class AVertex : public Vertex< MyUsedTypes, vertex::Normal3f, vertex::Coord3f,vertex::BitFlags >{};
46 class AFace : public Face< MyUsedTypes, face::VertexRef, face::Normal3f, face::EdgePlane, face::BitFlags> {};
47 class AMesh : public vcg::tri::TriMesh< std::vector<AVertex>, std::vector<AFace> > { };
48 
49 typedef vcg::AABBBinaryTreeIndex<AFace, AScalarType, vcg::EmptyClass> AIndex;
50 
51 static AMesh gMesh;
52 static AIndex gIndex;
53 
54 static void CreateMesh(void) {
55  vcg::tri::Dodecahedron<AMesh>(gMesh);
56 
60 }
61 
62 static void SetIndex(void) {
63  gIndex.Set(gMesh.face.begin(), gMesh.face.end());
64 }
65 
66 static void TestClosest(void) {
67  vcg::face::PointDistanceEPFunctor<AIndex::ScalarType> getPtDist;
68  const AIndex::CoordType queryPoint((AIndex::ScalarType)0, (AIndex::ScalarType)0, (AIndex::ScalarType)0);
69  const AIndex::ScalarType maxDist = std::numeric_limits<AIndex::ScalarType>::max();
70 
71  AIndex::ObjPtr closestFace;
72  AIndex::ScalarType closestDist;
73  AIndex::CoordType closestPoint;
74 
75  vcg::EmptyClass a;
76  closestFace = gIndex.GetClosest(getPtDist, a, queryPoint, maxDist, closestDist, closestPoint);
77 
78  printf("GetClosest Test:\n");
79 
80  if (closestFace != 0) {
81  printf("\tface : 0x%p\n", closestFace);
82  printf("\tdistance : %f\n", closestDist);
83  printf("\tpoint : [%f, %f, %f]\n", closestPoint[0], closestPoint[1], closestPoint[2]);
84  }
85  else {
86  printf("\tno object found (index is probably empty).\n");
87  }
88 }
89 
90 static void TestKClosest(void) {
91  vcg::face::PointDistanceEPFunctor<AIndex::ScalarType> getPtDist;
92  const unsigned int k = 10;
93  const AIndex::CoordType queryPoint((AIndex::ScalarType)0, (AIndex::ScalarType)0, (AIndex::ScalarType)0);
94  const AIndex::ScalarType maxDist = std::numeric_limits<AIndex::ScalarType>::max();
95 
96  std::vector<AIndex::ObjPtr> closestObjects;
97  std::vector<AIndex::ScalarType> closestDistances;
98  std::vector<AIndex::CoordType> closestPoints;
99 
100  vcg::EmptyClass a;
101  unsigned int rk = gIndex.GetKClosest(getPtDist, a, k, queryPoint, maxDist, closestObjects, closestDistances, closestPoints);
102 
103  printf("GetKClosest Test:\n");
104  printf("\tfound %d objects\n", rk);
105 }
106 
107 static void TestRay(void) {
108  const bool TEST_BACK_FACES = true;
109 
110  vcg::RayTriangleIntersectionFunctor<TEST_BACK_FACES> rayIntersector;
111  const AIndex::ScalarType maxDist = std::numeric_limits<AIndex::ScalarType>::max();
112  const AIndex::CoordType rayOrigin((AIndex::ScalarType)0, (AIndex::ScalarType)0, (AIndex::ScalarType)0);
113  const AIndex::CoordType rayDirection((AIndex::ScalarType)1, (AIndex::ScalarType)0, (AIndex::ScalarType)0);
114  const vcg::Ray3<AIndex::ScalarType, false> ray(rayOrigin, rayDirection);
115 
116  AIndex::ObjPtr isectFace;
117  AIndex::ScalarType rayT;
118  AIndex::CoordType isectPt;
119 
120  vcg::EmptyClass a;
121  isectFace = gIndex.DoRay(rayIntersector, a , ray, maxDist, rayT);
122 
123  printf("DoRay Test:\n");
124  if (isectFace != 0) {
125  printf("\tface : 0x%p\n", isectFace);
126  printf("\tray t : %f\n", rayT);
127  }
128  else {
129  printf("\tno object found (index is probably empty).\n");
130  }
131 }
132 
133 int main (int /*argc*/, char ** /*argv*/) {
134  CreateMesh();
135 
136  SetIndex();
137 
138  printf("Spatial Index Tests\n");
139  printf("---\n");
140  TestClosest();
141  printf("---\n");
142  TestKClosest();
143  printf("---\n");
144  TestRay();
145  printf("---\n");
146 
147  return (0);
148 }