├── .gitignore ├── 2D ├── KdTree.cpp ├── KdTree.h ├── Makefile ├── Node.cpp ├── Node.h ├── Screenshots │ ├── depth0.png │ ├── depth1.png │ ├── depth2.png │ ├── depth3.png │ └── depth4.png ├── main.cpp └── nbproject │ ├── Makefile-Debug.mk │ ├── Makefile-Release.mk │ ├── Makefile-impl.mk │ ├── Makefile-variables.mk │ ├── Package-Debug.bash │ ├── Package-Release.bash │ ├── configurations.xml │ └── project.xml ├── 3D ├── ConvexHull.cpp ├── ConvexHull.h ├── Demos │ ├── bimba.txt │ ├── domo.txt │ ├── icosaedro.txt │ ├── superficie_teste.txt │ ├── superficie_teste2.txt │ └── tetraedro.txt ├── IntersectRayAABB.h ├── IntersectRayTriangle.h ├── KdTree.cpp ├── KdTree.h ├── Makefile ├── Node.cpp ├── Node.h ├── Point2D.h ├── PointInPolygon.cpp ├── PointInPolygon.h ├── Ray.cpp ├── Ray.h ├── Render.h ├── Screenshots │ ├── depth0.png │ ├── depth1.png │ ├── depth2.png │ ├── depth3.png │ ├── depth4.png │ └── depth5.png ├── Timer.h ├── Tipos.h ├── Vector2D.h ├── Vector3D.h ├── main.cpp └── nbproject │ ├── Makefile-Debug.mk │ ├── Makefile-Release.mk │ ├── Makefile-impl.mk │ ├── Makefile-variables.mk │ ├── Package-Debug.bash │ ├── Package-Release.bash │ ├── configurations.xml │ ├── private │ ├── Makefile-variables.mk │ ├── configurations.xml │ ├── launcher.properties │ ├── private.properties │ └── private.xml │ ├── project.properties │ └── project.xml └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | # NetBeans specific # 2 | nbproject/private/ 3 | build/ 4 | nbbuild/ 5 | dist/ 6 | nbdist/ 7 | nbactions.xml 8 | nb-configuration.xml 9 | 10 | # Class Files # 11 | *.class 12 | 13 | # Package Files # 14 | *.jar 15 | *.war 16 | *.ear -------------------------------------------------------------------------------- /2D/KdTree.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * File: KdTree.cpp 3 | * Author: paulaceccon 4 | * 5 | * Created on 10 de Novembro de 2013, 11:11 6 | */ 7 | 8 | #include "KdTree.h" 9 | #include 10 | #include 11 | 12 | 13 | bool comp_x( const Triangle &t1, const Triangle &t2 ) 14 | { 15 | return t1.centroid_x < t2.centroid_x; 16 | } 17 | 18 | 19 | 20 | bool comp_y( const Triangle &t1, const Triangle &t2 ) 21 | { 22 | return t1.centroid_y < t2.centroid_y; 23 | } 24 | 25 | 26 | 27 | bool comp_z( const Triangle &t1, const Triangle &t2 ) 28 | { 29 | return t1.centroid_z < t2.centroid_z; 30 | } 31 | 32 | 33 | 34 | float find_max( float v1, float v2, float v3 ) 35 | { 36 | return std::max( std::max ( v1, v2 ), v3 ); 37 | } 38 | 39 | 40 | 41 | float find_min( float v1, float v2, float v3 ) 42 | { 43 | return std::min( std::min ( v1, v2 ), v3 ); 44 | } 45 | 46 | 47 | 48 | KdTree::KdTree( float * coordinates, int * triangleList, int numTriangles, int rSpace) 49 | { 50 | triangles = BuildTriangles ( coordinates, triangleList, numTriangles, rSpace); 51 | root = BuildKdTree( triangles, coordinates, triangleList, 0, numTriangles, 0, rSpace ); 52 | } 53 | 54 | 55 | 56 | std::vector KdTree::BuildTriangles( float * coordinates, int * triangleList, 57 | int numTriangles, int rSpace ) 58 | { 59 | for (int i = 0; i < numTriangles; i ++) 60 | { 61 | 62 | Triangle t; 63 | t.triangleCode = i; 64 | 65 | int index_1 = triangleList[i * 3]; 66 | int index_2 = triangleList[i * 3 + 1]; 67 | int index_3 = triangleList[i * 3 + 2]; 68 | 69 | t.centroid_x = ( coordinates[index_1 * rSpace] + coordinates[index_2 * rSpace] + coordinates[index_3 * rSpace] )/3; 70 | t.centroid_y = ( coordinates[index_1 * rSpace + 1] + coordinates[index_2 * rSpace + 1] + coordinates[index_3 * rSpace + 1] )/3; 71 | 72 | if (rSpace == 3) 73 | { 74 | t.centroid_z = ( coordinates[index_1 * rSpace + 2] + coordinates[index_2 * rSpace + 2] + coordinates[index_3 * rSpace + 2] )/3; 75 | } 76 | triangles.push_back( t ); 77 | } 78 | 79 | return triangles; 80 | } 81 | 82 | 83 | 84 | Node * KdTree::BuildKdTree( std::vector &triangles, float * coordinates, int * triangleList, 85 | int begin, int end, int depth, int rSpace ) 86 | { 87 | if (end - begin <= 0) 88 | return 0; 89 | 90 | int axis = depth % rSpace; 91 | 92 | // Sorting by axis to insert 93 | if (axis == 0) 94 | { 95 | std::sort( triangles.begin( ) + begin, triangles.begin( ) + end, comp_x ); 96 | } 97 | else if (axis == 1) 98 | { 99 | std::sort( triangles.begin( ) + begin, triangles.begin( ) + end, comp_y ); 100 | } 101 | else if (axis == 2) 102 | { 103 | std::sort( triangles.begin( ) + begin, triangles.begin( ) + end, comp_z ); 104 | } 105 | else 106 | { 107 | printf( "Axis Error" ); 108 | } 109 | 110 | int half = (begin + end) / 2; 111 | int triangle_code = triangles[half].triangleCode; 112 | 113 | // Getting the vertices from a triangle 114 | int index_1 = triangleList[triangle_code * 3]; 115 | int index_2 = triangleList[triangle_code * 3 + 1]; 116 | int index_3 = triangleList[triangle_code * 3 + 2]; 117 | 118 | // Getting it vertices coordinates 119 | float x_1 = coordinates[index_1 * rSpace]; 120 | float x_2 = coordinates[index_2 * rSpace]; 121 | float x_3 = coordinates[index_3 * rSpace]; 122 | 123 | float y_1 = coordinates[index_1 * rSpace + 1]; 124 | float y_2 = coordinates[index_2 * rSpace + 1]; 125 | float y_3 = coordinates[index_3 * rSpace + 1]; 126 | 127 | float z_1 = 0.0; 128 | float z_2 = 0.0; 129 | float z_3 = 0.0; 130 | 131 | if (rSpace == 3) 132 | { 133 | z_1 = coordinates[index_1 * rSpace + 2]; 134 | z_2 = coordinates[index_2 * rSpace + 2]; 135 | z_3 = coordinates[index_3 * rSpace + 2]; 136 | } 137 | 138 | // Finding it bounding box 139 | float x_min = find_min(x_1, x_2, x_3); 140 | float x_max = find_max(x_1, x_2, x_3); 141 | 142 | float y_min = find_min(y_1, y_2, y_3); 143 | float y_max = find_max(y_1, y_2, y_3); 144 | 145 | float z_min = find_min(z_1, z_2, z_3); 146 | float z_max = find_max(z_1, z_2, z_3); 147 | 148 | // Creating a node for this triangle 149 | Node * median = new Node( triangle_code, x_min, y_min, z_min, x_max, y_max, z_max ); 150 | median->left = BuildKdTree( triangles, coordinates, triangleList, begin, half, depth + 1, rSpace ); 151 | median->right = BuildKdTree( triangles, coordinates, triangleList, half + 1, end, depth + 1, rSpace ); 152 | 153 | UpdateBoundingBox( median ); 154 | return median; 155 | } 156 | 157 | 158 | 159 | void KdTree::UpdateBoundingBox( Node * node ) 160 | { 161 | // Merging its bounding box with its children's one 162 | if ( node->left != 0 && node->right != 0 ) 163 | { 164 | float children_x_min = find_min(node->min_x, node->left->min_x, node->right->min_x); 165 | float children_y_min = find_min(node->min_y, node->left->min_y, node->right->min_y); 166 | float children_z_min = find_min(node->min_z, node->left->min_z, node->right->min_z); 167 | 168 | float children_x_max = find_max(node->max_x, node->left->max_x, node->right->max_x); 169 | float children_y_max = find_max(node->max_y, node->left->max_y, node->right->max_y); 170 | float children_z_max = find_max(node->max_z, node->left->max_z, node->right->max_z); 171 | 172 | // Updating it bounding box 173 | node->min_x = children_x_min; 174 | node->min_y = children_y_min; 175 | node->min_z = children_z_min; 176 | 177 | node->max_x = children_x_max; 178 | node->max_y = children_y_max; 179 | node->max_z = children_z_max; 180 | } 181 | else 182 | { 183 | if ( node->left != 0 ) 184 | { 185 | float children_x_min = std::min(node->min_x, node->left->min_x); 186 | float children_y_min = std::min(node->min_y, node->left->min_y); 187 | float children_z_min = std::min(node->min_z, node->left->min_z); 188 | 189 | float children_x_max = std::max(node->max_x, node->left->max_x); 190 | float children_y_max = std::max(node->max_y, node->left->max_y); 191 | float children_z_max = std::max(node->max_z, node->left->max_z); 192 | 193 | // Updating it bounding box 194 | node->min_x = children_x_min; 195 | node->min_y = children_y_min; 196 | node->min_z = children_z_min; 197 | 198 | node->max_x = children_x_max; 199 | node->max_y = children_y_max; 200 | node->max_z = children_z_max; 201 | } 202 | else if ( node->right != 0) 203 | { 204 | float children_x_min = std::min(node->min_x, node->right->min_x); 205 | float children_y_min = std::min(node->min_y, node->right->min_y); 206 | float children_z_min = std::min(node->min_z, node->right->min_z); 207 | 208 | float children_x_max = std::max(node->max_x, node->right->max_x); 209 | float children_y_max = std::max(node->max_y, node->right->max_y); 210 | float children_z_max = std::max(node->max_z, node->right->max_z); 211 | 212 | // Updating it bounding box 213 | node->min_x = children_x_min; 214 | node->min_y = children_y_min; 215 | node->min_z = children_z_min; 216 | 217 | node->max_x = children_x_max; 218 | node->max_y = children_y_max; 219 | node->max_z = children_z_max; 220 | } 221 | } 222 | } 223 | 224 | 225 | //void KdTree::PrintKdTree( Node * root ) 226 | //{ 227 | // if (root == 0) 228 | // { 229 | // printf( "Nil\n" ); 230 | // return; 231 | // } 232 | // printf( "%lf %lf\n", root->p.x, root->p.y ); 233 | // PrintKdTree( root->left ); 234 | // PrintKdTree( root->right ); 235 | //} 236 | 237 | 238 | 239 | KdTree::~KdTree( ) 240 | { 241 | } 242 | 243 | -------------------------------------------------------------------------------- /2D/KdTree.h: -------------------------------------------------------------------------------- 1 | /* 2 | * File: KdTree.h 3 | * Author: paulaceccon 4 | * 5 | * Created on 10 de Novembro de 2013, 11:11 6 | */ 7 | 8 | #ifndef KDTREE_H 9 | #define KDTREE_H 10 | 11 | #include 12 | #include "Node.h" 13 | 14 | /** Struct that define a triangle **/ 15 | struct Triangle 16 | { 17 | long int triangleCode; /** Triangle ID **/ 18 | float centroid_x; /** Centroid in x **/ 19 | float centroid_y; /** Centroid in y **/ 20 | float centroid_z; /** Centroid in z **/ 21 | }; 22 | 23 | class KdTree 24 | { 25 | public: 26 | 27 | /** Vector of Triangles **/ 28 | std::vector triangles; 29 | 30 | /** Root of the kd-tree **/ 31 | Node * root; 32 | 33 | /** 34 | * Fills a list of triangles with specific informations. 35 | * @param coordinates A list of coordinates. 36 | * @param triangleList Triangles indexation. 37 | * @param numTriangles The number of triangles. 38 | * @param rSpace The r-d space (2d, 3d). 39 | * @return A list of triangles. 40 | */ 41 | std::vector BuildTriangles( float * coordinates, int * triangleList, 42 | int numTriangles, int rSpace ); 43 | 44 | /** 45 | * Builds a kd-tree based on the triangles information. 46 | * @param triangles A list containing the triangles information. 47 | * @param coordinates A list of coordinates. 48 | * @param triangleList Triangles indexation. 49 | * @param begin Begin of the list of triangles to be considered. 50 | * @param end End of the list of triangles to be considered. 51 | * @param depth Depth of the kd-tree. 52 | * @param rSpace The r-d space (2d, 3d). 53 | * @return A kd-tree based on the triangles information. 54 | */ 55 | Node * BuildKdTree ( std::vector &triangles, float * coordinates, int * triangleList, 56 | int begin, int end, int depth, int rSpace ); 57 | 58 | /** 59 | * Controller to build the triangles list and the kd-tree. 60 | * @param coordinates A list of coordinates. 61 | * @param triangleList Triangles indexation. 62 | * @param numTriangles The number of triangles. 63 | * @param rSpace The r-d space (2d, 3d). 64 | * @return A kd-tree based on the triangles information. 65 | */ 66 | KdTree( float * coordinates, int * triangleList, int numTriangles, int rSpace); 67 | 68 | void UpdateBoundingBox( Node * node ); 69 | void PrintKdTree ( Node * root ); 70 | virtual ~KdTree ( ); 71 | private: 72 | 73 | }; 74 | 75 | #endif /* KDTREE_H */ 76 | 77 | -------------------------------------------------------------------------------- /2D/Makefile: -------------------------------------------------------------------------------- 1 | # 2 | # There exist several targets which are by default empty and which can be 3 | # used for execution of your targets. These targets are usually executed 4 | # before and after some main targets. They are: 5 | # 6 | # .build-pre: called before 'build' target 7 | # .build-post: called after 'build' target 8 | # .clean-pre: called before 'clean' target 9 | # .clean-post: called after 'clean' target 10 | # .clobber-pre: called before 'clobber' target 11 | # .clobber-post: called after 'clobber' target 12 | # .all-pre: called before 'all' target 13 | # .all-post: called after 'all' target 14 | # .help-pre: called before 'help' target 15 | # .help-post: called after 'help' target 16 | # 17 | # Targets beginning with '.' are not intended to be called on their own. 18 | # 19 | # Main targets can be executed directly, and they are: 20 | # 21 | # build build a specific configuration 22 | # clean remove built files from a configuration 23 | # clobber remove all built files 24 | # all build all configurations 25 | # help print help mesage 26 | # 27 | # Targets .build-impl, .clean-impl, .clobber-impl, .all-impl, and 28 | # .help-impl are implemented in nbproject/makefile-impl.mk. 29 | # 30 | # Available make variables: 31 | # 32 | # CND_BASEDIR base directory for relative paths 33 | # CND_DISTDIR default top distribution directory (build artifacts) 34 | # CND_BUILDDIR default top build directory (object files, ...) 35 | # CONF name of current configuration 36 | # CND_PLATFORM_${CONF} platform name (current configuration) 37 | # CND_ARTIFACT_DIR_${CONF} directory of build artifact (current configuration) 38 | # CND_ARTIFACT_NAME_${CONF} name of build artifact (current configuration) 39 | # CND_ARTIFACT_PATH_${CONF} path to build artifact (current configuration) 40 | # CND_PACKAGE_DIR_${CONF} directory of package (current configuration) 41 | # CND_PACKAGE_NAME_${CONF} name of package (current configuration) 42 | # CND_PACKAGE_PATH_${CONF} path to package (current configuration) 43 | # 44 | # NOCDDL 45 | 46 | 47 | # Environment 48 | MKDIR=mkdir 49 | CP=cp 50 | CCADMIN=CCadmin 51 | 52 | 53 | # build 54 | build: .build-post 55 | 56 | .build-pre: 57 | # Add your pre 'build' code here... 58 | 59 | .build-post: .build-impl 60 | # Add your post 'build' code here... 61 | 62 | 63 | # clean 64 | clean: .clean-post 65 | 66 | .clean-pre: 67 | # Add your pre 'clean' code here... 68 | 69 | .clean-post: .clean-impl 70 | # Add your post 'clean' code here... 71 | 72 | 73 | # clobber 74 | clobber: .clobber-post 75 | 76 | .clobber-pre: 77 | # Add your pre 'clobber' code here... 78 | 79 | .clobber-post: .clobber-impl 80 | # Add your post 'clobber' code here... 81 | 82 | 83 | # all 84 | all: .all-post 85 | 86 | .all-pre: 87 | # Add your pre 'all' code here... 88 | 89 | .all-post: .all-impl 90 | # Add your post 'all' code here... 91 | 92 | 93 | # build tests 94 | build-tests: .build-tests-post 95 | 96 | .build-tests-pre: 97 | # Add your pre 'build-tests' code here... 98 | 99 | .build-tests-post: .build-tests-impl 100 | # Add your post 'build-tests' code here... 101 | 102 | 103 | # run tests 104 | test: .test-post 105 | 106 | .test-pre: build-tests 107 | # Add your pre 'test' code here... 108 | 109 | .test-post: .test-impl 110 | # Add your post 'test' code here... 111 | 112 | 113 | # help 114 | help: .help-post 115 | 116 | .help-pre: 117 | # Add your pre 'help' code here... 118 | 119 | .help-post: .help-impl 120 | # Add your post 'help' code here... 121 | 122 | 123 | 124 | # include project implementation makefile 125 | include nbproject/Makefile-impl.mk 126 | 127 | # include project make variables 128 | include nbproject/Makefile-variables.mk 129 | -------------------------------------------------------------------------------- /2D/Node.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * File: Node.cpp 3 | * Author: paulaceccon 4 | * 5 | * Created on 10 de Novembro de 2013, 10:46 6 | */ 7 | 8 | #include "Node.h" 9 | 10 | 11 | 12 | Node::Node( long int triangle_index, float min_x, float min_y, float min_z, 13 | float max_x, float max_y, float max_z ) 14 | { 15 | this->triangle_index = triangle_index; 16 | this->left = 0; 17 | this->right = 0; 18 | this->min_x = min_x; 19 | this->min_y = min_y; 20 | this->max_x = max_x; 21 | this->max_y = max_y; 22 | this->min_z = min_z; 23 | this->max_z = max_z; 24 | } 25 | 26 | 27 | 28 | Node::~Node( ) 29 | { 30 | } 31 | 32 | -------------------------------------------------------------------------------- /2D/Node.h: -------------------------------------------------------------------------------- 1 | /* 2 | * File: Node.h 3 | * Author: paulaceccon 4 | * 5 | * Created on 10 de Novembro de 2013, 10:46 6 | */ 7 | 8 | #ifndef NODE_H 9 | #define NODE_H 10 | 11 | class Node 12 | { 13 | public: 14 | 15 | /** Left and right nodes **/ 16 | Node * left, * right; 17 | 18 | /** Triangle index **/ 19 | long int triangle_index; 20 | 21 | /** Bounding box **/ 22 | float min_x, min_y, max_x, max_y, min_z, max_z; 23 | 24 | /** 25 | * Instantiates a point with x, y, z coordinates. 26 | * 27 | * @param triangleIndex 28 | * @param min_x Minimum coordinate in x. 29 | * @param min_y Minimum coordinate in y. 30 | * @param max_x Maximum coordinate in x. 31 | * @param max_y Maximum coordinate in y. 32 | * @param min_z Minimum coordinate in z. 33 | * @param max_z Maximum coordinate in z. 34 | */ 35 | Node(long int triangleIndex, float min_x, float min_y, float max_x, float max_y, float min_z, float max_z); 36 | 37 | virtual ~Node(); 38 | private: 39 | 40 | }; 41 | 42 | #endif /* NODE_H */ 43 | 44 | -------------------------------------------------------------------------------- /2D/Screenshots/depth0.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/paulaceccon/KdTree4TriangularMeshes/b7b2bc03fecdd2734e7ee38b152594d6a7a8ba80/2D/Screenshots/depth0.png -------------------------------------------------------------------------------- /2D/Screenshots/depth1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/paulaceccon/KdTree4TriangularMeshes/b7b2bc03fecdd2734e7ee38b152594d6a7a8ba80/2D/Screenshots/depth1.png -------------------------------------------------------------------------------- /2D/Screenshots/depth2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/paulaceccon/KdTree4TriangularMeshes/b7b2bc03fecdd2734e7ee38b152594d6a7a8ba80/2D/Screenshots/depth2.png -------------------------------------------------------------------------------- /2D/Screenshots/depth3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/paulaceccon/KdTree4TriangularMeshes/b7b2bc03fecdd2734e7ee38b152594d6a7a8ba80/2D/Screenshots/depth3.png -------------------------------------------------------------------------------- /2D/Screenshots/depth4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/paulaceccon/KdTree4TriangularMeshes/b7b2bc03fecdd2734e7ee38b152594d6a7a8ba80/2D/Screenshots/depth4.png -------------------------------------------------------------------------------- /2D/main.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * File: main.cpp 3 | * Author: paulaceccon 4 | * 5 | * Created on 10 de Novembro de 2013, 10:43 6 | */ 7 | 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include "Node.h" 13 | #include "KdTree.h" 14 | 15 | #ifdef __APPLE__ 16 | #include 17 | #include 18 | #elif __WINDOWS__ 19 | #include 20 | #include 21 | #endif 22 | 23 | /** The triangles coordinates **/ 24 | float coordinates[] = { -1.0, 1.0, 0.0, 0.0, 2.0, 0.0, 2.0, 3.0, 0.0, 2.0, 1.0, 1.0 }; 25 | /** The triangles indexation **/ 26 | int triangleList[] = { 0, 4, 5, 0, 1, 5, 1, 5, 2, 2, 3, 5, 3, 5, 4 }; 27 | /** Number of trinagles **/ 28 | int numTriangles = 5; 29 | /** r-d space of work **/ 30 | int rSpace = 2; 31 | 32 | // Define o sistema de coordenadas: (-winW, winW) em X 33 | // e (-winH, winH) em Y. 34 | // Estes valores é sempre calculado na função reshape e valem para o tamanho 35 | // inicial da janela apenas. 36 | double winW = 25.0, winH = 25.0; 37 | 38 | // Define o tamanho do grid desenhado ao fundo. 39 | // Este valor é usado para calcular o sistema de coordenadas. 40 | const double lenghGrid = 24.0; 41 | 42 | // Tamanho da janela em pixeis. Usado para converter do sistema de coordenadas 43 | // em pixeis para o sistema de coordenadas definido na janela. 44 | int widthWin = 600, heightWin = 600; 45 | 46 | // Cor de fundo da janela e do grid. 1.0f para preto e 0.0f para branco. 47 | float colorGrid = 1.0f; 48 | 49 | // Ponto selecionado, se houver. 50 | int selectPoint = -2; 51 | 52 | // Modo de interacao 53 | enum InteractionMode 54 | { 55 | INSERT_POINTS = 0, 56 | EDIT_POINTS = 1 57 | }; 58 | InteractionMode interactionMode = INSERT_POINTS; 59 | 60 | int depth = 0; 61 | 62 | bool viewAllAABB = false; 63 | 64 | 65 | /** 66 | * Funcao que renderiza o grid. 67 | */ 68 | void renderGrid( void ) 69 | { 70 | double i = 0.0, j = 0.0; 71 | 72 | // Desenha quadradinhos do grid ao fundo. 73 | for (i = -winH; i < winH; i++) 74 | { 75 | for (j = -winW; j < winW; j++) 76 | { 77 | glBegin( GL_POLYGON ); 78 | glColor3f( fabs( colorGrid - 0.94f ), fabs( colorGrid - 0.94f ), fabs( colorGrid - 0.94f ) ); 79 | glVertex2f( i * 2, j * 2 ); 80 | glVertex2f( i * 2 + 1, j * 2 ); 81 | glVertex2f( i * 2 + 1, j * 2 + 1 ); 82 | glVertex2f( i * 2, j * 2 + 1 ); 83 | glEnd( ); 84 | } 85 | } 86 | 87 | // Desenha linhas do grid. 88 | glBegin( GL_LINES ); 89 | 90 | glColor3f( fabs( colorGrid - 0.9f ), fabs( colorGrid - 0.9f ), fabs( colorGrid - 0.9f ) ); 91 | for (i = -winW; i < winW; i++) 92 | { 93 | glVertex2f( i, -winH ); 94 | glVertex2f( i, winH ); 95 | } 96 | for (i = -winH; i < winH; i++) 97 | { 98 | glVertex2f( -winW, i ); 99 | glVertex2f( winW, i ); 100 | } 101 | 102 | // Desenha linhas para os eixos X e Y. 103 | glColor3f( fabs( colorGrid - 0.4f ), fabs( colorGrid - 0.4f ), fabs( colorGrid - 0.4f ) ); 104 | glVertex2f( 0.0, -winH ); 105 | glVertex2f( 0.0, winH ); 106 | glVertex2f( -winW, 0.0 ); 107 | glVertex2f( winW, 0.0 ); 108 | glEnd( ); 109 | } 110 | 111 | 112 | 113 | /** 114 | * Funcao que renderiza a kdtree. 115 | */ 116 | void renderNode( Node * node, int d ) 117 | { 118 | if (node == 0 || d >= depth) 119 | { 120 | return; 121 | } 122 | 123 | if (d == depth - 1 || viewAllAABB) 124 | { 125 | 126 | float max_x = node->max_x; 127 | float max_y = node->max_y; 128 | float min_x = node->min_x; 129 | float min_y = node->min_y; 130 | 131 | if (rSpace == 2) 132 | { 133 | switch (d) 134 | { 135 | case 0: 136 | glColor3f( 1, .5, 0 ); 137 | break; 138 | case 1: 139 | glColor3f( 0, 1, 1 ); 140 | break; 141 | case 2: 142 | glColor3f( 0, 1, 0 ); 143 | break; 144 | case 3: 145 | glColor3f( 1, 1, 0 ); 146 | } 147 | 148 | glBegin( GL_LINES ); 149 | glVertex2f( min_x, min_y ); 150 | glVertex2f( min_x, max_y ); 151 | 152 | glVertex2f( min_x, min_y ); 153 | glVertex2f( max_x, min_y ); 154 | 155 | glVertex2f( max_x, min_y ); 156 | glVertex2f( max_x, max_y ); 157 | 158 | glVertex2f( min_x, max_y ); 159 | glVertex2f( max_x, max_y ); 160 | glEnd( ); 161 | 162 | int i = node->triangle_index; 163 | int index_1 = triangleList[i * 3]; 164 | int index_2 = triangleList[i * 3 + 1]; 165 | int index_3 = triangleList[i * 3 + 2]; 166 | 167 | float x = (coordinates[index_1 * rSpace] + coordinates[index_2 * rSpace] + coordinates[index_3 * rSpace]) / 3; 168 | float y = (coordinates[index_1 * rSpace + 1] + coordinates[index_2 * rSpace + 1] + coordinates[index_3 * rSpace + 1]) / 3; 169 | 170 | glPointSize( 6 ); 171 | 172 | glEnable( GL_POINT_SMOOTH ); 173 | glEnable( GL_BLEND ); 174 | glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA ); 175 | glBegin( GL_POINTS ); 176 | glVertex2f( x, y ); 177 | glEnd( ); 178 | 179 | } 180 | 181 | else if (rSpace == 3) 182 | { 183 | float max_z = node->max_z; 184 | float min_z = node->min_z; 185 | 186 | // TO DO: BUILD A CUBE 187 | } 188 | } 189 | renderNode( node->left, d + 1 ); 190 | renderNode( node->right, d + 1 ); 191 | } 192 | 193 | 194 | 195 | void renderKdTree( void ) 196 | { 197 | KdTree tree( coordinates, triangleList, numTriangles, rSpace ); 198 | 199 | renderNode( tree.root, 0 ); 200 | } 201 | 202 | 203 | 204 | void RenderTriangles( ) 205 | { 206 | glColor3f( 1.0, 0.0, 0.0 ); 207 | glPolygonMode( GL_FRONT_AND_BACK, GL_LINE ); 208 | glEnableClientState( GL_VERTEX_ARRAY ); 209 | glVertexPointer( rSpace, GL_FLOAT, 0, coordinates ); 210 | glDrawElements( GL_TRIANGLES, 3 * numTriangles, GL_UNSIGNED_INT, triangleList ); 211 | glPolygonMode( GL_FRONT_AND_BACK, GL_FILL ); 212 | } 213 | 214 | 215 | 216 | /** 217 | * Funcao que recebe eventos de redesenho da tela. 218 | * Esta função deve ser chamada todas as vezes que a tela precisar ser 219 | * redesenhada. 220 | */ 221 | void display( void ) 222 | { 223 | // Limpa a janela com a cor branca ou preta, dependendo do valor de fundo do grid. 224 | glClearColor( fabs( colorGrid - 1.0 ), fabs( colorGrid - 1.0 ), fabs( colorGrid - 1.0 ), 1.0 ); 225 | glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); 226 | 227 | // Renderiza o grid de referencia que aparece ao fundo da tela. 228 | renderGrid( ); 229 | 230 | glPushMatrix( ); 231 | glTranslated( -8, -8, 0 ); 232 | 233 | glScaled( 10, 10, 0 ); 234 | 235 | // Renderiza cada triangulo. 236 | RenderTriangles( ); 237 | 238 | // Renderiza a kdtree. 239 | renderKdTree( ); 240 | 241 | glPopMatrix( ); 242 | 243 | // Troca os buffers de escrita e desenho. 244 | glutSwapBuffers( ); 245 | } 246 | 247 | 248 | 249 | /** 250 | * Recebe eventos de redimensionamento da tela. 251 | * @param width - largura da janela em pixeis. 252 | * @param heigh - altura da janela em pixeis. 253 | */ 254 | void reshape( int width, int heigh ) 255 | { 256 | // Define qual porção da janela será usada. 257 | // Os parametros são: coordenadas inferior esquerda (0,0) e a largurda 258 | // e a altura a partir do ponto. 259 | glViewport( 0, 0, width, heigh ); 260 | 261 | // Salva as dimensoes da janela em pixeis. 262 | widthWin = width; 263 | heightWin = heigh; 264 | 265 | // Calcula a variacao do sistema de coordenadas de modo que o (0,0) fique 266 | // no centro da tela. 267 | winW = (int) (width / lenghGrid + 0.5); 268 | winH = (int) (heigh / lenghGrid + 0.5); 269 | 270 | // Define a matriz de projecao como corrente para definir o sistema de 271 | // coordenadas. 272 | glMatrixMode( GL_PROJECTION ); 273 | glLoadIdentity( ); 274 | 275 | // Define o sistema de coordenadas. 276 | gluOrtho2D( -winW, winW, -winH, winH ); 277 | 278 | // Define a matriz de modelview como corrente para realizar o desenho. 279 | glMatrixMode( GL_MODELVIEW ); 280 | glLoadIdentity( ); 281 | } 282 | 283 | 284 | 285 | void keyboard( unsigned char key, int x, int y ) 286 | { 287 | switch (key) 288 | { 289 | case 'd': 290 | depth--; 291 | if (depth < 0) 292 | depth = 0; 293 | break; 294 | case 'D': 295 | depth++; 296 | break; 297 | case 'v': 298 | viewAllAABB = !viewAllAABB; 299 | break; 300 | } 301 | display( ); 302 | } 303 | 304 | 305 | 306 | /** 307 | * Função principal. 308 | * @param argc - número de argumentos do programa. 309 | * @param argv - lista de argumentos do programa. 310 | * @return 311 | */ 312 | int main( int argc, char** argv ) 313 | { 314 | // Inicializa a glut passando os argumentos do programa. 315 | glutInit( &argc, argv ); 316 | 317 | // Inicializa o modo de display. Buffer duplo, RGBA e teste de profundidade. 318 | glutInitDisplayMode( GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH ); 319 | 320 | // Inicializa o tamanho da janela inicial. 600x600 pixels. 321 | glutInitWindowSize( 600, 600 ); 322 | 323 | // Inicializa a posicao inicial da janela. 324 | glutInitWindowPosition( 100, 100 ); 325 | 326 | // Cria a janela definindo um titulo. 327 | glutCreateWindow( "2D KdTree Adapted" ); 328 | 329 | // Registra a funcao de display. Esta função é chamada toda vez que a tela 330 | // precisa ser redesenhada. 331 | glutDisplayFunc( display ); 332 | 333 | glutKeyboardFunc( keyboard ); 334 | // Resgistra a funcao de reshape. Esta funcao é chamada toda vez que a janela 335 | // precisa ser redimensionada. Inclusive em sua criacao (momento em que ela 336 | // aparece pela primeira vez na tela. 337 | glutReshapeFunc( reshape ); 338 | 339 | // Inicia o loop da glut, o que faz o programa OpenGL começar a rodar. 340 | glutMainLoop( ); 341 | 342 | return (EXIT_SUCCESS); 343 | } 344 | 345 | -------------------------------------------------------------------------------- /2D/nbproject/Makefile-Debug.mk: -------------------------------------------------------------------------------- 1 | # 2 | # Generated Makefile - do not edit! 3 | # 4 | # Edit the Makefile in the project folder instead (../Makefile). Each target 5 | # has a -pre and a -post target defined where you can add customized code. 6 | # 7 | # This makefile implements configuration specific macros and targets. 8 | 9 | 10 | # Environment 11 | MKDIR=mkdir 12 | CP=cp 13 | GREP=grep 14 | NM=nm 15 | CCADMIN=CCadmin 16 | RANLIB=ranlib 17 | CC=gcc 18 | CCC=g++ 19 | CXX=g++ 20 | FC=gfortran 21 | AS=as 22 | 23 | # Macros 24 | CND_PLATFORM=GNU-MacOSX 25 | CND_DLIB_EXT=dylib 26 | CND_CONF=Debug 27 | CND_DISTDIR=dist 28 | CND_BUILDDIR=build 29 | 30 | # Include project Makefile 31 | include Makefile 32 | 33 | # Object Directory 34 | OBJECTDIR=${CND_BUILDDIR}/${CND_CONF}/${CND_PLATFORM} 35 | 36 | # Object Files 37 | OBJECTFILES= \ 38 | ${OBJECTDIR}/KdTree.o \ 39 | ${OBJECTDIR}/Node.o \ 40 | ${OBJECTDIR}/main.o 41 | 42 | 43 | # C Compiler Flags 44 | CFLAGS= 45 | 46 | # CC Compiler Flags 47 | CCFLAGS=-framework OpenGL -framework GLUT 48 | CXXFLAGS=-framework OpenGL -framework GLUT 49 | 50 | # Fortran Compiler Flags 51 | FFLAGS= 52 | 53 | # Assembler Flags 54 | ASFLAGS= 55 | 56 | # Link Libraries and Options 57 | LDLIBSOPTIONS= 58 | 59 | # Build Targets 60 | .build-conf: ${BUILD_SUBPROJECTS} 61 | "${MAKE}" -f nbproject/Makefile-${CND_CONF}.mk ${CND_DISTDIR}/${CND_CONF}/${CND_PLATFORM}/kdtree2d 62 | 63 | ${CND_DISTDIR}/${CND_CONF}/${CND_PLATFORM}/kdtree2d: ${OBJECTFILES} 64 | ${MKDIR} -p ${CND_DISTDIR}/${CND_CONF}/${CND_PLATFORM} 65 | ${LINK.cc} -o ${CND_DISTDIR}/${CND_CONF}/${CND_PLATFORM}/kdtree2d ${OBJECTFILES} ${LDLIBSOPTIONS} 66 | 67 | ${OBJECTDIR}/KdTree.o: KdTree.cpp 68 | ${MKDIR} -p ${OBJECTDIR} 69 | ${RM} "$@.d" 70 | $(COMPILE.cc) -g -MMD -MP -MF "$@.d" -o ${OBJECTDIR}/KdTree.o KdTree.cpp 71 | 72 | ${OBJECTDIR}/Node.o: Node.cpp 73 | ${MKDIR} -p ${OBJECTDIR} 74 | ${RM} "$@.d" 75 | $(COMPILE.cc) -g -MMD -MP -MF "$@.d" -o ${OBJECTDIR}/Node.o Node.cpp 76 | 77 | ${OBJECTDIR}/main.o: main.cpp 78 | ${MKDIR} -p ${OBJECTDIR} 79 | ${RM} "$@.d" 80 | $(COMPILE.cc) -g -MMD -MP -MF "$@.d" -o ${OBJECTDIR}/main.o main.cpp 81 | 82 | # Subprojects 83 | .build-subprojects: 84 | 85 | # Clean Targets 86 | .clean-conf: ${CLEAN_SUBPROJECTS} 87 | ${RM} -r ${CND_BUILDDIR}/${CND_CONF} 88 | ${RM} ${CND_DISTDIR}/${CND_CONF}/${CND_PLATFORM}/kdtree2d 89 | 90 | # Subprojects 91 | .clean-subprojects: 92 | 93 | # Enable dependency checking 94 | .dep.inc: .depcheck-impl 95 | 96 | include .dep.inc 97 | -------------------------------------------------------------------------------- /2D/nbproject/Makefile-Release.mk: -------------------------------------------------------------------------------- 1 | # 2 | # Generated Makefile - do not edit! 3 | # 4 | # Edit the Makefile in the project folder instead (../Makefile). Each target 5 | # has a -pre and a -post target defined where you can add customized code. 6 | # 7 | # This makefile implements configuration specific macros and targets. 8 | 9 | 10 | # Environment 11 | MKDIR=mkdir 12 | CP=cp 13 | GREP=grep 14 | NM=nm 15 | CCADMIN=CCadmin 16 | RANLIB=ranlib 17 | CC=gcc 18 | CCC=g++ 19 | CXX=g++ 20 | FC=gfortran 21 | AS=as 22 | 23 | # Macros 24 | CND_PLATFORM=GNU-MacOSX 25 | CND_DLIB_EXT=dylib 26 | CND_CONF=Release 27 | CND_DISTDIR=dist 28 | CND_BUILDDIR=build 29 | 30 | # Include project Makefile 31 | include Makefile 32 | 33 | # Object Directory 34 | OBJECTDIR=${CND_BUILDDIR}/${CND_CONF}/${CND_PLATFORM} 35 | 36 | # Object Files 37 | OBJECTFILES= \ 38 | ${OBJECTDIR}/KdTree.o \ 39 | ${OBJECTDIR}/Node.o \ 40 | ${OBJECTDIR}/main.o 41 | 42 | 43 | # C Compiler Flags 44 | CFLAGS= 45 | 46 | # CC Compiler Flags 47 | CCFLAGS= 48 | CXXFLAGS= 49 | 50 | # Fortran Compiler Flags 51 | FFLAGS= 52 | 53 | # Assembler Flags 54 | ASFLAGS= 55 | 56 | # Link Libraries and Options 57 | LDLIBSOPTIONS= 58 | 59 | # Build Targets 60 | .build-conf: ${BUILD_SUBPROJECTS} 61 | "${MAKE}" -f nbproject/Makefile-${CND_CONF}.mk ${CND_DISTDIR}/${CND_CONF}/${CND_PLATFORM}/kdtree2d 62 | 63 | ${CND_DISTDIR}/${CND_CONF}/${CND_PLATFORM}/kdtree2d: ${OBJECTFILES} 64 | ${MKDIR} -p ${CND_DISTDIR}/${CND_CONF}/${CND_PLATFORM} 65 | ${LINK.cc} -o ${CND_DISTDIR}/${CND_CONF}/${CND_PLATFORM}/kdtree2d ${OBJECTFILES} ${LDLIBSOPTIONS} -framework OpenGL -framework GLUT 66 | 67 | ${OBJECTDIR}/KdTree.o: KdTree.cpp 68 | ${MKDIR} -p ${OBJECTDIR} 69 | ${RM} "$@.d" 70 | $(COMPILE.cc) -O2 -I/System/Library/Frameworks -MMD -MP -MF "$@.d" -o ${OBJECTDIR}/KdTree.o KdTree.cpp 71 | 72 | ${OBJECTDIR}/Node.o: Node.cpp 73 | ${MKDIR} -p ${OBJECTDIR} 74 | ${RM} "$@.d" 75 | $(COMPILE.cc) -O2 -I/System/Library/Frameworks -MMD -MP -MF "$@.d" -o ${OBJECTDIR}/Node.o Node.cpp 76 | 77 | ${OBJECTDIR}/main.o: main.cpp 78 | ${MKDIR} -p ${OBJECTDIR} 79 | ${RM} "$@.d" 80 | $(COMPILE.cc) -O2 -I/System/Library/Frameworks -MMD -MP -MF "$@.d" -o ${OBJECTDIR}/main.o main.cpp 81 | 82 | # Subprojects 83 | .build-subprojects: 84 | 85 | # Clean Targets 86 | .clean-conf: ${CLEAN_SUBPROJECTS} 87 | ${RM} -r ${CND_BUILDDIR}/${CND_CONF} 88 | ${RM} ${CND_DISTDIR}/${CND_CONF}/${CND_PLATFORM}/kdtree2d 89 | 90 | # Subprojects 91 | .clean-subprojects: 92 | 93 | # Enable dependency checking 94 | .dep.inc: .depcheck-impl 95 | 96 | include .dep.inc 97 | -------------------------------------------------------------------------------- /2D/nbproject/Makefile-impl.mk: -------------------------------------------------------------------------------- 1 | # 2 | # Generated Makefile - do not edit! 3 | # 4 | # Edit the Makefile in the project folder instead (../Makefile). Each target 5 | # has a pre- and a post- target defined where you can add customization code. 6 | # 7 | # This makefile implements macros and targets common to all configurations. 8 | # 9 | # NOCDDL 10 | 11 | 12 | # Building and Cleaning subprojects are done by default, but can be controlled with the SUB 13 | # macro. If SUB=no, subprojects will not be built or cleaned. The following macro 14 | # statements set BUILD_SUB-CONF and CLEAN_SUB-CONF to .build-reqprojects-conf 15 | # and .clean-reqprojects-conf unless SUB has the value 'no' 16 | SUB_no=NO 17 | SUBPROJECTS=${SUB_${SUB}} 18 | BUILD_SUBPROJECTS_=.build-subprojects 19 | BUILD_SUBPROJECTS_NO= 20 | BUILD_SUBPROJECTS=${BUILD_SUBPROJECTS_${SUBPROJECTS}} 21 | CLEAN_SUBPROJECTS_=.clean-subprojects 22 | CLEAN_SUBPROJECTS_NO= 23 | CLEAN_SUBPROJECTS=${CLEAN_SUBPROJECTS_${SUBPROJECTS}} 24 | 25 | 26 | # Project Name 27 | PROJECTNAME=KdTree2D 28 | 29 | # Active Configuration 30 | DEFAULTCONF=Debug 31 | CONF=${DEFAULTCONF} 32 | 33 | # All Configurations 34 | ALLCONFS=Debug Release 35 | 36 | 37 | # build 38 | .build-impl: .build-pre .validate-impl .depcheck-impl 39 | @#echo "=> Running $@... Configuration=$(CONF)" 40 | "${MAKE}" -f nbproject/Makefile-${CONF}.mk QMAKE=${QMAKE} SUBPROJECTS=${SUBPROJECTS} .build-conf 41 | 42 | 43 | # clean 44 | .clean-impl: .clean-pre .validate-impl .depcheck-impl 45 | @#echo "=> Running $@... Configuration=$(CONF)" 46 | "${MAKE}" -f nbproject/Makefile-${CONF}.mk QMAKE=${QMAKE} SUBPROJECTS=${SUBPROJECTS} .clean-conf 47 | 48 | 49 | # clobber 50 | .clobber-impl: .clobber-pre .depcheck-impl 51 | @#echo "=> Running $@..." 52 | for CONF in ${ALLCONFS}; \ 53 | do \ 54 | "${MAKE}" -f nbproject/Makefile-$${CONF}.mk QMAKE=${QMAKE} SUBPROJECTS=${SUBPROJECTS} .clean-conf; \ 55 | done 56 | 57 | # all 58 | .all-impl: .all-pre .depcheck-impl 59 | @#echo "=> Running $@..." 60 | for CONF in ${ALLCONFS}; \ 61 | do \ 62 | "${MAKE}" -f nbproject/Makefile-$${CONF}.mk QMAKE=${QMAKE} SUBPROJECTS=${SUBPROJECTS} .build-conf; \ 63 | done 64 | 65 | # build tests 66 | .build-tests-impl: .build-impl .build-tests-pre 67 | @#echo "=> Running $@... Configuration=$(CONF)" 68 | "${MAKE}" -f nbproject/Makefile-${CONF}.mk SUBPROJECTS=${SUBPROJECTS} .build-tests-conf 69 | 70 | # run tests 71 | .test-impl: .build-tests-impl .test-pre 72 | @#echo "=> Running $@... Configuration=$(CONF)" 73 | "${MAKE}" -f nbproject/Makefile-${CONF}.mk SUBPROJECTS=${SUBPROJECTS} .test-conf 74 | 75 | # dependency checking support 76 | .depcheck-impl: 77 | @echo "# This code depends on make tool being used" >.dep.inc 78 | @if [ -n "${MAKE_VERSION}" ]; then \ 79 | echo "DEPFILES=\$$(wildcard \$$(addsuffix .d, \$${OBJECTFILES} \$${TESTOBJECTFILES}))" >>.dep.inc; \ 80 | echo "ifneq (\$${DEPFILES},)" >>.dep.inc; \ 81 | echo "include \$${DEPFILES}" >>.dep.inc; \ 82 | echo "endif" >>.dep.inc; \ 83 | else \ 84 | echo ".KEEP_STATE:" >>.dep.inc; \ 85 | echo ".KEEP_STATE_FILE:.make.state.\$${CONF}" >>.dep.inc; \ 86 | fi 87 | 88 | # configuration validation 89 | .validate-impl: 90 | @if [ ! -f nbproject/Makefile-${CONF}.mk ]; \ 91 | then \ 92 | echo ""; \ 93 | echo "Error: can not find the makefile for configuration '${CONF}' in project ${PROJECTNAME}"; \ 94 | echo "See 'make help' for details."; \ 95 | echo "Current directory: " `pwd`; \ 96 | echo ""; \ 97 | fi 98 | @if [ ! -f nbproject/Makefile-${CONF}.mk ]; \ 99 | then \ 100 | exit 1; \ 101 | fi 102 | 103 | 104 | # help 105 | .help-impl: .help-pre 106 | @echo "This makefile supports the following configurations:" 107 | @echo " ${ALLCONFS}" 108 | @echo "" 109 | @echo "and the following targets:" 110 | @echo " build (default target)" 111 | @echo " clean" 112 | @echo " clobber" 113 | @echo " all" 114 | @echo " help" 115 | @echo "" 116 | @echo "Makefile Usage:" 117 | @echo " make [CONF=] [SUB=no] build" 118 | @echo " make [CONF=] [SUB=no] clean" 119 | @echo " make [SUB=no] clobber" 120 | @echo " make [SUB=no] all" 121 | @echo " make help" 122 | @echo "" 123 | @echo "Target 'build' will build a specific configuration and, unless 'SUB=no'," 124 | @echo " also build subprojects." 125 | @echo "Target 'clean' will clean a specific configuration and, unless 'SUB=no'," 126 | @echo " also clean subprojects." 127 | @echo "Target 'clobber' will remove all built files from all configurations and," 128 | @echo " unless 'SUB=no', also from subprojects." 129 | @echo "Target 'all' will will build all configurations and, unless 'SUB=no'," 130 | @echo " also build subprojects." 131 | @echo "Target 'help' prints this message." 132 | @echo "" 133 | 134 | -------------------------------------------------------------------------------- /2D/nbproject/Makefile-variables.mk: -------------------------------------------------------------------------------- 1 | # 2 | # Generated - do not edit! 3 | # 4 | # NOCDDL 5 | # 6 | CND_BASEDIR=`pwd` 7 | CND_BUILDDIR=build 8 | CND_DISTDIR=dist 9 | # Debug configuration 10 | CND_PLATFORM_Debug=GNU-MacOSX 11 | CND_ARTIFACT_DIR_Debug=dist/Debug/GNU-MacOSX 12 | CND_ARTIFACT_NAME_Debug=kdtree2d 13 | CND_ARTIFACT_PATH_Debug=dist/Debug/GNU-MacOSX/kdtree2d 14 | CND_PACKAGE_DIR_Debug=dist/Debug/GNU-MacOSX/package 15 | CND_PACKAGE_NAME_Debug=kdtree2d.tar 16 | CND_PACKAGE_PATH_Debug=dist/Debug/GNU-MacOSX/package/kdtree2d.tar 17 | # Release configuration 18 | CND_PLATFORM_Release=GNU-MacOSX 19 | CND_ARTIFACT_DIR_Release=dist/Release/GNU-MacOSX 20 | CND_ARTIFACT_NAME_Release=kdtree2d 21 | CND_ARTIFACT_PATH_Release=dist/Release/GNU-MacOSX/kdtree2d 22 | CND_PACKAGE_DIR_Release=dist/Release/GNU-MacOSX/package 23 | CND_PACKAGE_NAME_Release=kdtree2d.tar 24 | CND_PACKAGE_PATH_Release=dist/Release/GNU-MacOSX/package/kdtree2d.tar 25 | # 26 | # include compiler specific variables 27 | # 28 | # dmake command 29 | ROOT:sh = test -f nbproject/private/Makefile-variables.mk || \ 30 | (mkdir -p nbproject/private && touch nbproject/private/Makefile-variables.mk) 31 | # 32 | # gmake command 33 | .PHONY: $(shell test -f nbproject/private/Makefile-variables.mk || (mkdir -p nbproject/private && touch nbproject/private/Makefile-variables.mk)) 34 | # 35 | include nbproject/private/Makefile-variables.mk 36 | -------------------------------------------------------------------------------- /2D/nbproject/Package-Debug.bash: -------------------------------------------------------------------------------- 1 | #!/bin/bash -x 2 | 3 | # 4 | # Generated - do not edit! 5 | # 6 | 7 | # Macros 8 | TOP=`pwd` 9 | CND_PLATFORM=GNU-MacOSX 10 | CND_CONF=Debug 11 | CND_DISTDIR=dist 12 | CND_BUILDDIR=build 13 | CND_DLIB_EXT=dylib 14 | NBTMPDIR=${CND_BUILDDIR}/${CND_CONF}/${CND_PLATFORM}/tmp-packaging 15 | TMPDIRNAME=tmp-packaging 16 | OUTPUT_PATH=${CND_DISTDIR}/${CND_CONF}/${CND_PLATFORM}/kdtree2d 17 | OUTPUT_BASENAME=kdtree2d 18 | PACKAGE_TOP_DIR=kdtree2d/ 19 | 20 | # Functions 21 | function checkReturnCode 22 | { 23 | rc=$? 24 | if [ $rc != 0 ] 25 | then 26 | exit $rc 27 | fi 28 | } 29 | function makeDirectory 30 | # $1 directory path 31 | # $2 permission (optional) 32 | { 33 | mkdir -p "$1" 34 | checkReturnCode 35 | if [ "$2" != "" ] 36 | then 37 | chmod $2 "$1" 38 | checkReturnCode 39 | fi 40 | } 41 | function copyFileToTmpDir 42 | # $1 from-file path 43 | # $2 to-file path 44 | # $3 permission 45 | { 46 | cp "$1" "$2" 47 | checkReturnCode 48 | if [ "$3" != "" ] 49 | then 50 | chmod $3 "$2" 51 | checkReturnCode 52 | fi 53 | } 54 | 55 | # Setup 56 | cd "${TOP}" 57 | mkdir -p ${CND_DISTDIR}/${CND_CONF}/${CND_PLATFORM}/package 58 | rm -rf ${NBTMPDIR} 59 | mkdir -p ${NBTMPDIR} 60 | 61 | # Copy files and create directories and links 62 | cd "${TOP}" 63 | makeDirectory "${NBTMPDIR}/kdtree2d/bin" 64 | copyFileToTmpDir "${OUTPUT_PATH}" "${NBTMPDIR}/${PACKAGE_TOP_DIR}bin/${OUTPUT_BASENAME}" 0755 65 | 66 | 67 | # Generate tar file 68 | cd "${TOP}" 69 | rm -f ${CND_DISTDIR}/${CND_CONF}/${CND_PLATFORM}/package/kdtree2d.tar 70 | cd ${NBTMPDIR} 71 | tar -vcf ../../../../${CND_DISTDIR}/${CND_CONF}/${CND_PLATFORM}/package/kdtree2d.tar * 72 | checkReturnCode 73 | 74 | # Cleanup 75 | cd "${TOP}" 76 | rm -rf ${NBTMPDIR} 77 | -------------------------------------------------------------------------------- /2D/nbproject/Package-Release.bash: -------------------------------------------------------------------------------- 1 | #!/bin/bash -x 2 | 3 | # 4 | # Generated - do not edit! 5 | # 6 | 7 | # Macros 8 | TOP=`pwd` 9 | CND_PLATFORM=GNU-MacOSX 10 | CND_CONF=Release 11 | CND_DISTDIR=dist 12 | CND_BUILDDIR=build 13 | CND_DLIB_EXT=dylib 14 | NBTMPDIR=${CND_BUILDDIR}/${CND_CONF}/${CND_PLATFORM}/tmp-packaging 15 | TMPDIRNAME=tmp-packaging 16 | OUTPUT_PATH=${CND_DISTDIR}/${CND_CONF}/${CND_PLATFORM}/kdtree2d 17 | OUTPUT_BASENAME=kdtree2d 18 | PACKAGE_TOP_DIR=kdtree2d/ 19 | 20 | # Functions 21 | function checkReturnCode 22 | { 23 | rc=$? 24 | if [ $rc != 0 ] 25 | then 26 | exit $rc 27 | fi 28 | } 29 | function makeDirectory 30 | # $1 directory path 31 | # $2 permission (optional) 32 | { 33 | mkdir -p "$1" 34 | checkReturnCode 35 | if [ "$2" != "" ] 36 | then 37 | chmod $2 "$1" 38 | checkReturnCode 39 | fi 40 | } 41 | function copyFileToTmpDir 42 | # $1 from-file path 43 | # $2 to-file path 44 | # $3 permission 45 | { 46 | cp "$1" "$2" 47 | checkReturnCode 48 | if [ "$3" != "" ] 49 | then 50 | chmod $3 "$2" 51 | checkReturnCode 52 | fi 53 | } 54 | 55 | # Setup 56 | cd "${TOP}" 57 | mkdir -p ${CND_DISTDIR}/${CND_CONF}/${CND_PLATFORM}/package 58 | rm -rf ${NBTMPDIR} 59 | mkdir -p ${NBTMPDIR} 60 | 61 | # Copy files and create directories and links 62 | cd "${TOP}" 63 | makeDirectory "${NBTMPDIR}/kdtree2d/bin" 64 | copyFileToTmpDir "${OUTPUT_PATH}" "${NBTMPDIR}/${PACKAGE_TOP_DIR}bin/${OUTPUT_BASENAME}" 0755 65 | 66 | 67 | # Generate tar file 68 | cd "${TOP}" 69 | rm -f ${CND_DISTDIR}/${CND_CONF}/${CND_PLATFORM}/package/kdtree2d.tar 70 | cd ${NBTMPDIR} 71 | tar -vcf ../../../../${CND_DISTDIR}/${CND_CONF}/${CND_PLATFORM}/package/kdtree2d.tar * 72 | checkReturnCode 73 | 74 | # Cleanup 75 | cd "${TOP}" 76 | rm -rf ${NBTMPDIR} 77 | -------------------------------------------------------------------------------- /2D/nbproject/configurations.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 7 | KdTree.h 8 | Node.h 9 | 10 | 13 | KdTree.cpp 14 | Node.cpp 15 | main.cpp 16 | 17 | 20 | 21 | 25 | 26 | 30 | Makefile 31 | 32 | 33 | Makefile 34 | 35 | 36 | 37 | default 38 | true 39 | false 40 | 41 | 42 | 43 | -framework OpenGL -framework GLUT 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | default 60 | true 61 | false 62 | 63 | 64 | 65 | 5 66 | 67 | 68 | 5 69 | 70 | /System/Library/Frameworks 71 | 72 | 73 | 74 | 5 75 | 76 | 77 | 5 78 | 79 | 80 | -framework OpenGL -framework GLUT 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | -------------------------------------------------------------------------------- /2D/nbproject/project.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | org.netbeans.modules.cnd.makeproject 4 | 5 | 6 | KdTree2D 7 | 8 | cpp 9 | h 10 | UTF-8 11 | 12 | 13 | 14 | 15 | Debug 16 | 1 17 | 18 | 19 | Release 20 | 1 21 | 22 | 23 | 24 | false 25 | 26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /3D/ConvexHull.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * File: ConvexHull.cpp 3 | * Author: jeferson 4 | * 5 | * Created on 20 de Outubro de 2013, 18:52 6 | */ 7 | 8 | #include "ConvexHull.h" 9 | #include "Point2D.h" 10 | #include 11 | #include 12 | #include 13 | 14 | Point2D _pivot; 15 | 16 | 17 | 18 | ConvexHull::ConvexHull( Point2D* points, int numberPoints ) 19 | { 20 | _points = points; 21 | //_points = new Point2D[numberPoints]; 22 | //memcpy( _points, points, numberPoints * sizeof (Point2D) ); 23 | _numberPoints = numberPoints; 24 | } 25 | 26 | 27 | 28 | bool sortRadial( const Point2D p1, const Point2D p2 ) 29 | { 30 | Vector2D v1 = p1 - _pivot; 31 | Vector2D v2 = p2 - _pivot; 32 | return v1 % v2 >= 0.0; 33 | } 34 | 35 | 36 | 37 | void print( Point2D* p, int n ) 38 | { 39 | printf( "\nPontos no fecho\n" ); 40 | const char* a = ""; 41 | for (int i = 0; i < n; i++) 42 | { 43 | printf( "%d ", i ); 44 | p[i].print( a ); 45 | } 46 | } 47 | 48 | 49 | 50 | std::vector ConvexHull::grahamScan( ) 51 | { 52 | std::vector convexHull( _numberPoints ); 53 | //busca o pivo 54 | _pivot = _points[0]; 55 | int indexPivot = 0; 56 | for (int i = 1; i < _numberPoints; i++) 57 | { 58 | if (_pivot < _points[i]) 59 | { 60 | _pivot = _points[i]; 61 | indexPivot = i; 62 | } 63 | } 64 | //remove o pivot do vetor 65 | for (int i = indexPivot; i < _numberPoints - 1; i++) 66 | { 67 | _points[i] = _points[i + 1]; 68 | } 69 | //inclui o pivot no feixo convexo 70 | convexHull[0] = _pivot; 71 | 72 | //diminui o numero de pontos 73 | _numberPoints--; 74 | 75 | //oderna os pontos 76 | std::sort( _points, _points + _numberPoints, sortRadial ); 77 | int j = 1; 78 | for (int i = 0; i < _numberPoints; i++) 79 | { 80 | while (j > 1 && ((convexHull[j - 1] - convexHull[j - 2]) % (_points[i] - convexHull[j - 1])) <= 0) 81 | { 82 | j--; 83 | } 84 | convexHull[j++] = _points[i]; 85 | } 86 | convexHull.erase( convexHull.begin( ) + j, convexHull.end( ) ); 87 | 88 | std::vector convex(j); 89 | std::copy( &convexHull[0], &convexHull[0] + j, convex.begin( ) ); 90 | return convex; 91 | } 92 | 93 | 94 | 95 | std::vector ConvexHull::mergeConvexHull( ) 96 | { 97 | std::sort( _points, _points + _numberPoints ); 98 | return mergeHull( 0, _numberPoints ); 99 | } 100 | 101 | 102 | 103 | std::vector ConvexHull::mergeHull( int initial, int final ) 104 | { 105 | std::vector convex; 106 | if (fabs( final - initial ) < 3) 107 | { 108 | for (int i = initial; i < final; i++) 109 | { 110 | convex.push_back( _points[i] ); 111 | } 112 | return convex; 113 | } 114 | 115 | int mid = (initial + final) / 2; 116 | 117 | std::vector left = mergeHull( initial, mid ); 118 | std::vector right = mergeHull( mid, final ); 119 | 120 | if (left.size( ) + right.size( ) <= 3 && false) 121 | { 122 | std::vector convex; 123 | for (long unsigned int i = 0; i < left.size( ); i++) 124 | { 125 | convex.push_back( left[i] ); 126 | } 127 | for (long unsigned int i = 0; i < right.size( ); i++) 128 | { 129 | convex.push_back( right[i] ); 130 | } 131 | return convex; 132 | } 133 | else 134 | { 135 | return merge( left, right ); 136 | } 137 | } 138 | 139 | 140 | 141 | std::vector ConvexHull::merge( std::vector& left, std::vector& right ) 142 | { 143 | 144 | double area = 0.0; 145 | //verifica a orientacao 146 | for (unsigned int i = 0; i < left.size( ); i++) 147 | { 148 | area += left[i] % left[(i + 1) % left.size( )]; 149 | } 150 | if (area < 0) 151 | { 152 | std::reverse( left.begin( ), left.end( ) ); 153 | } 154 | 155 | area = 0.0; 156 | for (unsigned int i = 0; i < right.size( ); i++) 157 | { 158 | area += right[i] % right[(i + 1) % right.size( )]; 159 | } 160 | if (area > 0) 161 | { 162 | std::reverse( right.begin( ), right.end( ) ); 163 | } 164 | 165 | int maxElementLef = maxElement( left ); 166 | int maxElementRigh = maxElement( right ); 167 | int minElementLef = minElement( left ); 168 | int minElementRigh = minElement( right ); 169 | for (long unsigned int i = maxElementLef;; i = (i + 1) % left.size( )) 170 | { 171 | Vector2D v1 = left[i] - right[maxElementRigh]; 172 | Vector2D v2 = left[(i + 1) % left.size( )] - right[maxElementRigh]; 173 | if (v1 % v2 >= 0) 174 | { 175 | maxElementLef = i; 176 | break; 177 | } 178 | } 179 | for (long unsigned int i = maxElementRigh;; i = (i + 1) % right.size( )) 180 | { 181 | Vector2D v1 = right[i] - left[maxElementLef]; 182 | Vector2D v2 = right[(i + 1) % right.size( )] - left[maxElementLef]; 183 | if (v1 % v2 <= 0) 184 | { 185 | maxElementRigh = i; 186 | break; 187 | } 188 | } 189 | 190 | for (long unsigned int i = minElementLef;; i = (i + left.size( ) - 1) % left.size( )) 191 | { 192 | Vector2D v1 = left[(i + left.size( ) - 1) % left.size( )] - right[minElementRigh]; 193 | Vector2D v2 = left[i] - right[minElementRigh]; 194 | if (v1 % v2 >= 0) 195 | { 196 | minElementLef = i; 197 | break; 198 | } 199 | } 200 | 201 | for (long unsigned int i = minElementRigh;; i = (i + right.size( ) - 1) % right.size( )) 202 | { 203 | Vector2D v1 = right[i] - left[minElementLef]; 204 | Vector2D v2 = right[(i + right.size( ) - 1) % right.size( )] - left[minElementLef]; 205 | if (v1 % v2 >= 0) 206 | { 207 | minElementRigh = i; 208 | break; 209 | } 210 | } 211 | std::vector convex; 212 | for (int i = maxElementLef;; i = (i + 1) % left.size( )) 213 | { 214 | convex.push_back( left[i] ); 215 | if (i == minElementLef) 216 | break; 217 | } 218 | std::vector conv2; 219 | for (int i = maxElementRigh;; i = (i + 1) % right.size( )) 220 | { 221 | conv2.push_back( right[i] ); 222 | if (i == minElementRigh) 223 | break; 224 | } 225 | if (conv2.size( ) > 0) 226 | { 227 | for (int i = (int) conv2.size( ) - 1; i >= 0; i--) 228 | { 229 | convex.push_back( conv2[i] ); 230 | } 231 | } 232 | return convex; 233 | } 234 | 235 | 236 | 237 | int ConvexHull::maxElement( const std::vector& elements ) 238 | { 239 | Point2D p = elements[0]; 240 | int index = 0; 241 | for (unsigned int i = 1; i < elements.size( ); i++) 242 | { 243 | if (p.y < elements[i].y) 244 | { 245 | p = elements[i]; 246 | index = i; 247 | } 248 | } 249 | return index; 250 | } 251 | 252 | 253 | 254 | int ConvexHull::minElement( const std::vector& elements ) 255 | { 256 | Point2D p = elements[0]; 257 | int index = 0; 258 | for (unsigned int i = 1; i < elements.size( ); i++) 259 | { 260 | if (p.y > elements[i].y) 261 | { 262 | p = elements[i]; 263 | index = i; 264 | } 265 | } 266 | return index; 267 | } 268 | 269 | 270 | 271 | ConvexHull::~ConvexHull( ) 272 | { 273 | //delete[] _points; 274 | } 275 | 276 | -------------------------------------------------------------------------------- /3D/ConvexHull.h: -------------------------------------------------------------------------------- 1 | /* 2 | * File: ConvexHull.h 3 | * Author: jeferson 4 | * 5 | * Created on 20 de Outubro de 2013, 18:52 6 | */ 7 | 8 | #ifndef CONVEXHULL_H 9 | #define CONVEXHULL_H 10 | 11 | #include "Point2D.h" 12 | #include 13 | 14 | class ConvexHull 15 | { 16 | public: 17 | /** 18 | * Construtor quee recebe o conjunto dos pontos e numero de pontos 19 | * que sera calculado o convexHull 20 | * @param points - lista de pontos 21 | * @param numberPoints - numero de pontos 22 | */ 23 | ConvexHull ( Point2D* points, int numberPoints ); 24 | 25 | /** 26 | * Destrutor 27 | */ 28 | virtual ~ConvexHull ( ); 29 | 30 | /** 31 | * Calcula o Convex Hull usando Graham Scan 32 | * @return - pontos do convex hull 33 | */ 34 | std::vector grahamScan ( ); 35 | 36 | /** 37 | * Calcula o Convex Hull usando merge convex hull 38 | * @return - pontos do convex hull 39 | */ 40 | std::vector mergeConvexHull ( ); 41 | 42 | private: 43 | 44 | std::vector mergeHull ( int initial, int final ); 45 | 46 | std::vector merge ( std::vector& left, std::vector& right ); 47 | 48 | int maxElement( const std::vector& elements ); 49 | 50 | int minElement( const std::vector& elements ); 51 | 52 | /** 53 | * Lista de pontos a ser calculado o convexHull 54 | */ 55 | Point2D* _points; 56 | 57 | /** 58 | * Numero de pontos 59 | */ 60 | int _numberPoints; 61 | }; 62 | 63 | #endif /* CONVEXHULL_H */ 64 | 65 | -------------------------------------------------------------------------------- /3D/Demos/bimba.txt: -------------------------------------------------------------------------------- 1 | 133 262 2 | 0.156459 0.309434 -0.132210 3 | 0.025113 0.438097 -0.050445 4 | 0.268549 0.467069 -0.001091 5 | 0.334863 0.286956 -0.060913 6 | 0.403619 0.349078 0.145440 7 | 0.042144 0.482952 0.201154 8 | 0.173911 0.469340 0.247894 9 | 0.071732 0.309322 0.338956 10 | 0.338097 0.384080 0.241551 11 | -0.087480 0.340909 0.172139 12 | -0.087097 0.278672 0.031941 13 | -0.107970 0.264501 0.160698 14 | 0.097717 0.238112 -0.138064 15 | -0.007151 0.288460 -0.091611 16 | 0.346094 -0.466866 -0.045756 17 | 0.223826 -0.444385 -0.118909 18 | 0.261697 -0.416507 -0.225420 19 | 0.171040 -0.492346 0.060107 20 | 0.071107 -0.502704 0.097355 21 | 0.093208 -0.401386 0.120320 22 | -0.436003 -0.288593 -0.103509 23 | -0.403632 -0.252877 -0.183588 24 | -0.419931 -0.411054 -0.201164 25 | -0.365199 -0.124129 -0.031788 26 | -0.365640 -0.172114 0.027583 27 | -0.366780 -0.455471 -0.048342 28 | -0.420904 -0.441448 -0.093856 29 | -0.273112 -0.446914 -0.085639 30 | -0.329144 -0.298695 0.018420 31 | -0.057562 -0.127143 0.082586 32 | -0.213957 -0.354129 0.092802 33 | -0.090741 -0.372531 0.120819 34 | -0.239644 -0.473259 0.020507 35 | -0.132336 -0.496658 0.091327 36 | -0.040252 -0.056007 -0.172516 37 | 0.078377 -0.097723 -0.238202 38 | 0.014232 -0.316521 -0.330961 39 | -0.293682 -0.098394 -0.133790 40 | -0.244112 -0.382749 -0.317510 41 | -0.236980 -0.270958 -0.300075 42 | -0.105211 -0.285212 -0.325602 43 | -0.171682 -0.097721 -0.217865 44 | -0.330192 -0.305464 -0.216807 45 | -0.332408 -0.187490 -0.191203 46 | -0.359332 -0.401389 -0.240791 47 | -0.304210 -0.407840 -0.225044 48 | 0.269615 0.338666 0.335032 49 | 0.274289 0.197303 0.357258 50 | 0.399978 0.153935 0.255711 51 | 0.437505 0.193579 0.117558 52 | -0.000725 0.279652 0.293975 53 | 0.029036 0.203501 0.294082 54 | -0.042316 0.189718 0.211732 55 | 0.132687 0.125933 0.317778 56 | 0.237209 0.058288 0.306200 57 | -0.037780 0.119599 0.239093 58 | 0.006863 0.071902 0.216557 59 | 0.067760 -0.044930 0.251100 60 | 0.002948 -0.061889 0.140232 61 | 0.338315 0.187675 -0.047560 62 | 0.244146 0.023582 0.237192 63 | 0.310805 0.042418 0.217283 64 | 0.275841 0.127587 -0.030345 65 | 0.351187 0.088888 0.070727 66 | 0.337812 0.035760 0.122002 67 | 0.152499 -0.059241 0.243255 68 | 0.201245 -0.026112 0.285236 69 | 0.163202 -0.143771 0.147729 70 | 0.219237 -0.084736 0.179650 71 | 0.288807 -0.031889 0.150835 72 | 0.279403 -0.053592 0.206086 73 | 0.288743 -0.043056 0.040192 74 | 0.234833 -0.083289 0.065461 75 | 0.156164 0.169647 -0.104345 76 | 0.048362 0.181604 -0.120107 77 | 0.250058 0.244815 -0.112752 78 | 0.226568 0.186818 -0.106910 79 | 0.156798 0.109294 -0.113335 80 | 0.174440 0.034325 -0.043970 81 | 0.193440 0.113418 -0.061891 82 | 0.091588 -0.106952 0.056747 83 | 0.128976 -0.077478 -0.020483 84 | 0.112623 0.010900 -0.067839 85 | 0.226056 -0.318744 0.052474 86 | 0.293716 -0.326636 -0.009099 87 | 0.383113 -0.286721 -0.087029 88 | 0.186219 -0.152897 -0.024293 89 | 0.305364 -0.146084 -0.034187 90 | 0.078452 -0.150393 0.053857 91 | 0.064528 -0.231377 0.089093 92 | 0.124617 -0.056066 -0.082875 93 | 0.152498 -0.050721 -0.125828 94 | -0.100481 0.195007 0.043644 95 | -0.179441 0.187916 0.051107 96 | -0.076213 0.098694 0.089403 97 | -0.074826 0.228060 -0.102609 98 | -0.139460 0.288826 -0.066140 99 | -0.092705 0.308197 -0.117080 100 | -0.102924 0.168981 -0.079574 101 | -0.133117 0.220893 -0.077020 102 | -0.206995 0.206291 -0.054148 103 | -0.052261 0.198348 -0.050888 104 | -0.030616 0.245000 -0.075060 105 | -0.100778 0.303811 -0.050516 106 | -0.144656 0.319670 -0.001920 107 | -0.183991 0.244402 -0.008511 108 | -0.196167 0.202421 0.007355 109 | -0.195915 0.151237 -0.014910 110 | -0.139712 0.177719 -0.021781 111 | -0.132378 0.122115 0.003603 112 | -0.092164 0.065151 -0.006027 113 | -0.072848 0.134593 0.011481 114 | 0.052306 0.119415 -0.091499 115 | -0.012449 0.119537 -0.075124 116 | -0.247079 -0.159442 0.031775 117 | -0.142791 -0.080228 0.032842 118 | -0.152143 -0.031032 -0.026814 119 | -0.125625 -0.014124 -0.101440 120 | -0.038602 0.078662 -0.061650 121 | 0.005966 0.029762 -0.106339 122 | 0.302738 -0.119180 -0.105579 123 | 0.221110 -0.096073 -0.181204 124 | 0.395935 -0.427411 -0.181101 125 | 0.334108 -0.412556 -0.243167 126 | 0.281174 -0.253272 -0.234676 127 | 0.366336 -0.226702 -0.149072 128 | 0.343812 -0.216402 -0.186808 129 | 0.198747 -0.193417 -0.258922 130 | 0.127691 -0.262805 -0.315623 131 | -0.097090 -0.370643 -0.365475 132 | 0.012835 -0.375178 -0.359108 133 | 0.179999 -0.389000 -0.318986 134 | 0.111738 -0.382266 -0.341615 135 | 3 0 1 2 136 | 3 0 2 3 137 | 3 3 2 4 138 | 3 2 5 6 139 | 3 6 5 7 140 | 3 5 2 1 141 | 3 6 8 4 142 | 3 6 4 2 143 | 3 7 5 9 144 | 3 1 9 5 145 | 3 10 11 9 146 | 3 9 1 10 147 | 3 12 13 0 148 | 3 13 10 1 149 | 3 0 13 1 150 | 3 14 15 16 151 | 3 17 18 15 152 | 3 18 17 19 153 | 3 20 21 22 154 | 3 23 20 24 155 | 3 21 20 23 156 | 3 25 26 27 157 | 3 24 20 26 158 | 3 22 26 20 159 | 3 28 26 25 160 | 3 28 24 26 161 | 3 25 27 28 162 | 3 29 30 31 163 | 3 19 31 18 164 | 3 32 33 30 165 | 3 33 32 27 166 | 3 28 32 30 167 | 3 28 27 32 168 | 3 31 33 18 169 | 3 31 30 33 170 | 3 34 35 36 171 | 3 23 37 21 172 | 3 38 39 40 173 | 3 40 39 41 174 | 3 34 40 41 175 | 3 36 40 34 176 | 3 39 38 42 177 | 3 43 21 37 178 | 3 43 37 41 179 | 3 42 43 39 180 | 3 43 41 39 181 | 3 44 45 22 182 | 3 21 44 22 183 | 3 44 21 43 184 | 3 45 44 42 185 | 3 42 44 43 186 | 3 38 45 42 187 | 3 45 18 33 188 | 3 27 45 33 189 | 3 45 27 26 190 | 3 22 45 26 191 | 3 6 46 8 192 | 3 47 46 7 193 | 3 7 46 6 194 | 3 4 48 49 195 | 3 4 49 3 196 | 3 48 4 8 197 | 3 48 8 46 198 | 3 48 46 47 199 | 3 11 50 9 200 | 3 51 50 11 201 | 3 11 52 51 202 | 3 50 51 7 203 | 3 7 9 50 204 | 3 7 51 47 205 | 3 53 54 47 206 | 3 47 54 48 207 | 3 47 51 53 208 | 3 55 52 56 209 | 3 55 56 51 210 | 3 56 53 51 211 | 3 55 51 52 212 | 3 57 56 58 213 | 3 56 57 53 214 | 3 29 58 56 215 | 3 3 49 59 216 | 3 60 61 54 217 | 3 61 48 54 218 | 3 62 59 63 219 | 3 63 59 49 220 | 3 63 49 64 221 | 3 64 49 48 222 | 3 61 64 48 223 | 3 65 60 66 224 | 3 65 66 57 225 | 3 57 58 65 226 | 3 65 58 67 227 | 3 65 67 68 228 | 3 60 54 66 229 | 3 54 53 66 230 | 3 66 53 57 231 | 3 64 61 69 232 | 3 70 69 61 233 | 3 70 61 60 234 | 3 65 70 60 235 | 3 68 70 65 236 | 3 71 62 63 237 | 3 71 63 64 238 | 3 69 71 64 239 | 3 72 69 70 240 | 3 68 72 70 241 | 3 72 71 69 242 | 3 67 72 68 243 | 3 73 74 12 244 | 3 12 0 75 245 | 3 0 3 75 246 | 3 75 3 59 247 | 3 75 59 76 248 | 3 76 59 62 249 | 3 73 76 77 250 | 3 12 75 76 251 | 3 73 12 76 252 | 3 78 79 62 253 | 3 79 76 62 254 | 3 73 78 74 255 | 3 77 79 78 256 | 3 79 77 76 257 | 3 77 78 73 258 | 3 80 81 67 259 | 3 67 58 80 260 | 3 82 78 81 261 | 3 78 62 81 262 | 3 71 81 62 263 | 3 81 71 72 264 | 3 81 72 67 265 | 3 83 17 84 266 | 3 17 83 19 267 | 3 84 17 15 268 | 3 14 84 15 269 | 3 85 84 14 270 | 3 84 86 83 271 | 3 84 85 87 272 | 3 87 86 84 273 | 3 88 89 86 274 | 3 88 81 80 275 | 3 88 80 58 276 | 3 29 89 88 277 | 3 88 58 29 278 | 3 89 29 31 279 | 3 19 89 31 280 | 3 89 19 83 281 | 3 86 89 83 282 | 3 90 91 82 283 | 3 81 90 82 284 | 3 90 86 91 285 | 3 90 81 88 286 | 3 90 88 86 287 | 3 91 86 87 288 | 3 52 11 92 289 | 3 10 92 11 290 | 3 10 93 92 291 | 3 56 94 29 292 | 3 94 52 92 293 | 3 52 94 56 294 | 3 95 96 97 295 | 3 98 99 95 296 | 3 99 100 96 297 | 3 99 96 95 298 | 3 101 102 74 299 | 3 95 102 101 300 | 3 98 95 101 301 | 3 97 102 95 302 | 3 97 13 102 303 | 3 12 102 13 304 | 3 102 12 74 305 | 3 10 103 104 306 | 3 103 10 13 307 | 3 97 103 13 308 | 3 104 103 96 309 | 3 97 96 103 310 | 3 105 106 93 311 | 3 100 106 105 312 | 3 105 93 10 313 | 3 10 104 105 314 | 3 104 96 105 315 | 3 96 100 105 316 | 3 107 108 106 317 | 3 99 98 108 318 | 3 92 108 109 319 | 3 92 93 108 320 | 3 93 106 108 321 | 3 100 108 107 322 | 3 100 99 108 323 | 3 100 107 106 324 | 3 110 94 111 325 | 3 92 111 94 326 | 3 111 92 109 327 | 3 109 108 111 328 | 3 108 98 111 329 | 3 111 98 101 330 | 3 74 112 101 331 | 3 74 78 112 332 | 3 82 112 78 333 | 3 101 112 113 334 | 3 111 101 113 335 | 3 23 114 115 336 | 3 24 114 23 337 | 3 28 114 24 338 | 3 114 28 30 339 | 3 115 114 29 340 | 3 114 30 29 341 | 3 94 115 29 342 | 3 37 116 117 343 | 3 110 117 116 344 | 3 116 37 23 345 | 3 23 115 116 346 | 3 94 116 115 347 | 3 116 94 110 348 | 3 117 34 41 349 | 3 41 37 117 350 | 3 34 117 118 351 | 3 117 110 118 352 | 3 111 118 110 353 | 3 111 113 118 354 | 3 113 119 118 355 | 3 112 119 113 356 | 3 82 119 112 357 | 3 91 119 82 358 | 3 119 34 118 359 | 3 34 119 35 360 | 3 91 120 121 361 | 3 121 35 119 362 | 3 121 119 91 363 | 3 87 120 91 364 | 3 85 120 87 365 | 3 14 122 85 366 | 3 14 16 122 367 | 3 123 122 16 368 | 3 16 124 123 369 | 3 120 85 125 370 | 3 85 122 125 371 | 3 120 125 126 372 | 3 125 122 126 373 | 3 126 122 123 374 | 3 123 124 126 375 | 3 121 120 126 376 | 3 127 124 128 377 | 3 128 35 127 378 | 3 35 128 36 379 | 3 35 121 127 380 | 3 126 127 121 381 | 3 124 127 126 382 | 3 45 129 130 383 | 3 129 45 38 384 | 3 40 129 38 385 | 3 130 129 36 386 | 3 40 36 129 387 | 3 124 131 128 388 | 3 124 16 131 389 | 3 18 132 15 390 | 3 45 132 18 391 | 3 132 128 131 392 | 3 16 132 131 393 | 3 16 15 132 394 | 3 130 132 45 395 | 3 36 132 130 396 | 3 36 128 132 397 | -------------------------------------------------------------------------------- /3D/Demos/icosaedro.txt: -------------------------------------------------------------------------------- 1 | 12 20 2 | 0 0.85065080835203993 0.52573111211913 0 3 | 1 -0.85065080835203993 0.52573111211913 0 4 | 2 0.85065080835203993 -0.52573111211913 0 5 | 3 -0.85065080835203993 -0.52573111211913 0 6 | 4 0.52573111211913 0 0.85065080835203993 7 | 5 0.52573111211913 0 -0.85065080835203993 8 | 6 -0.52573111211913 0 0.85065080835203993 9 | 7 -0.52573111211913 0 -0.85065080835203993 10 | 8 0 0.85065080835203993 0.52573111211913 11 | 9 0 -0.85065080835203993 0.52573111211913 12 | 10 0 0.85065080835203993 -0.52573111211913 13 | 11 0 -0.85065080835203993 -0.52573111211913 14 | 0 0 8 4 15 | 1 0 5 10 16 | 2 2 4 9 17 | 3 2 11 5 18 | 4 1 6 8 19 | 5 1 10 7 20 | 6 3 9 6 21 | 7 3 7 11 22 | 8 0 10 8 23 | 9 1 8 10 24 | 10 2 9 11 25 | 11 3 11 9 26 | 12 4 2 0 27 | 13 5 0 2 28 | 14 6 1 3 29 | 15 7 3 1 30 | 16 8 6 4 31 | 17 9 4 6 32 | 18 10 5 7 33 | 19 11 7 5 34 | 35 | 36 | 4 4 2 37 | 0 1 -1 0 38 | 1 1 1 0 39 | 2 -1 1 0 40 | 3 -1 -1 0 41 | 0 0 0 1 42 | 1 0 0 1 43 | 2 0 0 1 44 | 3 0 0 1 45 | 0 3 2 0 46 | 1 0 1 2 47 | t = 1,6180339887498948482045868343656 48 | t1 = 0,52573111211913360602566908484789 49 | 50 | -------------------------------------------------------------------------------- /3D/Demos/tetraedro.txt: -------------------------------------------------------------------------------- 1 | 4 4 2 | 0 0.816496580927 0 -0.577350269189 3 | 1 -0.816496580927 0 -0.577350269189 4 | 2 0 0.816496580927 0.577350269189 5 | 3 0 -0.8164965809277 0.577350269189 6 | 0 0 2 1 7 | 1 1 3 0 8 | 2 0 3 2 9 | 3 2 3 1 10 | 11 | 12 | 13 | 4 4 14 | 15 | 0 10 0 0 16 | 17 | 1 -10 0 0 18 | 19 | 2 0 10 0 20 | 21 | 3 5 5 10 22 | 23 | 0 0 2 1 24 | 25 | 1 1 3 0 26 | 27 | 2 0 3 2 28 | 29 | 3 2 3 1 30 | 31 | 32 | 33 | 34 | FORMATO 35 | 36 | Primeira linha 37 | 38 | N M //N vertices e M triangulos 39 | 40 | 41 | 42 | Proximas N linhas 43 | 44 | Index x y z 45 | 46 | 47 | Proximas M linhas 48 | 49 | Index Vertice1 Vertice2 Vertice 3 50 | 51 | 0.816496580927 52 | 0.866025403784 53 | 54 | 55 | 0,81649658092772603273242802490199 56 | 0,57735026918962576450914878050198 57 | 58 | -------------------------------------------------------------------------------- /3D/IntersectRayAABB.h: -------------------------------------------------------------------------------- 1 | /* 2 | * File: ComputeIntersectRayAABB.h 3 | * Author: jeferson 4 | * 5 | * Created on 28 de Novembro de 2013, 15:30 6 | */ 7 | 8 | #ifndef INTERSECTRAYAABB_H 9 | #define INTERSECTRAYAABB_H 10 | #include "Ray.h" 11 | 12 | 13 | 14 | bool intersectRayAABB( const Ray& r, float x0, float y0, float z0, float x1, float y1, float z1 ) 15 | { 16 | switch (r._classification) 17 | { 18 | case MMM: 19 | 20 | if ((r._origin.x < x0) || (r._origin.y < y0) || (r._origin.z < z0) 21 | || (r._jbyi * x0 - y1 + r._c_xy > 0) 22 | || (r._ibyj * y0 - x1 + r._c_yx > 0) 23 | || (r._jbyk * z0 - y1 + r._c_zy > 0) 24 | || (r._kbyj * y0 - z1 + r._c_yz > 0) 25 | || (r._kbyi * x0 - z1 + r._c_xz > 0) 26 | || (r._ibyk * z0 - x1 + r._c_zx > 0) 27 | ) 28 | return false; 29 | 30 | return true; 31 | 32 | case MMP: 33 | 34 | if ((r._origin.x < x0) || (r._origin.y < y0) || (r._origin.z > z1) 35 | || (r._jbyi * x0 - y1 + r._c_xy > 0) 36 | || (r._ibyj * y0 - x1 + r._c_yx > 0) 37 | || (r._jbyk * z1 - y1 + r._c_zy > 0) 38 | || (r._kbyj * y0 - z0 + r._c_yz < 0) 39 | || (r._kbyi * x0 - z0 + r._c_xz < 0) 40 | || (r._ibyk * z1 - x1 + r._c_zx > 0) 41 | ) 42 | return false; 43 | 44 | return true; 45 | 46 | case MPM: 47 | 48 | if ((r._origin.x < x0) || (r._origin.y > y1) || (r._origin.z < z0) 49 | || (r._jbyi * x0 - y0 + r._c_xy < 0) 50 | || (r._ibyj * y1 - x1 + r._c_yx > 0) 51 | || (r._jbyk * z0 - y0 + r._c_zy < 0) 52 | || (r._kbyj * y1 - z1 + r._c_yz > 0) 53 | || (r._kbyi * x0 - z1 + r._c_xz > 0) 54 | || (r._ibyk * z0 - x1 + r._c_zx > 0) 55 | ) 56 | return false; 57 | 58 | return true; 59 | 60 | case MPP: 61 | 62 | if ((r._origin.x < x0) || (r._origin.y > y1) || (r._origin.z > z1) 63 | || (r._jbyi * x0 - y0 + r._c_xy < 0) 64 | || (r._ibyj * y1 - x1 + r._c_yx > 0) 65 | || (r._jbyk * z1 - y0 + r._c_zy < 0) 66 | || (r._kbyj * y1 - z0 + r._c_yz < 0) 67 | || (r._kbyi * x0 - z0 + r._c_xz < 0) 68 | || (r._ibyk * z1 - x1 + r._c_zx > 0) 69 | ) 70 | return false; 71 | 72 | return true; 73 | 74 | case PMM: 75 | 76 | if ((r._origin.x > x1) || (r._origin.y < y0) || (r._origin.z < z0) 77 | || (r._jbyi * x1 - y1 + r._c_xy > 0) 78 | || (r._ibyj * y0 - x0 + r._c_yx < 0) 79 | || (r._jbyk * z0 - y1 + r._c_zy > 0) 80 | || (r._kbyj * y0 - z1 + r._c_yz > 0) 81 | || (r._kbyi * x1 - z1 + r._c_xz > 0) 82 | || (r._ibyk * z0 - x0 + r._c_zx < 0) 83 | ) 84 | return false; 85 | 86 | return true; 87 | 88 | case PMP: 89 | 90 | if ((r._origin.x > x1) || (r._origin.y < y0) || (r._origin.z > z1) 91 | || (r._jbyi * x1 - y1 + r._c_xy > 0) 92 | || (r._ibyj * y0 - x0 + r._c_yx < 0) 93 | || (r._jbyk * z1 - y1 + r._c_zy > 0) 94 | || (r._kbyj * y0 - z0 + r._c_yz < 0) 95 | || (r._kbyi * x1 - z0 + r._c_xz < 0) 96 | || (r._ibyk * z1 - x0 + r._c_zx < 0) 97 | ) 98 | return false; 99 | 100 | return true; 101 | 102 | case PPM: 103 | 104 | if ((r._origin.x > x1) || (r._origin.y > y1) || (r._origin.z < z0) 105 | || (r._jbyi * x1 - y0 + r._c_xy < 0) 106 | || (r._ibyj * y1 - x0 + r._c_yx < 0) 107 | || (r._jbyk * z0 - y0 + r._c_zy < 0) 108 | || (r._kbyj * y1 - z1 + r._c_yz > 0) 109 | || (r._kbyi * x1 - z1 + r._c_xz > 0) 110 | || (r._ibyk * z0 - x0 + r._c_zx < 0) 111 | ) 112 | return false; 113 | 114 | return true; 115 | 116 | case PPP: 117 | 118 | if ((r._origin.x > x1) || (r._origin.y > y1) || (r._origin.z > z1) 119 | || (r._jbyi * x1 - y0 + r._c_xy < 0) 120 | || (r._ibyj * y1 - x0 + r._c_yx < 0) 121 | || (r._jbyk * z1 - y0 + r._c_zy < 0) 122 | || (r._kbyj * y1 - z0 + r._c_yz < 0) 123 | || (r._kbyi * x1 - z0 + r._c_xz < 0) 124 | || (r._ibyk * z1 - x0 + r._c_zx < 0) 125 | ) 126 | return false; 127 | 128 | return true; 129 | 130 | case OMM: 131 | 132 | if ((r._origin.x < x0) || (r._origin.x > x1) 133 | || (r._origin.y < y0) || (r._origin.z < z0) 134 | || (r._jbyk * z0 - y1 + r._c_zy > 0) 135 | || (r._kbyj * y0 - z1 + r._c_yz > 0) 136 | ) 137 | return false; 138 | 139 | return true; 140 | 141 | case OMP: 142 | 143 | if ((r._origin.x < x0) || (r._origin.x > x1) 144 | || (r._origin.y < y0) || (r._origin.z > z1) 145 | || (r._jbyk * z1 - y1 + r._c_zy > 0) 146 | || (r._kbyj * y0 - z0 + r._c_yz < 0) 147 | ) 148 | return false; 149 | 150 | return true; 151 | 152 | case OPM: 153 | 154 | if ((r._origin.x < x0) || (r._origin.x > x1) 155 | || (r._origin.y > y1) || (r._origin.z < z0) 156 | || (r._jbyk * z0 - y0 + r._c_zy < 0) 157 | || (r._kbyj * y1 - z1 + r._c_yz > 0) 158 | ) 159 | return false; 160 | 161 | return true; 162 | 163 | case OPP: 164 | 165 | if ((r._origin.x < x0) || (r._origin.x > x1) 166 | || (r._origin.y > y1) || (r._origin.z > z1) 167 | || (r._jbyk * z1 - y0 + r._c_zy < 0) 168 | || (r._kbyj * y1 - z0 + r._c_yz < 0) 169 | ) 170 | return false; 171 | 172 | return true; 173 | 174 | case MOM: 175 | 176 | if ((r._origin.y < y0) || (r._origin.y > y1) 177 | || (r._origin.x < x0) || (r._origin.z < z0) 178 | || (r._kbyi * x0 - z1 + r._c_xz > 0) 179 | || (r._ibyk * z0 - x1 + r._c_zx > 0) 180 | ) 181 | return false; 182 | 183 | return true; 184 | 185 | case MOP: 186 | 187 | if ((r._origin.y < y0) || (r._origin.y > y1) 188 | || (r._origin.x < x0) || (r._origin.z > z1) 189 | || (r._kbyi * x0 - z0 + r._c_xz < 0) 190 | || (r._ibyk * z1 - x1 + r._c_zx > 0) 191 | ) 192 | return false; 193 | 194 | return true; 195 | 196 | case POM: 197 | 198 | if ((r._origin.y < y0) || (r._origin.y > y1) 199 | || (r._origin.x > x1) || (r._origin.z < z0) 200 | || (r._kbyi * x1 - z1 + r._c_xz > 0) 201 | || (r._ibyk * z0 - x0 + r._c_zx < 0) 202 | ) 203 | return false; 204 | 205 | return true; 206 | 207 | case POP: 208 | 209 | if ((r._origin.y < y0) || (r._origin.y > y1) 210 | || (r._origin.x > x1) || (r._origin.z > z1) 211 | || (r._kbyi * x1 - z0 + r._c_xz < 0) 212 | || (r._ibyk * z1 - x0 + r._c_zx < 0) 213 | ) 214 | return false; 215 | 216 | return true; 217 | 218 | case MMO: 219 | 220 | if ((r._origin.z < z0) || (r._origin.z > z1) 221 | || (r._origin.x < x0) || (r._origin.y < y0) 222 | || (r._jbyi * x0 - y1 + r._c_xy > 0) 223 | || (r._ibyj * y0 - x1 + r._c_yx > 0) 224 | ) 225 | return false; 226 | 227 | return true; 228 | 229 | case MPO: 230 | 231 | if ((r._origin.z < z0) || (r._origin.z > z1) 232 | || (r._origin.x < x0) || (r._origin.y > y1) 233 | || (r._jbyi * x0 - y0 + r._c_xy < 0) 234 | || (r._ibyj * y1 - x1 + r._c_yx > 0) 235 | ) 236 | return false; 237 | 238 | return true; 239 | 240 | case PMO: 241 | 242 | if ((r._origin.z < z0) || (r._origin.z > z1) 243 | || (r._origin.x > x1) || (r._origin.y < y0) 244 | || (r._jbyi * x1 - y1 + r._c_xy > 0) 245 | || (r._ibyj * y0 - x0 + r._c_yx < 0) 246 | ) 247 | return false; 248 | 249 | return true; 250 | 251 | case PPO: 252 | 253 | if ((r._origin.z < z0) || (r._origin.z > z1) 254 | || (r._origin.x > x1) || (r._origin.y > y1) 255 | || (r._jbyi * x1 - y0 + r._c_xy < 0) 256 | || (r._ibyj * y1 - x0 + r._c_yx < 0) 257 | ) 258 | return false; 259 | 260 | return true; 261 | 262 | case MOO: 263 | 264 | if ((r._origin.x < x0) 265 | || (r._origin.y < y0) || (r._origin.y > y1) 266 | || (r._origin.z < z0) || (r._origin.z > z1) 267 | ) 268 | return false; 269 | 270 | return true; 271 | 272 | case POO: 273 | 274 | if ((r._origin.x > x1) 275 | || (r._origin.y < y0) || (r._origin.y > y1) 276 | || (r._origin.z < z0) || (r._origin.z > z1) 277 | ) 278 | return false; 279 | 280 | return true; 281 | 282 | case OMO: 283 | 284 | if ((r._origin.y < y0) 285 | || (r._origin.x < x0) || (r._origin.x > x1) 286 | || (r._origin.z < z0) || (r._origin.z > z1) 287 | ) 288 | return false; 289 | 290 | return true; 291 | 292 | case OPO: 293 | 294 | if ((r._origin.y > y1) 295 | || (r._origin.x < x0) || (r._origin.x > x1) 296 | || (r._origin.z < z0) || (r._origin.z > z1) 297 | ) 298 | return false; 299 | 300 | return true; 301 | 302 | case OOM: 303 | 304 | if ((r._origin.z < z0) 305 | || (r._origin.x < x0) || (r._origin.x > x1) 306 | || (r._origin.y < y0) || (r._origin.y > y1) 307 | ) 308 | return false; 309 | 310 | return true; 311 | 312 | case OOP: 313 | 314 | if ((r._origin.z > z1) 315 | || (r._origin.x < x0) || (r._origin.x > x1) 316 | || (r._origin.y < y0) || (r._origin.y > y1) 317 | ) 318 | return false; 319 | 320 | return true; 321 | 322 | } 323 | 324 | return false; 325 | } 326 | 327 | #endif /* INTERSECTRAYAABB_H */ 328 | 329 | -------------------------------------------------------------------------------- /3D/IntersectRayTriangle.h: -------------------------------------------------------------------------------- 1 | /* 2 | * File: IntersectRayTriangle.h 3 | * Author: jeferson 4 | * 5 | * Created on 28 de Novembro de 2013, 16:48 6 | */ 7 | 8 | #ifndef INTERSECTRAYTRIANGLE_H 9 | #define INTERSECTRAYTRIANGLE_H 10 | 11 | #include "Ray.h" 12 | 13 | 14 | 15 | /** 16 | * 17 | * @param r 18 | * @param x1 19 | * @param y1 20 | * @param z1 21 | * @param x2 22 | * @param y2 23 | * @param z2 24 | * @param x3 25 | * @param y3 26 | * @param z3 27 | * @return 0 - nao intercepte, 1 caso intercepte, 2 caso intercepte a borda 28 | */ 29 | short int intersectRayTriangle( const Ray& r, float x1, float y1, float z1, 30 | float x2, float y2, float z2, float x3, float y3, float z3 ) 31 | { 32 | double eps = 10E-8; 33 | 34 | //vertices do triangulo 35 | Point3D A( x1, y1, z1 ); 36 | Point3D B( x2, y2, z2 ); 37 | Point3D C( x3, y3, z3 ); 38 | 39 | Vector3D normal = (B - A) % (C - A); 40 | double d = -A * normal; 41 | 42 | double den = normal * r._direction; 43 | if (fabs( den ) < eps) 44 | { 45 | return 0; 46 | } 47 | double t = -(normal * r._origin + d) / (den); 48 | if (t < 0) 49 | { 50 | return 0; 51 | } 52 | 53 | Point3D intersection = r._origin + t * r._direction; 54 | 55 | Vector3D v1 = (B - intersection) % (C - intersection); 56 | Vector3D v2 = (intersection - A) % (C - A); 57 | Vector3D v3 = (B - A) % (intersection - A); 58 | 59 | normal.normalize( ); 60 | 61 | //verifica se o ponto esta dentro do triangulo 62 | double l1 = normal * (v1); 63 | double l2 = normal * (v2); 64 | double l3 = normal * (v3); 65 | 66 | if (l1 > eps && l2 > eps && l3 > eps) 67 | { 68 | return 1; 69 | } else if (fabs( l1 ) <= eps || fabs( l2 ) <= eps || fabs( l3 ) <= eps) 70 | { 71 | return 2; 72 | } else 73 | { 74 | return 0; 75 | } 76 | 77 | } 78 | 79 | 80 | #endif /* INTERSECTRAYTRIANGLE_H */ 81 | 82 | -------------------------------------------------------------------------------- /3D/KdTree.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * File: KdTree.cpp 3 | * Author: paulaceccon 4 | * 5 | * Created on 10 de Novembro de 2013, 11:11 6 | */ 7 | 8 | #include "KdTree.h" 9 | #include 10 | #include 11 | 12 | 13 | 14 | bool compX( const Triangle &t1, const Triangle &t2 ) 15 | { 16 | return t1._centroidX < t2._centroidX; 17 | } 18 | 19 | 20 | 21 | bool compY( const Triangle &t1, const Triangle &t2 ) 22 | { 23 | return t1._centroidY < t2._centroidY; 24 | } 25 | 26 | 27 | 28 | bool compZ( const Triangle &t1, const Triangle &t2 ) 29 | { 30 | return t1._centroidZ < t2._centroidZ; 31 | } 32 | 33 | 34 | 35 | float findMax( float v1, float v2, float v3 ) 36 | { 37 | return std::max( std::max( v1, v2 ), v3 ); 38 | } 39 | 40 | 41 | 42 | float findMin( float v1, float v2, float v3 ) 43 | { 44 | return std::min( std::min( v1, v2 ), v3 ); 45 | } 46 | 47 | 48 | 49 | KdTree::KdTree( float * coordinates, int * triangleList, int numTriangles, int numberCoordinatesPerVertex ) 50 | { 51 | _numberCoordinatesPerVertex = numberCoordinatesPerVertex; 52 | Triangle* triangles = BuildTriangles( coordinates, triangleList, numTriangles ); 53 | _root = buildKdTree( triangles, coordinates, triangleList, 0, numTriangles, 0 ); 54 | delete [] triangles; 55 | } 56 | 57 | 58 | 59 | Triangle* KdTree::BuildTriangles( float * coordinates, int * triangleList, int numTriangles ) 60 | { 61 | Triangle* triangles = new Triangle[numTriangles]; 62 | for (int i = 0; i < numTriangles; i++) 63 | { 64 | Triangle t; 65 | t._triangleIndex = i; 66 | int index1 = triangleList[i * 3 + 0]; 67 | int index2 = triangleList[i * 3 + 1]; 68 | int index3 = triangleList[i * 3 + 2]; 69 | 70 | t._centroidX = (coordinates[index1 * _numberCoordinatesPerVertex + 0] + coordinates[index2 * _numberCoordinatesPerVertex + 0] + coordinates[index3 * _numberCoordinatesPerVertex + 0]) / 3; 71 | t._centroidY = (coordinates[index1 * _numberCoordinatesPerVertex + 1] + coordinates[index2 * _numberCoordinatesPerVertex + 1] + coordinates[index3 * _numberCoordinatesPerVertex + 1]) / 3; 72 | t._centroidZ = 0.0; 73 | 74 | if (_numberCoordinatesPerVertex == 3) 75 | { 76 | t._centroidZ = (coordinates[index1 * _numberCoordinatesPerVertex + 2] + coordinates[index2 * _numberCoordinatesPerVertex + 2] + coordinates[index3 * _numberCoordinatesPerVertex + 2]) / 3; 77 | } 78 | triangles[i] = t; 79 | } 80 | return triangles; 81 | } 82 | 83 | 84 | 85 | Node * KdTree::buildKdTree( Triangle *triangles, float * coordinates, int * triangleList, 86 | int begin, int end, int depth ) 87 | { 88 | if (end - begin <= 0) 89 | return 0; 90 | 91 | int axis = depth % _numberCoordinatesPerVertex; 92 | 93 | // Sorting by axis to insert 94 | if (axis == 0) 95 | { 96 | std::sort( triangles + begin, triangles + end, compX ); 97 | } 98 | else if (axis == 1) 99 | { 100 | std::sort( triangles + begin, triangles + end, compY ); 101 | } 102 | else if (axis == 2) 103 | { 104 | std::sort( triangles + begin, triangles + end, compZ ); 105 | } 106 | else 107 | { 108 | printf( "Axis Error" ); 109 | } 110 | 111 | int half = (begin + end) / 2; 112 | int triangleIndex = triangles[half]._triangleIndex; 113 | 114 | // Getting the vertices from a triangle 115 | int index1 = triangleList[triangleIndex * 3 + 0]; 116 | int index2 = triangleList[triangleIndex * 3 + 1]; 117 | int index3 = triangleList[triangleIndex * 3 + 2]; 118 | 119 | // Getting it vertices coordinates 120 | float x1 = coordinates[index1 * _numberCoordinatesPerVertex]; 121 | float x2 = coordinates[index2 * _numberCoordinatesPerVertex]; 122 | float x3 = coordinates[index3 * _numberCoordinatesPerVertex]; 123 | 124 | float y1 = coordinates[index1 * _numberCoordinatesPerVertex + 1]; 125 | float y2 = coordinates[index2 * _numberCoordinatesPerVertex + 1]; 126 | float y3 = coordinates[index3 * _numberCoordinatesPerVertex + 1]; 127 | 128 | float z1 = 0.0; 129 | float z2 = 0.0; 130 | float z3 = 0.0; 131 | 132 | if (_numberCoordinatesPerVertex == 3) 133 | { 134 | z1 = coordinates[index1 * _numberCoordinatesPerVertex + 2]; 135 | z2 = coordinates[index2 * _numberCoordinatesPerVertex + 2]; 136 | z3 = coordinates[index3 * _numberCoordinatesPerVertex + 2]; 137 | } 138 | 139 | // Finding it bounding box 140 | float xMin = findMin( x1, x2, x3 ); 141 | float xMax = findMax( x1, x2, x3 ); 142 | 143 | float yMin = findMin( y1, y2, y3 ); 144 | float yMax = findMax( y1, y2, y3 ); 145 | 146 | float zMin = findMin( z1, z2, z3 ); 147 | float zMax = findMax( z1, z2, z3 ); 148 | 149 | // Creating a node for this triangle 150 | Node * median = new Node( triangleIndex, xMin, yMin, zMin, xMax, yMax, zMax ); 151 | median->left = buildKdTree( triangles, coordinates, triangleList, begin, half, depth + 1 ); 152 | median->right = buildKdTree( triangles, coordinates, triangleList, half + 1, end, depth + 1 ); 153 | 154 | updateBoundingBox( median ); 155 | 156 | return median; 157 | } 158 | 159 | 160 | 161 | void KdTree::updateBoundingBox( Node * node ) 162 | { 163 | // Merging its bounding box with its children's one 164 | if (node->left != 0 && node->right != 0) 165 | { 166 | float childrenXMin = findMin( node->minX, node->left->minX, node->right->minX ); 167 | float childrenYMin = findMin( node->minY, node->left->minY, node->right->minY ); 168 | float childrenZMin = findMin( node->minZ, node->left->minZ, node->right->minZ ); 169 | 170 | float childrenXMax = findMax( node->maxX, node->left->maxX, node->right->maxX ); 171 | float childrenYMax = findMax( node->maxY, node->left->maxY, node->right->maxY ); 172 | float childrenZMax = findMax( node->maxZ, node->left->maxZ, node->right->maxZ ); 173 | 174 | // Updating it bounding box 175 | node->minX = childrenXMin; 176 | node->minY = childrenYMin; 177 | node->minZ = childrenZMin; 178 | 179 | node->maxX = childrenXMax; 180 | node->maxY = childrenYMax; 181 | node->maxZ = childrenZMax; 182 | } 183 | else 184 | { 185 | if (node->left != 0) 186 | { 187 | float childrenXMin = std::min( node->minX, node->left->minX ); 188 | float childrenYMin = std::min( node->minY, node->left->minY ); 189 | float childrenZMin = std::min( node->minZ, node->left->minZ ); 190 | 191 | float childrenXMax = std::max( node->maxX, node->left->maxX ); 192 | float childrenYMax = std::max( node->maxY, node->left->maxY ); 193 | float childrenZMax = std::max( node->maxZ, node->left->maxZ ); 194 | 195 | // Updating it bounding box 196 | node->minX = childrenXMin; 197 | node->minY = childrenYMin; 198 | node->minZ = childrenZMin; 199 | 200 | node->maxX = childrenXMax; 201 | node->maxY = childrenYMax; 202 | node->maxZ = childrenZMax; 203 | } 204 | else if (node->right != 0) 205 | { 206 | float childrenXMin = std::min( node->minX, node->right->minX ); 207 | float childrenYMin = std::min( node->minY, node->right->minY ); 208 | float childrenZMin = std::min( node->minZ, node->right->minZ ); 209 | 210 | float childrenXMax = std::max( node->maxX, node->right->maxX ); 211 | float childrenYMax = std::max( node->maxY, node->right->maxY ); 212 | float childrenZMax = std::max( node->maxZ, node->right->maxZ ); 213 | 214 | // Updating it bounding box 215 | node->minX = childrenXMin; 216 | node->minY = childrenYMin; 217 | node->minZ = childrenZMin; 218 | 219 | node->maxX = childrenXMax; 220 | node->maxY = childrenYMax; 221 | node->maxZ = childrenZMax; 222 | } 223 | } 224 | } 225 | 226 | 227 | 228 | KdTree::~KdTree( ) 229 | { 230 | deleteKdTree( _root ); 231 | } 232 | 233 | 234 | 235 | void KdTree::deleteKdTree( Node* node ) 236 | { 237 | if (node == 0) 238 | { 239 | return; 240 | } 241 | deleteKdTree( node->left ); 242 | deleteKdTree( node->right ); 243 | delete node; 244 | } 245 | 246 | 247 | 248 | const Node* KdTree::getRoot( ) 249 | { 250 | return _root; 251 | } -------------------------------------------------------------------------------- /3D/KdTree.h: -------------------------------------------------------------------------------- 1 | /* 2 | * File: KdTree.h 3 | * Author: paulaceccon 4 | * 5 | * Created on 10 de Novembro de 2013, 11:11 6 | */ 7 | 8 | #ifndef KDTREE_H 9 | #define KDTREE_H 10 | 11 | #include 12 | #include "Node.h" 13 | 14 | struct Triangle 15 | { 16 | int _triangleIndex; 17 | float _centroidX; 18 | float _centroidY; 19 | float _centroidZ; 20 | }; 21 | 22 | 23 | 24 | class KdTree 25 | { 26 | public: 27 | Triangle* BuildTriangles( float * coordinates, int * triangleList, int numTriangles ); 28 | KdTree( float * coordinates, int * triangleList, int numTriangles, int rSpace ); 29 | void PrintKdTree( Node * root ); 30 | virtual ~KdTree( ); 31 | const Node* getRoot(); 32 | private: 33 | 34 | Node * _root; 35 | int _numberCoordinatesPerVertex; 36 | 37 | void deleteKdTree( Node* node ); 38 | void updateBoundingBox( Node * node ); 39 | Node * buildKdTree( Triangle *triangles, float * coordinates, int * triangleList, 40 | int begin, int end, int depth); 41 | }; 42 | 43 | #endif /* KDTREE_H */ 44 | 45 | -------------------------------------------------------------------------------- /3D/Makefile: -------------------------------------------------------------------------------- 1 | # 2 | # There exist several targets which are by default empty and which can be 3 | # used for execution of your targets. These targets are usually executed 4 | # before and after some main targets. They are: 5 | # 6 | # .build-pre: called before 'build' target 7 | # .build-post: called after 'build' target 8 | # .clean-pre: called before 'clean' target 9 | # .clean-post: called after 'clean' target 10 | # .clobber-pre: called before 'clobber' target 11 | # .clobber-post: called after 'clobber' target 12 | # .all-pre: called before 'all' target 13 | # .all-post: called after 'all' target 14 | # .help-pre: called before 'help' target 15 | # .help-post: called after 'help' target 16 | # 17 | # Targets beginning with '.' are not intended to be called on their own. 18 | # 19 | # Main targets can be executed directly, and they are: 20 | # 21 | # build build a specific configuration 22 | # clean remove built files from a configuration 23 | # clobber remove all built files 24 | # all build all configurations 25 | # help print help mesage 26 | # 27 | # Targets .build-impl, .clean-impl, .clobber-impl, .all-impl, and 28 | # .help-impl are implemented in nbproject/makefile-impl.mk. 29 | # 30 | # Available make variables: 31 | # 32 | # CND_BASEDIR base directory for relative paths 33 | # CND_DISTDIR default top distribution directory (build artifacts) 34 | # CND_BUILDDIR default top build directory (object files, ...) 35 | # CONF name of current configuration 36 | # CND_PLATFORM_${CONF} platform name (current configuration) 37 | # CND_ARTIFACT_DIR_${CONF} directory of build artifact (current configuration) 38 | # CND_ARTIFACT_NAME_${CONF} name of build artifact (current configuration) 39 | # CND_ARTIFACT_PATH_${CONF} path to build artifact (current configuration) 40 | # CND_PACKAGE_DIR_${CONF} directory of package (current configuration) 41 | # CND_PACKAGE_NAME_${CONF} name of package (current configuration) 42 | # CND_PACKAGE_PATH_${CONF} path to package (current configuration) 43 | # 44 | # NOCDDL 45 | 46 | 47 | # Environment 48 | MKDIR=mkdir 49 | CP=cp 50 | CCADMIN=CCadmin 51 | 52 | 53 | # build 54 | build: .build-post 55 | 56 | .build-pre: 57 | # Add your pre 'build' code here... 58 | 59 | .build-post: .build-impl 60 | # Add your post 'build' code here... 61 | 62 | 63 | # clean 64 | clean: .clean-post 65 | 66 | .clean-pre: 67 | # Add your pre 'clean' code here... 68 | 69 | .clean-post: .clean-impl 70 | # Add your post 'clean' code here... 71 | 72 | 73 | # clobber 74 | clobber: .clobber-post 75 | 76 | .clobber-pre: 77 | # Add your pre 'clobber' code here... 78 | 79 | .clobber-post: .clobber-impl 80 | # Add your post 'clobber' code here... 81 | 82 | 83 | # all 84 | all: .all-post 85 | 86 | .all-pre: 87 | # Add your pre 'all' code here... 88 | 89 | .all-post: .all-impl 90 | # Add your post 'all' code here... 91 | 92 | 93 | # build tests 94 | build-tests: .build-tests-post 95 | 96 | .build-tests-pre: 97 | # Add your pre 'build-tests' code here... 98 | 99 | .build-tests-post: .build-tests-impl 100 | # Add your post 'build-tests' code here... 101 | 102 | 103 | # run tests 104 | test: .test-post 105 | 106 | .test-pre: 107 | # Add your pre 'test' code here... 108 | 109 | .test-post: .test-impl 110 | # Add your post 'test' code here... 111 | 112 | 113 | # help 114 | help: .help-post 115 | 116 | .help-pre: 117 | # Add your pre 'help' code here... 118 | 119 | .help-post: .help-impl 120 | # Add your post 'help' code here... 121 | 122 | 123 | 124 | # include project implementation makefile 125 | include nbproject/Makefile-impl.mk 126 | 127 | # include project make variables 128 | include nbproject/Makefile-variables.mk 129 | -------------------------------------------------------------------------------- /3D/Node.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * File: Node.cpp 3 | * Author: paulaceccon 4 | * 5 | * Created on 10 de Novembro de 2013, 10:46 6 | */ 7 | 8 | #include "Node.h" 9 | 10 | 11 | 12 | Node::Node( long int triangleIndex, float minX, float minY, float minZ, 13 | float maxX, float maxY, float maxZ ) 14 | { 15 | this->triangleIndex = triangleIndex; 16 | this->left = 0; 17 | this->right = 0; 18 | this->minX = minX; 19 | this->minY = minY; 20 | this->maxX = maxX; 21 | this->maxY = maxY; 22 | this->minZ = minZ; 23 | this->maxZ = maxZ; 24 | } 25 | 26 | 27 | 28 | Node::~Node( ) 29 | { 30 | } 31 | 32 | -------------------------------------------------------------------------------- /3D/Node.h: -------------------------------------------------------------------------------- 1 | /* 2 | * File: Node.h 3 | * Author: paulaceccon 4 | * 5 | * Created on 10 de Novembro de 2013, 10:46 6 | */ 7 | 8 | #ifndef NODE_H 9 | #define NODE_H 10 | 11 | class Node 12 | { 13 | public: 14 | Node * left, * right; 15 | long int triangleIndex; 16 | float minX, minY, maxX, maxY, minZ, maxZ; 17 | Node( long int triangleIndex, float minX, float minY, float minZ, float maxX, float maxY, float maxZ ); 18 | virtual ~Node( ); 19 | private: 20 | 21 | }; 22 | 23 | #endif /* NODE_H */ 24 | 25 | -------------------------------------------------------------------------------- /3D/Point2D.h: -------------------------------------------------------------------------------- 1 | /* 2 | * File: Point2D.h 3 | * Author: jeferson 4 | * 5 | * Created on 19 de Outubro de 2013, 11:04 6 | */ 7 | 8 | #ifndef POINT2D_H 9 | #define POINT2D_H 10 | 11 | #include "Vector2D.h" 12 | 13 | 14 | typedef Vector2D Point2D; 15 | 16 | 17 | #endif /* POINT2D_H */ 18 | 19 | -------------------------------------------------------------------------------- /3D/PointInPolygon.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * File: PointInPolygon.cpp 3 | * Author: jeferson 4 | * 5 | * Created on 19 de Outubro de 2013, 11:05 6 | */ 7 | 8 | #include "PointInPolygon.h" 9 | 10 | 11 | 12 | bool equal( double val1, double val2, double tol = 10E-5 ) 13 | { 14 | if (fabs( val1 - val2 ) < tol) 15 | return true; 16 | return false; 17 | } 18 | 19 | 20 | 21 | PointInPolygon::PointInPolygon( Point2D* polygon, int numberPoints ) 22 | { 23 | _polygon = polygon; 24 | _numberPoints = numberPoints; 25 | } 26 | 27 | 28 | 29 | PointInPolygon::~PointInPolygon( ) 30 | { 31 | } 32 | 33 | 34 | 35 | short PointInPolygon::rayCrossings( const Point2D& p ) 36 | { 37 | _testPoint = p; 38 | int cross = 0; 39 | for (int i = 0; i < _numberPoints; i++) 40 | { 41 | int next = (i + 1) % _numberPoints; 42 | if (!equal( _polygon[i].y, _polygon[next].y, Point2D::eps )) 43 | { 44 | Point2D intersection; 45 | if (computeIntersectionRay( _polygon[i], _polygon[next], intersection )) 46 | { 47 | if (equal( _testPoint.x, intersection.x, Point2D::eps )) 48 | { 49 | return 0; 50 | } 51 | else 52 | { 53 | if (intersection.x > _testPoint.x && intersection.y > std::min( _polygon[i].y, _polygon[next].y )) 54 | { 55 | cross++; 56 | } 57 | } 58 | } 59 | } 60 | else 61 | { 62 | if (intersectEdge( _polygon[i], _polygon[next] )) 63 | { 64 | return 0; 65 | } 66 | } 67 | } 68 | if (cross % 2 == 1) 69 | { 70 | return -1; 71 | } 72 | return 1; 73 | } 74 | 75 | 76 | 77 | short PointInPolygon::rotationIndex( const Point2D& p ) 78 | { 79 | _testPoint = p; 80 | double a = 0.0; 81 | for (int i = 0; i < _numberPoints; i++) 82 | { 83 | int next = (i + 1) % _numberPoints; 84 | if (intersectEdge( _polygon[i], _polygon[next] )) 85 | { 86 | return 0; 87 | } 88 | a += angle( _polygon[i], p, _polygon[next] ); 89 | } 90 | if (equal( a, 0.0 )) 91 | return 1; 92 | return -1; 93 | } 94 | 95 | 96 | 97 | bool PointInPolygon::intersectEdge( const Point2D& p1, const Point2D& p2 ) 98 | { 99 | //para está sobre a aresta o roduto vetorial deve ser zero e o produto escalar 100 | //deve ser menor ou igual a zero. 101 | Vector2D v1 = p1 - _testPoint; 102 | Vector2D v2 = p2 - _testPoint; 103 | return equal( v1 % v2, 0.0, Point2D::eps ) && v1 * v2 <= 0.0; 104 | } 105 | 106 | 107 | 108 | bool PointInPolygon::computeIntersectionRay( const Point2D& p1, const Point2D& p2, Point2D& intersection ) 109 | { 110 | //calcula o parametro da interpolacao linear que gera o ponto 111 | //de intersecao do segmento 112 | double s = (_testPoint.y - p1.y) / (p2.y - p1.y); 113 | 114 | //verifica se a intersecao esta dentro do segmento 115 | if (s >= 0.0 && s <= 1.0) 116 | { 117 | intersection = (1 - s) * p1 + s * p2; 118 | return true; 119 | } 120 | return false; 121 | } 122 | 123 | 124 | 125 | double PointInPolygon::angle( const Point2D& p, const Point2D& q, const Point2D& r ) 126 | { 127 | Vector2D u = p - q; 128 | Vector2D v = r - q; 129 | // return atan2( u % v, u * v ); 130 | double ang1 = pseudoAngle( u ); 131 | double ang2 = pseudoAngle( v ); 132 | double ang = fabs( ang1 - ang2 ); 133 | ang = ang > 4.0 ? 8 - ang : ang; 134 | int sinal = u % v > 0 ? 1 : -1; 135 | return sinal * ang; 136 | } 137 | 138 | 139 | 140 | double PointInPolygon::pseudoAngle( const Vector2D& v ) 141 | { 142 | if (v.y >= 0) 143 | { 144 | if (v.x >= 0) 145 | { 146 | if (v.x >= v.y) 147 | { 148 | return ( v.y / v.x); 149 | } 150 | else 151 | { 152 | return ( 2 - v.x / v.y); 153 | } 154 | } 155 | else 156 | { 157 | if (v.x >= -v.y) 158 | { 159 | return ( 2 - v.x / v.y); 160 | } 161 | else 162 | { 163 | return ( 4 + v.y / v.x); 164 | } 165 | } 166 | } 167 | else 168 | { 169 | if (v.x >= 0) 170 | { 171 | if (v.x >= -v.y) 172 | { 173 | return ( v.y / v.x); 174 | } 175 | else 176 | { 177 | return ( -2 - v.x / v.y); 178 | } 179 | } 180 | else 181 | { 182 | if (v.x >= v.y) 183 | { 184 | return ( -2 - v.x / v.y); 185 | } 186 | else 187 | { 188 | return ( -4 + v.y / v.x); 189 | } 190 | } 191 | } 192 | } -------------------------------------------------------------------------------- /3D/PointInPolygon.h: -------------------------------------------------------------------------------- 1 | /* 2 | * File: PointInPolygon.h 3 | * Author: jeferson 4 | * 5 | * Created on 19 de Outubro de 2013, 11:05 6 | */ 7 | 8 | #ifndef POINTINPOLYGON_H 9 | #define POINTINPOLYGON_H 10 | 11 | #include "Point2D.h" 12 | 13 | class PointInPolygon 14 | { 15 | public: 16 | /** 17 | * Construtor que recebe a lista de pontos de poligono 18 | * @param polygon - lista de pontos do poligono 19 | * @param numberPoints - numero de pontos no poligono 20 | */ 21 | PointInPolygon ( Point2D* polygon, int numberPoints ); 22 | 23 | /** 24 | * Verifica se um ponto p esta dentro (-1), na fronteira (0) ou fora (1) de um 25 | * poligono. Essa implementacao usa o algoritmo de tracado de raios, onde 26 | * um raio eh tracado a partir do ponto p para o infinito e numero de 27 | * intersecoes com o poligono é contada. Se der impar está dentro, 28 | * se der par está fora. 29 | * @param p - ponto p a ser testado contra o poligono 30 | * @return -1 se o ponto estive dentro do poligono, 0 se estiver na fronteira 31 | * e 1 para o caso do ponto estiver fora do polígono. 32 | */ 33 | short rayCrossings ( const Point2D& p ); 34 | 35 | /** 36 | * Verifica se um ponto p esta dentro (-1), na fronteira (0) ou fora (1) de um 37 | * poligono. Essa implementacao usa indice de rotacao, onde o angulo do ponto 38 | * em relacao a cada segmento de reta é somado com sinal, assim se o ponto 39 | * estiver dentro o valor do angulo eh 2 PI, caso coontrario o valor do angulo 40 | * total eh zero 41 | * @param p - ponto p a ser testado contra o poligono 42 | * @return -1 se o ponto estive dentro do poligono, 0 se estiver na fronteira 43 | * e 1 para o caso do ponto estiver fora do polígono. 44 | */ 45 | short rotationIndex ( const Point2D& p ); 46 | 47 | /** 48 | * Destrutor 49 | */ 50 | virtual ~PointInPolygon ( ); 51 | private: 52 | 53 | /** 54 | * Lista de ponto que definem o poligono 55 | */ 56 | Point2D* _polygon; 57 | 58 | /** 59 | * Numero de pontos do poligono 60 | */ 61 | int _numberPoints; 62 | 63 | /** 64 | * Ponto a ser testado 65 | */ 66 | Point2D _testPoint; 67 | 68 | /** 69 | * Verifica se o ponto p esta sobre a aresta p1p2. 70 | * @param p1 - ponto da aresta 71 | * @param p2 - ponto da aresta 72 | * @return - true se estiver sobre a aresta e false caso contrario 73 | */ 74 | bool intersectEdge ( const Point2D& p1, const Point2D& p2); 75 | 76 | /** 77 | * Calcula a intersecao do segmento p1p2 com a reta (x, y) + (1, 0)t 78 | * @param p1 - ponto do segmento 79 | * @param p2 - ponto do segmento 80 | * @param intersection - ponto de intersecao, se existir, calculado 81 | * @return - true se existir intersecao e false caso contrario 82 | */ 83 | bool computeIntersectionRay ( const Point2D& p1, const Point2D& p2, Point2D& intersection ); 84 | 85 | /** 86 | * Calcula o angulo orientado dos vetores pq rq 87 | * @return - angulo orientador 88 | */ 89 | double angle( const Point2D& p, const Point2D& q, const Point2D& r); 90 | 91 | double pseudoAngle( const Vector2D& v); 92 | }; 93 | 94 | #endif /* POINTINPOLYGON_H */ 95 | 96 | -------------------------------------------------------------------------------- /3D/Ray.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * File: Ray.cpp 3 | * Author: jeferson 4 | * 5 | * Created on 28 de Novembro de 2013, 15:00 6 | */ 7 | 8 | #include "Ray.h" 9 | 10 | 11 | 12 | Ray::Ray( const Point3D& origin, const Vector3D& direction ) 13 | { 14 | //common variables 15 | _origin = origin; 16 | _direction = direction; 17 | 18 | _inverseDirection = 1.0f / _direction; 19 | 20 | //ray slope 21 | _ibyj = _direction.x * _inverseDirection.y; 22 | _jbyi = _direction.y * _inverseDirection.x; 23 | _jbyk = _direction.y * _inverseDirection.z; 24 | _kbyj = _direction.z * _inverseDirection.y; 25 | _ibyk = _direction.x * _inverseDirection.z; 26 | _kbyi = _direction.z * _inverseDirection.x; 27 | 28 | _c_xy = _origin.y - _jbyi * _origin.x; 29 | _c_xz = _origin.z - _kbyi * _origin.x; 30 | _c_yx = _origin.x - _ibyj * _origin.y; 31 | _c_yz = _origin.z - _kbyj * _origin.y; 32 | _c_zx = _origin.x - _ibyk * _origin.z; 33 | _c_zy = _origin.y - _jbyk * _origin.z; 34 | 35 | 36 | //ray slope classification 37 | if (_direction.x < 0) 38 | { 39 | if (_direction.y < 0) 40 | { 41 | if (_direction.z < 0) 42 | { 43 | _classification = MMM; 44 | } 45 | else if (_direction.z > 0) 46 | { 47 | _classification = MMP; 48 | } 49 | else//(k >= 0) 50 | { 51 | _classification = MMO; 52 | } 53 | } 54 | else//(j >= 0) 55 | { 56 | if (_direction.z < 0) 57 | { 58 | _classification = MPM; 59 | if (_direction.y == 0) 60 | { 61 | _classification = MOM; 62 | } 63 | } 64 | else//(k >= 0) 65 | { 66 | if ((_direction.y == 0) && (_direction.z == 0)) 67 | { 68 | _classification = MOO; 69 | } 70 | else if (_direction.z == 0) 71 | { 72 | _classification = MPO; 73 | } 74 | else if (_direction.y == 0) 75 | { 76 | _classification = MOP; 77 | } 78 | else 79 | { 80 | _classification = MPP; 81 | } 82 | } 83 | } 84 | } 85 | else//(i >= 0) 86 | { 87 | if (_direction.y < 0) 88 | { 89 | if (_direction.z < 0) 90 | { 91 | _classification = PMM; 92 | if (_direction.x == 0) 93 | { 94 | _classification = OMM; 95 | } 96 | } 97 | else//(k >= 0) 98 | { 99 | if ((_direction.x == 0) && (_direction.z == 0)) 100 | { 101 | _classification = OMO; 102 | } 103 | else if (_direction.z == 0) 104 | { 105 | _classification = PMO; 106 | } 107 | else if (_direction.x == 0) 108 | { 109 | _classification = OMP; 110 | } 111 | else 112 | { 113 | _classification = PMP; 114 | } 115 | } 116 | } 117 | else//(j >= 0) 118 | { 119 | if (_direction.z < 0) 120 | { 121 | if ((_direction.x == 0) && (_direction.y == 0)) 122 | { 123 | _classification = OOM; 124 | } 125 | else if (_direction.x == 0) 126 | { 127 | _classification = OPM; 128 | } 129 | else if (_direction.y == 0) 130 | { 131 | _classification = POM; 132 | } 133 | else 134 | { 135 | _classification = PPM; 136 | } 137 | } 138 | else//(k > 0) 139 | { 140 | if (_direction.x == 0) 141 | { 142 | if (_direction.y == 0) 143 | { 144 | _classification = OOP; 145 | } 146 | else if (_direction.z == 0) 147 | { 148 | _classification = OPO; 149 | } 150 | else 151 | { 152 | _classification = OPP; 153 | } 154 | } 155 | else 156 | { 157 | if ((_direction.y == 0) && (_direction.z == 0)) 158 | { 159 | _classification = POO; 160 | } 161 | else if (_direction.y == 0) 162 | { 163 | _classification = POP; 164 | } 165 | else if (_direction.z == 0) 166 | { 167 | _classification = PPO; 168 | } 169 | else 170 | { 171 | _classification = PPP; 172 | } 173 | } 174 | } 175 | } 176 | } 177 | } 178 | 179 | 180 | 181 | Ray::~Ray( ) 182 | { 183 | } 184 | 185 | -------------------------------------------------------------------------------- /3D/Ray.h: -------------------------------------------------------------------------------- 1 | /* 2 | * File: Ray.h 3 | * Author: jeferson 4 | * 5 | * Created on 28 de Novembro de 2013, 15:00 6 | */ 7 | 8 | #ifndef RAY_H 9 | #define RAY_H 10 | 11 | #include "Vector3D.h" 12 | 13 | enum CLASSIFICATION 14 | { 15 | MMM, MMP, MPM, MPP, PMM, PMP, PPM, PPP, POO, MOO, OPO, OMO, 16 | OOP, OOM, OMM, OMP, OPM, OPP, MOM, MOP, POM, POP, MMO, MPO, 17 | PMO, PPO 18 | }; 19 | 20 | class Ray 21 | { 22 | public: 23 | /** 24 | * Construtor do raio que recebe a origem e direcao do mesmo 25 | * @param origin - origem do raio 26 | * @param direction - direcao 27 | */ 28 | Ray( const Point3D& origin, const Vector3D& direction ); 29 | 30 | /** 31 | * Destrutor do raio 32 | */ 33 | virtual ~Ray( ); 34 | 35 | /** 36 | * Origem do raio 37 | */ 38 | Point3D _origin; 39 | 40 | /** 41 | * Direcao do raio 42 | */ 43 | Vector3D _direction; 44 | 45 | /** 46 | * Direcao inversa das componentes 47 | */ 48 | Point3D _inverseDirection; 49 | 50 | 51 | CLASSIFICATION _classification; 52 | 53 | float _ibyj, _jbyi, _kbyj, _jbyk, _ibyk, _kbyi; //slope 54 | float _c_xy, _c_xz, _c_yx, _c_yz, _c_zx, _c_zy; 55 | 56 | private: 57 | }; 58 | 59 | #endif /* RAY_H */ 60 | 61 | -------------------------------------------------------------------------------- /3D/Render.h: -------------------------------------------------------------------------------- 1 | /* 2 | * File: Render.h 3 | * Author: jeferson 4 | * 5 | * Created on 1 de Agosto de 2012, 21:31 6 | */ 7 | 8 | #ifndef RENDER_H 9 | #define RENDER_H 10 | //modos de visualizacao 11 | 12 | 13 | 14 | /** 15 | * Desenha um gradeado no plano xy para servir como referencia 16 | */ 17 | void renderGridXY( ) 18 | { 19 | glDisable( GL_LIGHTING ); 20 | 21 | glColor3f( 0.75f, 0.75f, 0.438f ); 22 | 23 | glBegin( GL_LINES ); 24 | for (int i = -500; i <= 500; i += 5) 25 | { 26 | glVertex3i( i, 500, 0 ); 27 | glVertex3i( i, -500, 0 ); 28 | } 29 | for (int i = -500; i <= 500; i += 5) 30 | { 31 | glVertex3i( 500, i, 0 ); 32 | glVertex3i( -500, i, 0 ); 33 | } 34 | glEnd( ); 35 | 36 | glEnable( GL_LIGHTING ); 37 | } 38 | 39 | 40 | 41 | /** 42 | * Renderiza a superficie usando shader 43 | */ 44 | 45 | void renderSurfaceWireframe( float *vertices, float *normais, int* triangles, int numTriangles, bool fillSurface ) 46 | { 47 | if (fillSurface) 48 | { 49 | glPolygonMode( GL_FRONT_AND_BACK, GL_FILL ); 50 | } 51 | else 52 | { 53 | glPolygonMode( GL_FRONT_AND_BACK, GL_LINE ); 54 | } 55 | 56 | glPushAttrib( GL_ALL_ATTRIB_BITS ); 57 | glLightModeli( GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE ); 58 | glColorMaterial( GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE ); 59 | glEnable( GL_COLOR_MATERIAL ); 60 | 61 | glEnable( GL_DEPTH_TEST ); 62 | glEnable( GL_NORMALIZE ); 63 | glEnable( GL_LIGHTING ); 64 | 65 | glDisable( GL_BLEND ); 66 | glDisable( GL_TEXTURE_2D ); 67 | glDisable( GL_TEXTURE_1D ); 68 | 69 | glPushClientAttrib( GL_CLIENT_VERTEX_ARRAY_BIT ); 70 | 71 | glEnableClientState( GL_VERTEX_ARRAY ); 72 | glEnableClientState( GL_NORMAL_ARRAY ); 73 | 74 | glColor3f( 1.0f, 0.0f, 0.0f ); 75 | glVertexPointer( 3, GL_FLOAT, 0, vertices ); 76 | glNormalPointer( GL_FLOAT, 0, normais ); 77 | 78 | glDrawElements( GL_TRIANGLES, 3 * numTriangles, GL_UNSIGNED_INT, triangles ); 79 | 80 | glDisableClientState( GL_VERTEX_ARRAY ); 81 | glDisableClientState( GL_NORMAL_ARRAY ); 82 | 83 | glPopAttrib( ); 84 | glPopClientAttrib( ); 85 | 86 | 87 | glPolygonMode( GL_FRONT_AND_BACK, GL_FILL ); 88 | } 89 | 90 | #endif /* RENDER_H */ 91 | 92 | -------------------------------------------------------------------------------- /3D/Screenshots/depth0.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/paulaceccon/KdTree4TriangularMeshes/b7b2bc03fecdd2734e7ee38b152594d6a7a8ba80/3D/Screenshots/depth0.png -------------------------------------------------------------------------------- /3D/Screenshots/depth1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/paulaceccon/KdTree4TriangularMeshes/b7b2bc03fecdd2734e7ee38b152594d6a7a8ba80/3D/Screenshots/depth1.png -------------------------------------------------------------------------------- /3D/Screenshots/depth2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/paulaceccon/KdTree4TriangularMeshes/b7b2bc03fecdd2734e7ee38b152594d6a7a8ba80/3D/Screenshots/depth2.png -------------------------------------------------------------------------------- /3D/Screenshots/depth3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/paulaceccon/KdTree4TriangularMeshes/b7b2bc03fecdd2734e7ee38b152594d6a7a8ba80/3D/Screenshots/depth3.png -------------------------------------------------------------------------------- /3D/Screenshots/depth4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/paulaceccon/KdTree4TriangularMeshes/b7b2bc03fecdd2734e7ee38b152594d6a7a8ba80/3D/Screenshots/depth4.png -------------------------------------------------------------------------------- /3D/Screenshots/depth5.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/paulaceccon/KdTree4TriangularMeshes/b7b2bc03fecdd2734e7ee38b152594d6a7a8ba80/3D/Screenshots/depth5.png -------------------------------------------------------------------------------- /3D/Timer.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Timer.h 3 | * 4 | * Created on: Nov 26, 2013 5 | * Author: jduartejr 6 | */ 7 | 8 | #ifndef TIMER_H_ 9 | #define TIMER_H_ 10 | 11 | class Timer 12 | { 13 | public: 14 | 15 | 16 | 17 | Timer( ) 18 | { 19 | clock_gettime( CLOCK_REALTIME, &_startTime ); 20 | } 21 | 22 | 23 | 24 | inline void restart( ) 25 | { 26 | clock_gettime( CLOCK_REALTIME, &_startTime ); 27 | } 28 | 29 | 30 | 31 | inline double elapsed( ) const 32 | { 33 | timespec endTime; 34 | clock_gettime( CLOCK_REALTIME, &endTime ); 35 | return (endTime.tv_sec - _startTime.tv_sec) + (double) (endTime.tv_nsec - _startTime.tv_nsec) / 1000000000.0; 36 | } 37 | 38 | 39 | 40 | inline void printTime( const char* timerString ) const 41 | { 42 | printf( "%s: %lf\n", timerString, elapsed( ) ); 43 | } 44 | 45 | private: 46 | timespec _startTime; 47 | }; 48 | 49 | #endif /* TIMER_H_ */ -------------------------------------------------------------------------------- /3D/Tipos.h: -------------------------------------------------------------------------------- 1 | 2 | #ifndef _TIPOS_ 3 | #define _TIPOS_ 4 | #define MAX_TRIANGULOS 1 5 | #define TIPO float 6 | #define INF 2000000000 7 | #define PI 3.141592 8 | #include 9 | using namespace std; 10 | 11 | /*--------------------------------------------------------------------------* 12 | Define o tipo camera usado para auxiliar na movimentacao da cena 13 | --------------------------------------------------------------------------*/ 14 | struct Camera 15 | { 16 | double positionX, positionY, positionZ; //posicao da camera 17 | double targetX, targetY, targetZ; //alvo da camera, onde olha 18 | double walkingDirectionX, walkingDirectionY, walkingDirectionZ; //direcao(anda) 19 | double walkingLateralDirectionX, walkingLateralDirectionY, walkingLateralDirectionZ; //direcao(anda) 20 | double velocity, lateralVelocity; 21 | double rotateX, rotateY; 22 | double velocityRotation; 23 | 24 | /*-99.787116, -196.355437, 141.518071 -98.592712, -194.109091, 139.928313 25 | 26 | 0.398134, 0.748782, -0.529919 0.748782, -0.398134, -0.529919 27 | 28 | 28.000000, -32.000000*/ 29 | Camera() { 30 | positionX = -99.787116; 31 | positionY = -196.355437; 32 | positionZ = 141.518071; 33 | targetX = -98.592712; 34 | targetY = -194.109091; 35 | targetZ = 139.928313; 36 | walkingDirectionX = 0.398134; 37 | walkingDirectionY = 0.748782; 38 | walkingDirectionZ = -0.529919; 39 | walkingLateralDirectionX = 0.748782; 40 | walkingLateralDirectionY = -0.398134; 41 | walkingLateralDirectionZ = -0.529919; 42 | velocity = lateralVelocity = 0.0; 43 | rotateX = 28.000000; 44 | rotateY = -32.000000; 45 | velocityRotation = 1; 46 | } 47 | }; 48 | #endif 49 | -------------------------------------------------------------------------------- /3D/Vector2D.h: -------------------------------------------------------------------------------- 1 | /* 2 | * File: Vector.h 3 | * Author: jeferson 4 | * 5 | * Created on 23 de Setembro de 2013, 20:28 6 | */ 7 | 8 | #ifndef VECTOR2D_H 9 | #define VECTOR2D_H 10 | 11 | #include 12 | #include 13 | #include 14 | 15 | template 16 | class Vector2D 17 | { 18 | public: 19 | T x, y; 20 | 21 | /** 22 | * Construtor default 23 | */ 24 | Vector2D ( ) 25 | { 26 | } 27 | 28 | /** 29 | * Construtor com parametros 30 | * @param vx - coordenada x do vetor 31 | * @param vy - coordenada y do vetor 32 | */ 33 | Vector2D ( T vx, T vy ) 34 | : x ( vx ), y ( vy ) 35 | { 36 | } 37 | 38 | /** 39 | * Construtor com parametros 40 | * @param v - vetor de floar com tres posicoes correspondentes as coordenadas 41 | * x e y do vetor 42 | */ 43 | Vector2D ( const T v[2] ) 44 | : x ( v[0] ), y ( v[1] ) 45 | { 46 | } 47 | 48 | /** 49 | * Destrutor 50 | */ 51 | ~Vector2D ( ) 52 | { 53 | } 54 | 55 | /** 56 | * Define novas coordenadas para o vetor 57 | * @param vx - coordenada x do vetor 58 | * @param vy - coordenada y do vetor 59 | */ 60 | void set ( T vx, T vy) 61 | { 62 | x = vx; 63 | y = vy; 64 | } 65 | 66 | /** 67 | * 68 | * Define novas coordenadas para o vetor 69 | * @param v - vetor de floar com tres posicoes correspondentes as coordenadas 70 | * x e y do vetor 71 | */ 72 | void set ( const T v[2] ) 73 | { 74 | x = v[0]; 75 | y = v[1]; 76 | } 77 | 78 | /** 79 | * Realiza o produto escalar entre o vetor corrente e o vetor q. 80 | * @param q - vetor o qual sera realizado produto escalar com o vetor 81 | * corrente 82 | * @return - valor do produto escalar 83 | */ 84 | inline T dot ( const Vector2D& q ) const 85 | { 86 | return x * q.x + y * q.y; 87 | } 88 | 89 | /** 90 | * Realiza produto vetorial entre os vetores a e b (a X b) e escreve 91 | * o resultado no no vetor corrente. 92 | * @param a - vetor a 93 | * @param b - vetor b 94 | */ 95 | inline T cross ( const Vector2D& a, const Vector2D& b ) 96 | { 97 | return a.x * b.y - a.y * b.x; 98 | } 99 | 100 | /** 101 | * Calcula o comprimento ao quadrado do vetor 102 | * @return - comprimento ao quadrado do vetor corrente 103 | */ 104 | inline T sqrLength ( ) const 105 | { 106 | return (x * x + y * y ); 107 | } 108 | 109 | /** 110 | * Calcula o comprimento do vetor 111 | * @return - comprimento do vetor 112 | */ 113 | inline T length ( ) const 114 | { 115 | return (T) sqrt ( x * x + y * y ); 116 | } 117 | 118 | /** 119 | * Normaliza o vetor e retorna a norma que o vetor tinha antes de ser 120 | * normalizado 121 | * @return - norma do vetor antes de ser normalizado 122 | */ 123 | inline T normalize ( ) 124 | { 125 | T l = length ( ); 126 | if (l != (T)0.0) 127 | { 128 | T d = (T)1.0 / l; 129 | x *= d; 130 | y *= d; 131 | } 132 | return l; 133 | } 134 | 135 | /** 136 | * Calculo o angulo entre dois vetores, o corrente e o vetor v passado por 137 | * parametro. 138 | * @param v - vetor v passado por parametro para ser calculado o angulo 139 | * com o vetor corrente 140 | * @return - angulo entre o vetor v e o vetor corrente 141 | */ 142 | inline T angle ( const Vector2D& v ) const 143 | { 144 | return (T) ( acos ( dot ( v ) / ( length ( ) * v.length ( ) ) ) ); 145 | } 146 | 147 | /** 148 | * Imprime o vetor com uma mensagem passada por parametro 149 | * @param label - mensagem impressa antes das coordenadas do vetor 150 | */ 151 | inline void print ( const char* label ) const 152 | { 153 | printf ( "%s = {%g, %g}\n", label, (float)x, (float)y); 154 | } 155 | 156 | /** 157 | * Sobrecarga do operador +=. Atribui ao vetor corrente a soma das suas coordenadas 158 | * com as coordenas de outro vetor passado por parametro. Exemplo de uso: 159 | * a += b. 160 | * @param other- vetor a direita da operacao de += 161 | * @return - retorna uma referencia para o vetor corrente com ja com o resultado 162 | * da operacao 163 | */ 164 | inline Vector2D& operator+= (const Vector2D& other ) 165 | { 166 | x += other.x; 167 | y += other.y; 168 | return *this; 169 | } 170 | 171 | /** 172 | * Sobrecarga do operador +=. Adiciona um vetor escalar as coordenadas do 173 | * vetor. Eh equivalente a somar o vetor corrente pelo vetor 174 | * (scalar, scalar, scalar). 175 | * @param scalar - valor escalar a ser somado 176 | * @return - vetor resultado da soma 177 | */ 178 | inline Vector2D& operator+= (T scalar) 179 | { 180 | x += scalar; 181 | y += scalar; 182 | return *this; 183 | } 184 | 185 | /** 186 | * Sobrecarga do operador -=. Atribui ao vetor corrente a subtracao das suas coordenadas 187 | * pelas coordenas de outro vetor passado por parametro. Exemplo de uso: 188 | * a -= b. 189 | * @param other- vetor a direita da operacao de -= 190 | * @return - retorna uma referencia para o vetor corrente com ja com o resultado 191 | * da operacao. 192 | */ 193 | inline Vector2D& operator-= (const Vector2D& other ) 194 | { 195 | x -= other.x; 196 | y -= other.y; 197 | return *this; 198 | } 199 | 200 | /** 201 | * Sobrecarga do operador -=. Adiciona um vetor escalar as coordenadas do 202 | * vetor. Eh equivalente a subtrair o vetor corrente pelo vetor 203 | * (scalar, scalar, scalar). 204 | * @param scalar - valor escalar a ser subtraido 205 | * @return - vetor resultado da soma 206 | */ 207 | inline Vector2D& operator-= (T scalar) 208 | { 209 | x -= scalar; 210 | y -= scalar; 211 | return *this; 212 | } 213 | 214 | /** 215 | * Sobrecarga do operador unario -. Inverte o sinal de todas as coordenadas 216 | * do vetor corrente e salva em um novo vetor 217 | * @return - retorna novo vetor com as coordenadas com sinal trocado 218 | */ 219 | inline Vector2D operator- ( ) 220 | { 221 | Vector2D v ( -x, -y ); 222 | return v; 223 | } 224 | 225 | /** 226 | * Sobrecarga do operador *= entre vetores. Multiplica coordenada a coordenada 227 | * dois vetores (o corrente e o passado por parametro) e salva o resultado 228 | * no vetor corrente. Exemplo de uso: a *= b. 229 | * @param other - vetor a ser multiplicado, coordenada a coordenada, pelo 230 | * vetor corrente 231 | * @return - retorna referencia para vetor corrente ja com a operacao aplicada 232 | */ 233 | inline Vector2D& operator*= (const Vector2D& other ) 234 | { 235 | x *= other.x; 236 | y *= other.y; 237 | return *this; 238 | } 239 | 240 | /** 241 | * Sobrecarga do operador *= entre vetor e escalar. Multiplica coordenada a 242 | * coordenada o vetor corrente por um escalar. 243 | * @param scalar - escalar a ser multiplicado pelo vetor corrente 244 | * @return - retorna referencia para vetor corrente ja com a operacao aplicada 245 | */ 246 | inline Vector2D& operator*= (T scalar) 247 | { 248 | x *= scalar; 249 | y *= scalar; 250 | return *this; 251 | } 252 | 253 | /** 254 | * Sobrecarga do operador /= . Divide todas as coordenadas do vetor corrente 255 | * por um escalar. 256 | * @param scalar - escalar a ser dividido pelo vetor corrente 257 | * @return - retorna referencia para vetor corrente ja com a operacao aplicada 258 | */ 259 | inline Vector2D& operator/= (T scalar) 260 | { 261 | if (scalar != (T)0.0) 262 | { 263 | x /= scalar; 264 | y /= scalar; 265 | } 266 | return *this; 267 | } 268 | 269 | 270 | inline friend bool operator == (const Vector2D& v1, const Vector2D& v2 ) 271 | { 272 | return ( fabs ( v1.x - v2.x ) < eps ) && ( fabs ( v1.y - v2.y ) < eps ); 273 | } 274 | 275 | inline friend bool operator <(const Vector2D& v1, const Vector2D& v2 ) 276 | { 277 | if (v1.x < v2.x) 278 | return true; 279 | return false; 280 | } 281 | 282 | inline friend bool operator != (const Vector2D& v1, const Vector2D& v2 ) 283 | { 284 | return ( fabs ( v1.x - v2.x ) > eps ) || ( fabs ( v1.y - v2.y ) > eps ); 285 | } 286 | 287 | /** 288 | * Sobrecarga do operador + entre dois vetores: Soma dois vetores e retorna 289 | * resultado da soma 290 | * @param one - primeiro vetor 291 | * @param other - segundo vetor 292 | * @return - vetor com o resultado da soma dos dois vetores 293 | */ 294 | inline friend Vector2D operator+ (const Vector2D& one, const Vector2D& other ) 295 | { 296 | Vector2D res ( one ); 297 | return res += other; 298 | } 299 | 300 | /** 301 | * Sobrecarga do operador + entre vetor e escalar: Soma um valor escalar 302 | * a todas as coordenadas do vetor. Exemplo de usoo: vetor + escalar 303 | * @param one - vetor 304 | * @param scalar - escalar que sera somado a todas as coordenadas do vetor 305 | * @return - vetor com o resultado da soma do vetor pelo escalars 306 | */ 307 | inline friend Vector2D operator+ (const Vector2D& one, T scalar) 308 | { 309 | Vector2D res ( one ); 310 | return res += scalar; 311 | } 312 | 313 | /** 314 | * Sobrecarga do operador + entre vetor e escalar: Soma um valor escalar 315 | * a todas as coordenadas do vetor. Exemplo de usoo: escalar + vetor 316 | * @param scalar - escalar que sera somado a todas as coordenadas do vetor 317 | * @param one - vetor 318 | * @return - vetor com o resultado da soma do vetor pelo escalars 319 | */ 320 | inline friend Vector2D operator+ (T scalar, const Vector2D& one ) 321 | { 322 | Vector2D res ( one ); 323 | return res += scalar; 324 | } 325 | 326 | /** 327 | * Sobrecarga do operador - entre vetores: Subtrai um vetor pelo outro 328 | * @param one - primeiro vetor 329 | * @param other - segundo vetor 330 | * @return - vetor com o resultado da subtracao entre os vetores 331 | */ 332 | inline friend Vector2D operator- (const Vector2D& one, const Vector2D& other ) 333 | { 334 | Vector2D res ( one ); 335 | return res -= other; 336 | } 337 | 338 | /** 339 | * Sobrecarga do operador - entre vetor e escalar: Subtrai um valor escalar 340 | * a todas as coordenadas do vetor. Exemplo de uso: vetor - scalar 341 | * @param one - vetor 342 | * @param scalar - escalar que sera subtraido de todas as coordenadas do vetor 343 | * @return - vetor com o resultado da subtração pelo escalar 344 | */ 345 | inline friend Vector2D operator- (const Vector2D& one, T scalar) 346 | { 347 | Vector2D res ( one ); 348 | return res -= scalar; 349 | } 350 | 351 | /** 352 | * Sobrecarga do operador - entre vetor e escalar: Subtrai um valor escalar 353 | * a todas as coordenadas do vetor. Equivalente a (scalar, scalar, scalar) - 354 | * (v1, v2, v3). Exemplo de uso: scalar - vetor 355 | * @param scalar - escalar que sera subtraido de todas as coordenadas do vetor 356 | * @param one - vetor 357 | * @return - vetor com o resultado da subtração pelo escalar 358 | */ 359 | inline friend Vector2D operator- (T scalar, const Vector2D& one ) 360 | { 361 | Vector2D res ( one ); 362 | res *= -1; 363 | return res += scalar; 364 | } 365 | 366 | /** 367 | * Multiplica dois vetores componente a componente 368 | * @param one - primeiro vetor 369 | * @param other - segundo vetor 370 | * @return - vetor resultante da multiplicacao componente a componente 371 | */ 372 | inline friend T operator* (const Vector2D& one, const Vector2D& other ) 373 | { 374 | return one.x * other.x + one.y * other.y; 375 | } 376 | 377 | /** 378 | * Multiplica todas as componentes de um vetor po um escalar 379 | * @param one - vetor 380 | * @param scalar - escalar 381 | * @return - vetor resultante da multiplicacao do escalar por todas as 382 | * componentes 383 | */ 384 | inline friend Vector2D operator* (const Vector2D& one, T scalar) 385 | { 386 | Vector2D res ( one ); 387 | return res *= scalar; 388 | } 389 | 390 | /** 391 | * Multiplica todas as componentes de um vetor po um escalar 392 | * @param scalar - escalar 393 | * @param one - vetor 394 | * @return - vetor resultante da multiplicacao do escalar por todas as 395 | * componentes 396 | */ 397 | inline friend Vector2D operator* (T scalar, const Vector2D& one ) 398 | { 399 | Vector2D res ( one ); 400 | return res *= scalar; 401 | } 402 | 403 | /** 404 | * Divide todas as componentes de um vetor por escalar 405 | * @param one - vetor 406 | * @param scalar - escalar 407 | * @return vetor resultante da divisao das coordenadas por um escalar 408 | */ 409 | inline friend Vector2D operator/ (const Vector2D& one, T scalar) 410 | { 411 | Vector2D res ( one ); 412 | return res /= scalar; 413 | } 414 | 415 | /** 416 | * Controi um novo vetor onde cada coordenada e resultado da divisao do 417 | * escalar pela coordenada do vetor 418 | * @param scalar - escalar 419 | * @param one - vetor 420 | * @return - vetor resultante da operacaos 421 | */ 422 | inline friend Vector2D operator/ (T scalar, const Vector2D& one ) 423 | { 424 | Vector2D res ( scalar / one.x, scalar / one.y ); 425 | return res; 426 | } 427 | 428 | /** 429 | * Sobrecarrega do operador de % para realizar o produto vetorial de dois 430 | * vetores 431 | * @param a - primeiro vetor 432 | * @param b - segundo vetor 433 | * @return - vetor resultante do produto vetorial entre a e b 434 | */ 435 | inline friend T operator % (const Vector2D& a, const Vector2D& b ) 436 | { 437 | return a.x * b.y - b.x * a.y; 438 | } 439 | 440 | friend std::ostream& operator << ( std::ostream& o, const Vector2D& v ) 441 | { 442 | return o << "(" << v.x << ", " << v.y << ")"; 443 | } 444 | static const double eps = 10E-5; 445 | }; 446 | 447 | 448 | 449 | #endif /* VECTOR2D_H */ 450 | 451 | -------------------------------------------------------------------------------- /3D/Vector3D.h: -------------------------------------------------------------------------------- 1 | /* 2 | * File: Vector.h 3 | * Author: jeferson 4 | * 5 | * Created on 23 de Setembro de 2013, 20:28 6 | */ 7 | 8 | #ifndef VECTOR3D_H 9 | #define VECTOR3D_H 10 | 11 | #include 12 | #include 13 | #include 14 | 15 | template 16 | class Vector3D 17 | { 18 | public: 19 | T x, y, z; 20 | 21 | 22 | 23 | /** 24 | * Construtor default 25 | */ 26 | Vector3D( ) 27 | { 28 | x = y = z = 0.0; 29 | } 30 | 31 | 32 | 33 | /** 34 | * Construtor com parametros 35 | * @param vx - coordenada x do vetor 36 | * @param vy - coordenada y do vetor 37 | * @param vz - coordenada z do vetor 38 | */ 39 | Vector3D( T vx, T vy, T vz ) 40 | : x( vx ), y( vy ), z( vz ) 41 | { 42 | } 43 | 44 | 45 | 46 | /** 47 | * Construtor com parametros 48 | * @param v - vetor de floar com tres posicoes correspondentes as coordenadas 49 | * x, y e z do vetor 50 | */ 51 | Vector3D( const T v[3] ) 52 | : x( v[0] ), y( v[1] ), z( v[2] ) 53 | { 54 | } 55 | 56 | 57 | 58 | /** 59 | * Destrutor 60 | */ 61 | ~Vector3D( ) 62 | { 63 | } 64 | 65 | 66 | 67 | /** 68 | * Define novas coordenadas para o vetor 69 | * @param vx - coordenada x do vetor 70 | * @param vy - coordenada y do vetor 71 | * @param vz - coordenada z do vetor 72 | */ 73 | void set( T vx, T vy, T vz ) 74 | { 75 | x = vx; 76 | y = vy; 77 | z = vz; 78 | } 79 | 80 | 81 | 82 | /** 83 | * Define novas coordenadas para o vetor 84 | * @param v - vetor de floar com tres posicoes correspondentes as coordenadas 85 | * x, y e z do vetor 86 | */ 87 | void set( const T v[3] ) 88 | { 89 | x = v[0]; 90 | y = v[1]; 91 | z = v[2]; 92 | } 93 | 94 | 95 | 96 | /** 97 | * Realiza o produto escalar entre o vetor corrente e o vetor q. 98 | * @param q - vetor o qual sera realizado produto escalar com o vetor 99 | * corrente 100 | * @return - valor do produto escalar 101 | */ 102 | inline T dot( const Vector3D& q ) const 103 | { 104 | return x * q.x + y * q.y + z * q.z; 105 | } 106 | 107 | 108 | 109 | /** 110 | * Realiza produto vetorial entre os vetores a e b (a X b) e escreve 111 | * o resultado no no vetor corrente. 112 | * @param a - vetor a 113 | * @param b - vetor b 114 | */ 115 | inline void cross( const Vector3D& a, const Vector3D& b ) 116 | { 117 | x = a.y * b.z - a.z * b.y; 118 | y = a.z * b.x - a.x * b.z; 119 | z = a.x * b.y - a.y * b.x; 120 | } 121 | 122 | 123 | 124 | /** 125 | * Calcula o comprimento ao quadrado do vetor 126 | * @return - comprimento ao quadrado do vetor corrente 127 | */ 128 | inline T sqrLength( ) const 129 | { 130 | return (x * x + y * y + z * z); 131 | } 132 | 133 | 134 | 135 | /** 136 | * Calcula o comprimento do vetor 137 | * @return - comprimento do vetor 138 | */ 139 | inline T length( ) const 140 | { 141 | return (T) sqrt( x * x + y * y + z * z ); 142 | } 143 | 144 | 145 | 146 | /** 147 | * Normaliza o vetor e retorna a norma que o vetor tinha antes de ser 148 | * normalizado 149 | * @return - norma do vetor antes de ser normalizado 150 | */ 151 | inline T normalize( ) 152 | { 153 | T l = length( ); 154 | if (l != (T) 0.0) 155 | { 156 | T d = (T) 1.0 / l; 157 | x *= d; 158 | y *= d; 159 | z *= d; 160 | } 161 | return l; 162 | } 163 | 164 | 165 | 166 | /** 167 | * Calculo o angulo entre dois vetores, o corrente e o vetor v passado por 168 | * parametro. 169 | * @param v - vetor v passado por parametro para ser calculado o angulo 170 | * com o vetor corrente 171 | * @return - angulo entre o vetor v e o vetor corrente 172 | */ 173 | inline T angle( const Vector3D& v ) const 174 | { 175 | return (T) (acos( dot( v ) / (length( ) * v.length( )) )); 176 | } 177 | 178 | 179 | 180 | /** 181 | * Imprime o vetor com uma mensagem passada por parametro 182 | * @param label - mensagem impressa antes das coordenadas do vetor 183 | */ 184 | inline void print( const char* label ) const 185 | { 186 | printf( "%s = {%g, %g, %g}\n", label, x, y, z ); 187 | } 188 | 189 | 190 | 191 | /** 192 | * Sobrecarga do operador +=. Atribui ao vetor corrente a soma das suas coordenadas 193 | * com as coordenas de outro vetor passado por parametro. Exemplo de uso: 194 | * a += b. 195 | * @param other- vetor a direita da operacao de += 196 | * @return - retorna uma referencia para o vetor corrente com ja com o resultado 197 | * da operacao 198 | */ 199 | inline Vector3D& operator+=(const Vector3D& other) 200 | { 201 | x += other.x; 202 | y += other.y; 203 | z += other.z; 204 | return *this; 205 | } 206 | 207 | 208 | 209 | /** 210 | * Sobrecarga do operador +=. Adiciona um vetor escalar as coordenadas do 211 | * vetor. Eh equivalente a somar o vetor corrente pelo vetor 212 | * (scalar, scalar, scalar). 213 | * @param scalar - valor escalar a ser somado 214 | * @return - vetor resultado da soma 215 | */ 216 | inline Vector3D& operator+=(T scalar) 217 | { 218 | x += scalar; 219 | y += scalar; 220 | z += scalar; 221 | return *this; 222 | } 223 | 224 | 225 | 226 | /** 227 | * Sobrecarga do operador -=. Atribui ao vetor corrente a subtracao das suas coordenadas 228 | * pelas coordenas de outro vetor passado por parametro. Exemplo de uso: 229 | * a -= b. 230 | * @param other- vetor a direita da operacao de -= 231 | * @return - retorna uma referencia para o vetor corrente com ja com o resultado 232 | * da operacao. 233 | */ 234 | inline Vector3D& operator-=(const Vector3D& other) 235 | { 236 | x -= other.x; 237 | y -= other.y; 238 | z -= other.z; 239 | return *this; 240 | } 241 | 242 | 243 | 244 | /** 245 | * Sobrecarga do operador -=. Adiciona um vetor escalar as coordenadas do 246 | * vetor. Eh equivalente a subtrair o vetor corrente pelo vetor 247 | * (scalar, scalar, scalar). 248 | * @param scalar - valor escalar a ser subtraido 249 | * @return - vetor resultado da soma 250 | */ 251 | inline Vector3D& operator-=(T scalar) 252 | { 253 | x -= scalar; 254 | y -= scalar; 255 | z -= scalar; 256 | return *this; 257 | } 258 | 259 | 260 | 261 | /** 262 | * Sobrecarga do operador unario -. Inverte o sinal de todas as coordenadas 263 | * do vetor corrente e salva em um novo vetor 264 | * @return - retorna novo vetor com as coordenadas com sinal trocado 265 | */ 266 | inline Vector3D operator-() 267 | { 268 | Vector3D v( -x, -y, -z ); 269 | return v; 270 | } 271 | 272 | 273 | 274 | /** 275 | * Sobrecarga do operador *= entre vetores. Multiplica coordenada a coordenada 276 | * dois vetores (o corrente e o passado por parametro) e salva o resultado 277 | * no vetor corrente. Exemplo de uso: a *= b. 278 | * @param other - vetor a ser multiplicado, coordenada a coordenada, pelo 279 | * vetor corrente 280 | * @return - retorna referencia para vetor corrente ja com a operacao aplicada 281 | */ 282 | inline Vector3D& operator*=(const Vector3D& other) 283 | { 284 | x *= other.x; 285 | y *= other.y; 286 | z *= other.z; 287 | return *this; 288 | } 289 | 290 | 291 | 292 | /** 293 | * Sobrecarga do operador *= entre vetor e escalar. Multiplica coordenada a 294 | * coordenada o vetor corrente por um escalar. 295 | * @param scalar - escalar a ser multiplicado pelo vetor corrente 296 | * @return - retorna referencia para vetor corrente ja com a operacao aplicada 297 | */ 298 | inline Vector3D& operator*=(T scalar) 299 | { 300 | x *= scalar; 301 | y *= scalar; 302 | z *= scalar; 303 | return *this; 304 | } 305 | 306 | 307 | 308 | /** 309 | * Sobrecarga do operador /= . Divide todas as coordenadas do vetor corrente 310 | * por um escalar. 311 | * @param scalar - escalar a ser dividido pelo vetor corrente 312 | * @return - retorna referencia para vetor corrente ja com a operacao aplicada 313 | */ 314 | inline Vector3D& operator/=(T scalar) 315 | { 316 | if (scalar != (T) 0.0) 317 | { 318 | x /= scalar; 319 | y /= scalar; 320 | z /= scalar; 321 | } 322 | return *this; 323 | } 324 | //* Free operators 325 | 326 | 327 | 328 | inline friend bool operator ==(const Vector3D& v1, const Vector3D& v2) 329 | { 330 | return ( fabs( v1.x - v2.x ) < eps) && (fabs( v1.y - v2.y ) < eps) && (fabs( v1.z - v2.z ) < eps); 331 | } 332 | 333 | 334 | 335 | inline friend bool operator !=(const Vector3D& v1, const Vector3D& v2) 336 | { 337 | return ( fabs( v1.x - v2.x ) > eps) || (fabs( v1.y - v2.y ) > eps) || (fabs( v1.z - v2.z ) > eps); 338 | } 339 | 340 | 341 | 342 | /** 343 | * Sobrecarga do operador + entre dois vetores: Soma dois vetores e retorna 344 | * resultado da soma 345 | * @param one - primeiro vetor 346 | * @param other - segundo vetor 347 | * @return - vetor com o resultado da soma dos dois vetores 348 | */ 349 | inline friend Vector3D operator+(const Vector3D& one, const Vector3D& other) 350 | { 351 | Vector3D res( one ); 352 | return res += other; 353 | } 354 | 355 | 356 | 357 | /** 358 | * Sobrecarga do operador + entre vetor e escalar: Soma um valor escalar 359 | * a todas as coordenadas do vetor. Exemplo de usoo: vetor + escalar 360 | * @param one - vetor 361 | * @param scalar - escalar que sera somado a todas as coordenadas do vetor 362 | * @return - vetor com o resultado da soma do vetor pelo escalars 363 | */ 364 | inline friend Vector3D operator+(const Vector3D& one, T scalar) 365 | { 366 | Vector3D res( one ); 367 | return res += scalar; 368 | } 369 | 370 | 371 | 372 | /** 373 | * Sobrecarga do operador + entre vetor e escalar: Soma um valor escalar 374 | * a todas as coordenadas do vetor. Exemplo de usoo: escalar + vetor 375 | * @param scalar - escalar que sera somado a todas as coordenadas do vetor 376 | * @param one - vetor 377 | * @return - vetor com o resultado da soma do vetor pelo escalars 378 | */ 379 | inline friend Vector3D operator+(T scalar, const Vector3D& one) 380 | { 381 | Vector3D res( one ); 382 | return res += scalar; 383 | } 384 | 385 | 386 | 387 | /** 388 | * Sobrecarga do operador - entre vetores: Subtrai um vetor pelo outro 389 | * @param one - primeiro vetor 390 | * @param other - segundo vetor 391 | * @return - vetor com o resultado da subtracao entre os vetores 392 | */ 393 | inline friend Vector3D operator-(const Vector3D& one, const Vector3D& other) 394 | { 395 | Vector3D res( one ); 396 | return res -= other; 397 | } 398 | 399 | 400 | 401 | /** 402 | * Sobrecarga do operador - entre vetor e escalar: Subtrai um valor escalar 403 | * a todas as coordenadas do vetor. Exemplo de uso: vetor - scalar 404 | * @param one - vetor 405 | * @param scalar - escalar que sera subtraido de todas as coordenadas do vetor 406 | * @return - vetor com o resultado da subtração pelo escalar 407 | */ 408 | inline friend Vector3D operator-(const Vector3D& one, T scalar) 409 | { 410 | Vector3D res( one ); 411 | return res -= scalar; 412 | } 413 | 414 | 415 | 416 | /** 417 | * Sobrecarga do operador - entre vetor e escalar: Subtrai um valor escalar 418 | * a todas as coordenadas do vetor. Equivalente a (scalar, scalar, scalar) - 419 | * (v1, v2, v3). Exemplo de uso: scalar - vetor 420 | * @param scalar - escalar que sera subtraido de todas as coordenadas do vetor 421 | * @param one - vetor 422 | * @return - vetor com o resultado da subtração pelo escalar 423 | */ 424 | inline friend Vector3D operator-(T scalar, const Vector3D& one) 425 | { 426 | Vector3D res( one ); 427 | res *= -1; 428 | return res += scalar; 429 | } 430 | 431 | 432 | 433 | /** 434 | * Multiplica dois vetores componente a componente 435 | * @param one - primeiro vetor 436 | * @param other - segundo vetor 437 | * @return - vetor resultante da multiplicacao componente a componente 438 | */ 439 | inline friend double operator*(const Vector3D& one, const Vector3D& other) 440 | { 441 | return one.x * other.x + one.y * other.y + one.z * other.z; 442 | } 443 | 444 | 445 | 446 | /** 447 | * Multiplica todas as componentes de um vetor po um escalar 448 | * @param one - vetor 449 | * @param scalar - escalar 450 | * @return - vetor resultante da multiplicacao do escalar por todas as 451 | * componentes 452 | */ 453 | inline friend Vector3D operator*(const Vector3D& one, T scalar) 454 | { 455 | Vector3D res( one ); 456 | return res *= scalar; 457 | } 458 | 459 | 460 | 461 | /** 462 | * Multiplica todas as componentes de um vetor po um escalar 463 | * @param scalar - escalar 464 | * @param one - vetor 465 | * @return - vetor resultante da multiplicacao do escalar por todas as 466 | * componentes 467 | */ 468 | inline friend Vector3D operator*(T scalar, const Vector3D& one) 469 | { 470 | Vector3D res( one ); 471 | return res *= scalar; 472 | } 473 | 474 | 475 | 476 | /** 477 | * Divide todas as componentes de um vetor por escalar 478 | * @param one - vetor 479 | * @param scalar - escalar 480 | * @return vetor resultante da divisao das coordenadas por um escalar 481 | */ 482 | inline friend Vector3D operator/(const Vector3D& one, T scalar) 483 | { 484 | Vector3D res( one ); 485 | return res /= scalar; 486 | } 487 | 488 | 489 | 490 | /** 491 | * Controi um novo vetor onde cada coordenada e resultado da divisao do 492 | * escalar pela coordenada do vetor 493 | * @param scalar - escalar 494 | * @param one - vetor 495 | * @return - vetor resultante da operacaos 496 | */ 497 | inline friend Vector3D operator/(T scalar, const Vector3D& one) 498 | { 499 | Vector3D res( scalar / one.x, scalar / one.y, scalar / one.z ); 500 | return res; 501 | } 502 | 503 | 504 | 505 | /** 506 | * Sobrecarrega do operador de % para realizar o produto vetorial de dois 507 | * vetores 508 | * @param a - primeiro vetor 509 | * @param b - segundo vetor 510 | * @return - vetor resultante do produto vetorial entre a e b 511 | */ 512 | inline friend Vector3D operator %(const Vector3D& a, const Vector3D& b) 513 | { 514 | Vector3D res( a.y * b.z - b.y * a.z, b.x * a.z - a.x * b.z, a.x * b.y - b.x * a.y ); 515 | return res; 516 | } 517 | 518 | 519 | 520 | friend std::ostream& operator <<(std::ostream& o, const Vector3D& v) 521 | { 522 | return o << "(" << v.x << ", " << v.y << ", " << v.z << ")"; 523 | } 524 | static const double eps = 10E-5; 525 | }; 526 | 527 | typedef Vector3D Point3D; 528 | 529 | #endif /* VECTOR3D_H */ 530 | 531 | -------------------------------------------------------------------------------- /3D/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include "Tipos.h" 17 | #include "Render.h" 18 | #include "KdTree.h" 19 | #include "Ray.h" 20 | #include "IntersectRayAABB.h" 21 | #include "IntersectRayTriangle.h" 22 | //#include "Timer.h" 23 | #include "ConvexHull.h" 24 | #include "Point2D.h" 25 | #include "PointInPolygon.h" 26 | //#include 27 | 28 | using namespace std; 29 | 30 | //controle da camera 31 | Camera cam; 32 | 33 | //vetor que determina a teclas clicadas 34 | bool teclas[256] = { false }; 35 | 36 | //variaveis que armazenam a dimensao da janela principal 37 | float displayW = 500, displayH = 50000; 38 | 39 | //variavel que armazena o zoom da camerda 40 | float camZoom = 0; 41 | 42 | //variavel pra determinar se a grade xy deve ser renderizada ou nao 43 | bool gridXY = true; 44 | 45 | //vetores para iluminacao 46 | GLfloat light_position[] = { 0, -200, 100 }; 47 | GLfloat light_position2[] = { 100, 100, 100 }; 48 | GLfloat light_position3[] = { -100, 100, 100 }; 49 | GLfloat mat_ambient[] = { 0.1, 0.1, 0.1, 1.0 }; 50 | GLfloat mat_diffuse[] = { 1.0, 1.0, 1.0, 1.0 }; 51 | GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 }; 52 | GLfloat mat_shininess[] = { 50.0 }; 53 | 54 | // variaveis que armazenam a posicao inicial do mouse quando este eh arrastado na tela 55 | int lastPositionMouseMoveX = 0, lastPositionMouseMoveY = 0; 56 | 57 | //variavel para armazenar se houve um mouse press na janela principal 58 | bool mousePressLeftButton = false; 59 | 60 | //variavel que armazena a velocidade corrente da camera 61 | double velocityCamera = 0.1; 62 | 63 | //vetor de normais da superf�cie 64 | float *normals = 0; 65 | 66 | //vetor de coordenaas 67 | float* coordinates = 0; 68 | 69 | //vetor com triangulacao 70 | int* triangles = 0; 71 | 72 | //instancia da arvore 73 | KdTree* kdTree = 0; 74 | 75 | //numero de pontos 76 | int numberPoints = 0; 77 | 78 | //numero de triangulos 79 | int numberTriangles = 0; 80 | 81 | //angulo de abertura da camera 82 | float fov = 0.0f; 83 | 84 | //variavel que determina se a superficie vai ser vizualizada como malha ou faces 85 | bool fillSurface = true; 86 | 87 | //variavel pra determinar se a supeficie deve ser renderizada ou nao 88 | bool viewSurface = true; 89 | 90 | //varievel que armazena o ID da janela 91 | int janela3D = 0; 92 | 93 | int depth = 0; 94 | 95 | int cont; 96 | int contAABBTest = 0; 97 | 98 | 99 | Point3D origin( 0.0, 0.5, 0.0 ); 100 | Vector3D direction( 0.0, 0.0, 0.0 ); 101 | 102 | float xMed, yMed, zMed; 103 | 104 | std::vector< Point3D > insideSurface; 105 | int incrementGrid = 1; 106 | 107 | bool renderCubes = false; 108 | 109 | 110 | 111 | void renderCube( float xMin, float yMin, float zMin, float xMax, float yMax, float zMax ) 112 | { 113 | float points[8][3] = { 114 | {xMin, yMin, zMin }, //0 115 | {xMin, yMin, zMax }, //1 116 | {xMin, yMax, zMin }, //2 117 | {xMin, yMax, zMax }, //3 118 | {xMax, yMin, zMin }, //4 119 | {xMax, yMin, zMax }, //5 120 | {xMax, yMax, zMin }, //6 121 | {xMax, yMax, zMax }//7 122 | }; 123 | 124 | int quads[24] = { 125 | 5, 7, 3, 1, 126 | 0, 2, 6, 4, 127 | 4, 6, 7, 5, 128 | 6, 2, 3, 7, 129 | 2, 0, 1, 3, 130 | 0, 4, 5, 1 131 | }; 132 | 133 | glEnable( GL_BLEND ); 134 | glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA ); 135 | 136 | glEnableClientState( GL_VERTEX_ARRAY ); 137 | 138 | glVertexPointer( 3, GL_FLOAT, 0, points ); 139 | 140 | glDrawElements( GL_QUADS, 24, GL_UNSIGNED_INT, quads ); 141 | 142 | glDisableClientState( GL_VERTEX_ARRAY ); 143 | 144 | glDisable( GL_BLEND ); 145 | } 146 | 147 | 148 | 149 | void renderNode( const Node * node, int d ) 150 | { 151 | if (node == 0 || d >= depth) 152 | { 153 | return; 154 | } 155 | 156 | if (d == depth - 1) 157 | { 158 | float increment = 1.0f / numberTriangles; 159 | glColor4f( cont * increment, 1.0f, 0.0, 0.6 ); 160 | glPolygonMode( GL_FRONT_AND_BACK, GL_FILL ); 161 | renderCube( node->minX, node->minY, node->minZ, node->maxX, node->maxY, node->maxZ ); 162 | 163 | glColor3f( 1.0, 0.0, 0.0 ); 164 | glLineWidth( 4.0f ); 165 | glPolygonMode( GL_FRONT_AND_BACK, GL_LINE ); 166 | renderCube( node->minX, node->minY, node->minZ, node->maxX, node->maxY, node->maxZ ); 167 | glLineWidth( 1.0f ); 168 | } 169 | cont++; 170 | 171 | renderNode( node->left, d + 1 ); 172 | renderNode( node->right, d + 1 ); 173 | } 174 | 175 | 176 | 177 | void renderKdTree( ) 178 | { 179 | if (kdTree) 180 | { 181 | cont = 0; 182 | renderNode( kdTree->getRoot( ), 0 ); 183 | } 184 | } 185 | 186 | 187 | 188 | void renderRay( ) 189 | { 190 | glLineWidth( 6.0f ); 191 | glColor3f( 0.0f, 0.0f, 1.0f ); 192 | 193 | glBegin( GL_LINES ); 194 | glVertex3f( origin.x, origin.y, origin.z ); 195 | glVertex3f( origin.x + 1000 * direction.x, origin.y + 1000 * direction.y, origin.z + 1000 * direction.z ); 196 | glEnd( ); 197 | 198 | glLineWidth( 1.0f ); 199 | } 200 | 201 | 202 | 203 | void renderInsideSurface( ) 204 | { 205 | if (!kdTree) 206 | return; 207 | 208 | glColor3f( 1.0, 0.5, 0.0 ); 209 | glBegin( GL_POINTS ); 210 | //glutSolidCube( 1000 ); 211 | for (unsigned int i = 0; i < insideSurface.size( ); i++) 212 | { 213 | glPushMatrix( ); 214 | glTranslated( insideSurface[i].x, insideSurface[i].y, insideSurface[i].z ); 215 | glutSolidCube( 1 ); 216 | glPopMatrix( ); 217 | } 218 | glEnd( ); 219 | } 220 | 221 | 222 | 223 | /** 224 | * Funcao que renderiza a janela principal 225 | */ 226 | void display( void ) 227 | { 228 | glutSetWindow( janela3D ); 229 | glViewport( 0, 0, (int) displayW, (int) displayH ); 230 | glClearColor( 1.0, 1.0, 1.0, 0.0 ); 231 | glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); 232 | glMatrixMode( GL_PROJECTION ); 233 | glLoadIdentity( ); 234 | 235 | if (teclas[(int) '4']) 236 | cam.rotateX -= cam.velocityRotation; 237 | 238 | if (teclas[(int) '6']) 239 | cam.rotateX += cam.velocityRotation; 240 | 241 | if (teclas[(int) '8']) 242 | cam.rotateY -= cam.velocityRotation; 243 | 244 | if (teclas[(int) '2']) 245 | cam.rotateY += cam.velocityRotation; 246 | 247 | if (teclas[(int) 'w']) 248 | { 249 | cam.velocity = velocityCamera; 250 | } 251 | else if (teclas[(int) 's']) 252 | { 253 | cam.velocity = -velocityCamera; 254 | } 255 | else 256 | { 257 | cam.velocity = 0.0; 258 | } 259 | 260 | if (teclas[(int) 'a']) 261 | { 262 | cam.lateralVelocity = -velocityCamera; 263 | } 264 | else if (teclas[(int) 'd']) 265 | { 266 | cam.lateralVelocity = +velocityCamera; 267 | } 268 | else 269 | { 270 | cam.lateralVelocity = 0.0; 271 | } 272 | 273 | if (cam.rotateY > 90) 274 | cam.rotateY = 90; 275 | 276 | if (cam.rotateY <-90) 277 | cam.rotateY = -90; 278 | 279 | cam.walkingDirectionX = sin( PI * cam.rotateX / 180.0 ) * cos( PI * cam.rotateY / 180.0 ); 280 | cam.walkingDirectionZ = sin( PI * cam.rotateY / 180.0 ); 281 | cam.walkingDirectionY = cos( PI * cam.rotateX / 180.0 ) * cos( PI * cam.rotateY / 180.0 ); 282 | cam.walkingLateralDirectionX = sin( PI * (cam.rotateX + 90) / 180.0 ) * cos( PI * cam.rotateY / 180.0 ); 283 | cam.walkingLateralDirectionZ = sin( PI * cam.rotateY / 180.0 ); 284 | cam.walkingLateralDirectionY = cos( PI * (cam.rotateX + 90) / 180.0 ) * cos( PI * cam.rotateY / 180.0 ); 285 | 286 | cam.targetX = cam.walkingDirectionX * 3 + cam.positionX; 287 | cam.targetY = cam.walkingDirectionY * 3 + cam.positionY; 288 | cam.targetZ = cam.walkingDirectionZ * 3 + cam.positionZ; 289 | cam.positionX += cam.walkingDirectionX * cam.velocity; 290 | cam.positionY += cam.walkingDirectionY * cam.velocity; 291 | cam.positionZ += cam.walkingDirectionZ * cam.velocity; 292 | cam.positionX += cam.walkingLateralDirectionX * cam.lateralVelocity; 293 | cam.positionY += cam.walkingLateralDirectionY * cam.lateralVelocity; 294 | 295 | glMatrixMode( GL_PROJECTION ); 296 | glLoadIdentity( ); 297 | fov = 45 + camZoom; 298 | if (fov < 5.0f) fov = 5.0f; 299 | if (fov > 160.0f) fov = 160.0f; 300 | 301 | gluPerspective( fov, (float) displayW / (float) displayH, 1.0f, 50000.0f ); 302 | glMatrixMode( GL_MODELVIEW ); 303 | glLoadIdentity( ); 304 | 305 | gluLookAt( cam.positionX, cam.positionY, cam.positionZ, // eye 306 | cam.targetX, cam.targetY, cam.targetZ, // target 307 | 0, 0, 1 ); // up 308 | glLightfv( GL_LIGHT0, GL_POSITION, light_position ); 309 | glLightfv( GL_LIGHT1, GL_POSITION, light_position2 ); 310 | glLightfv( GL_LIGHT2, GL_POSITION, light_position3 ); 311 | 312 | glColor3f( 1.0, 0.0, 0.0 ); 313 | 314 | if (gridXY) 315 | { 316 | renderGridXY( ); 317 | } 318 | 319 | glEnable( GL_LIGHTING ); 320 | if (viewSurface) 321 | { 322 | glPushMatrix( ); 323 | //glScalef( 0.10f, 0.10f, 0.50f ); 324 | renderSurfaceWireframe( coordinates, normals, triangles, numberTriangles, fillSurface ); 325 | renderKdTree( ); 326 | renderRay( ); 327 | glPopMatrix( ); 328 | } 329 | if (renderCubes) 330 | renderInsideSurface( ); 331 | 332 | glDisable( GL_LIGHTING ); 333 | glFlush( ); 334 | glutSwapBuffers( ); 335 | } 336 | 337 | 338 | 339 | /** 340 | * Funcao que recebe evendo de quando um tecla � solta do teclado 341 | * @param key - tecla 342 | * @param x - posicao x do mouse na tela 343 | * @param y - posicao y do mouse na tela 344 | */ 345 | void keyboardUpInput( unsigned char key, int x, int y ) 346 | { 347 | int a = x; 348 | a = y; 349 | teclas[key] = false; 350 | } 351 | 352 | 353 | 354 | /** 355 | * Funcao de idle da janela principal. Evita que a janela seja travada 356 | */ 357 | void idleWindow3D( void ) 358 | { 359 | glutSetWindow( janela3D ); 360 | glutPostRedisplay( ); 361 | } 362 | 363 | 364 | 365 | /** 366 | * Janela que recebe eventos de reshape da janela 3D 367 | * @param w - largura da janela em pixeis 368 | * @param h - altura da janela em pixeis 369 | */ 370 | void reshapeWindow3D( int w, int h ) 371 | { 372 | glutSetWindow( janela3D ); 373 | displayW = w; 374 | displayH = h; 375 | glViewport( 0, 0, w, h ); 376 | glMatrixMode( GL_PROJECTION ); 377 | glLoadIdentity( ); 378 | gluPerspective( 90, (float) w / h, 2, 100 ); 379 | glMatrixMode( GL_MODELVIEW ); 380 | glutPostWindowRedisplay( janela3D ); 381 | } 382 | 383 | 384 | 385 | /** 386 | * Mouse move na janela princial 387 | * @param x - coordenada x corrente 388 | * @param y - coordenada y corrente 389 | */ 390 | void mouseMoveWindow3D( GLsizei x, GLsizei y ) 391 | { 392 | if (mousePressLeftButton) 393 | { 394 | cam.rotateX += x - lastPositionMouseMoveX; 395 | cam.rotateY -= y - lastPositionMouseMoveY; 396 | lastPositionMouseMoveX = x; 397 | lastPositionMouseMoveY = y; 398 | } 399 | } 400 | 401 | 402 | 403 | void computeNormals( ) 404 | { 405 | // Inicializa as normais 406 | for (int i = 0; i < 3 * numberPoints; i++) 407 | normals[i] = 0.0; 408 | 409 | // Percorre as faces calculando 410 | for (int i = 0; i < numberTriangles; i++) 411 | { 412 | int v0, v1, v2; 413 | 414 | v0 = triangles[3 * i + 0]; 415 | v1 = triangles[3 * i + 1]; 416 | v2 = triangles[3 * i + 2]; 417 | 418 | float v1x = coordinates[ 3 * v1 + 0 ] - coordinates[ 3 * v0 + 0 ]; 419 | float v1y = coordinates[ 3 * v1 + 1 ] - coordinates[ 3 * v0 + 1 ]; 420 | float v1z = coordinates[ 3 * v1 + 2 ] - coordinates[ 3 * v0 + 2 ]; 421 | 422 | float v2x = coordinates[ 3 * v2 + 0 ] - coordinates[ 3 * v0 + 0 ]; 423 | float v2y = coordinates[ 3 * v2 + 1 ] - coordinates[ 3 * v0 + 1 ]; 424 | float v2z = coordinates[ 3 * v2 + 2 ] - coordinates[ 3 * v0 + 2 ]; 425 | 426 | float x = v1y * v2z - v2y * v1z; 427 | float y = v1z * v2x - v1x * v2z; 428 | float z = v1x * v2y - v1y * v2x; 429 | 430 | // Adiciona a normal da face a todos os vertices da face 431 | normals[3 * v0 + 0] += x; 432 | normals[3 * v0 + 1] += y; 433 | normals[3 * v0 + 2] += z; 434 | 435 | normals[3 * v1 + 0] += x; 436 | normals[3 * v1 + 1] += y; 437 | normals[3 * v1 + 2] += z; 438 | 439 | normals[3 * v2 + 0] += x; 440 | normals[3 * v2 + 1] += y; 441 | normals[3 * v2 + 2] += z; 442 | } 443 | 444 | // normaliza??o das componentes 445 | for (int i = 0; i < numberPoints; i++) 446 | { 447 | float nx, ny, nz; 448 | nx = normals[3 * i + 0]; 449 | ny = normals[3 * i + 1]; 450 | nz = normals[3 * i + 2]; 451 | 452 | float lenght = sqrt( nx * nx + ny * ny + nz * nz ); 453 | 454 | if (lenght != 0.0f) 455 | { 456 | normals[3 * i + 0] /= lenght; 457 | normals[3 * i + 1] /= lenght; 458 | normals[3 * i + 2] /= lenght; 459 | } 460 | } 461 | } 462 | 463 | 464 | 465 | int getNumIntersection( const Node* n, const Ray& r, bool &bord ) 466 | { 467 | if (n == 0 || bord) 468 | { 469 | return 0; 470 | } 471 | 472 | int cont = 0; 473 | //contAABBTest++; 474 | if (intersectRayAABB( r, n->minX, n->minY, n->minZ, n->maxX, n->maxY, n->maxZ )) 475 | { 476 | int triangleIndex = n->triangleIndex; 477 | 478 | int v0 = triangles[3 * triangleIndex + 0]; 479 | int v1 = triangles[3 * triangleIndex + 1]; 480 | int v2 = triangles[3 * triangleIndex + 2]; 481 | 482 | float x1, x2, x3, y1, y2, y3, z1, z2, z3; 483 | x1 = coordinates[3 * v0 + 0]; 484 | x2 = coordinates[3 * v1 + 0]; 485 | x3 = coordinates[3 * v2 + 0]; 486 | 487 | y1 = coordinates[3 * v0 + 1]; 488 | y2 = coordinates[3 * v1 + 1]; 489 | y3 = coordinates[3 * v2 + 1]; 490 | 491 | z1 = coordinates[3 * v0 + 2]; 492 | z2 = coordinates[3 * v1 + 2]; 493 | z3 = coordinates[3 * v2 + 2]; 494 | 495 | int ret = intersectRayTriangle( r, x1, y1, z1, x2, y2, z2, x3, y3, z3 ); 496 | if (ret == 2) 497 | { 498 | bord = true; 499 | } 500 | if (ret) 501 | { 502 | cont++; 503 | } 504 | cont += getNumIntersection( n->left, r, bord ); 505 | cont += getNumIntersection( n->right, r, bord ); 506 | } 507 | return cont; 508 | } 509 | 510 | 511 | 512 | void getDirection( Vector3D & direction, float x, float y, float z ) 513 | { 514 | if (x > xMed) 515 | { 516 | direction.x = 1.0f; 517 | } 518 | else 519 | { 520 | direction.x = -1.0f; 521 | } 522 | 523 | if (y > yMed) 524 | { 525 | direction.y = 1.0f; 526 | } 527 | else 528 | { 529 | direction.y = -1.0f; 530 | } 531 | 532 | if (z > zMed) 533 | { 534 | direction.z = 1.0f; 535 | } 536 | else 537 | { 538 | direction.z = -1.0f; 539 | } 540 | } 541 | 542 | 543 | 544 | int testIntersections( float x = 0, float y = 0, float z = 0 ) 545 | { 546 | bool bord = false; 547 | int nIntersections = 0; 548 | int a = 0; 549 | do 550 | { 551 | Point3D origin = Point3D( x, y, z ); 552 | // int triangleIndex = rand( ) % numberTriangles; 553 | // 554 | // int v0 = triangles[3 * triangleIndex + 0]; 555 | // int v1 = triangles[3 * triangleIndex + 1]; 556 | // int v2 = triangles[3 * triangleIndex + 2]; 557 | // 558 | // float x1, x2, x3, y1, y2, y3, z1, z2, z3; 559 | // x1 = coordinates[3 * v0 + 0]; 560 | // x2 = coordinates[3 * v1 + 0]; 561 | // x3 = coordinates[3 * v2 + 0]; 562 | // 563 | // y1 = coordinates[3 * v0 + 1]; 564 | // y2 = coordinates[3 * v1 + 1]; 565 | // y3 = coordinates[3 * v2 + 1]; 566 | // 567 | // z1 = coordinates[3 * v0 + 2]; 568 | // z2 = coordinates[3 * v1 + 2]; 569 | // z3 = coordinates[3 * v2 + 2]; 570 | // 571 | // float xM = (x1 + x2 + x3) / 3.0; 572 | // float yM = (y1 + y2 + y3) / 3.0; 573 | // float zM = (z1 + z2 + z3) / 3.0; 574 | // 575 | // 576 | 577 | Point3D direction; 578 | if (a == 0) 579 | { 580 | //getDirection( direction, x, y, z ); 581 | if (z >= (kdTree->getRoot( )->minZ + kdTree->getRoot( )->maxZ) / 2.0) 582 | { 583 | direction = Vector3D (0.0, 0.0, 1); 584 | } 585 | else 586 | { 587 | direction = Vector3D (0.0, 0.0, -1); 588 | } 589 | } 590 | else 591 | { 592 | direction = Vector3D (rand( ) % 10, rand( ) % 10, rand( ) % 10); 593 | } 594 | 595 | bord = false; 596 | Ray r( origin, direction ); 597 | nIntersections = getNumIntersection( kdTree->getRoot( ), r, bord ); 598 | // if (bord) 599 | // printf( "borda\n" ); 600 | a++; 601 | if (a > 100) 602 | { 603 | printf( "Numero maximo de iteracoes\n" ); 604 | break; 605 | } 606 | } 607 | while (bord); 608 | 609 | 610 | 611 | return nIntersections; 612 | } 613 | 614 | 615 | 616 | void openMesh( ) 617 | { 618 | std::string fileName; 619 | // printf( "Digite o nome do arquivo: " ); 620 | //std::cin >> fileName; 621 | fileName = "Demos/icosaedro.txt"; 622 | 623 | std::ifstream in( fileName.c_str( ) ); 624 | if (in.fail( )) 625 | { 626 | printf( "Falha ao abrir arquivo\n" ); 627 | return; 628 | } 629 | in >> numberPoints >> numberTriangles; 630 | 631 | printf( "Arquivo carregado\n" ); 632 | if (normals != 0) 633 | { 634 | delete [] normals; 635 | } 636 | if (coordinates != 0) 637 | { 638 | delete []coordinates; 639 | } 640 | if (triangles != 0) 641 | { 642 | delete []triangles; 643 | } 644 | 645 | normals = new float[3 * numberPoints]; 646 | coordinates = new float[3 * numberPoints]; 647 | triangles = new int[3 * numberTriangles]; 648 | 649 | int lixo; 650 | for (int i = 0; i < numberPoints; i++) 651 | { 652 | in >> lixo >> coordinates[3 * i] >> coordinates[3 * i + 1] >> coordinates[3 * i + 2]; 653 | coordinates[3 * i + 0] *= 50.f; 654 | coordinates[3 * i + 1] *= 50.f; 655 | coordinates[3 * i + 2] *= 50.f; 656 | } 657 | for (int i = 0; i < numberTriangles; i++) 658 | { 659 | in >> lixo >> triangles[3 * i] >> triangles[3 * i + 1] >> triangles[3 * i + 2]; 660 | if (fileName == "Demos/superficie_teste.txt") 661 | { 662 | triangles[3 * i]--; 663 | triangles[3 * i + 1]--; 664 | triangles[3 * i + 2]--; 665 | } 666 | } 667 | computeNormals( ); 668 | 669 | if (kdTree != 0) 670 | { 671 | delete kdTree; 672 | } 673 | 674 | kdTree = new KdTree( coordinates, triangles, numberTriangles, 3 ); 675 | 676 | if (fileName == "Demos/superficie_teste.txt") 677 | { 678 | for (int i = 0; i < numberPoints; i++) 679 | { 680 | coordinates[3 * i + 0] = coordinates[3 * i + 0] - kdTree->getRoot( )->minX; 681 | coordinates[3 * i + 1] = coordinates[3 * i + 1] - kdTree->getRoot( )->minY; 682 | coordinates[3 * i + 2] = coordinates[3 * i + 2] - kdTree->getRoot( )->minZ; 683 | 684 | // coordinates[3 * i + 0] *= 0.1f; 685 | // coordinates[3 * i + 1] *= 0.1f; 686 | // coordinates[3 * i + 2] *= 0.5f; 687 | } 688 | } 689 | delete kdTree; 690 | kdTree = new KdTree( coordinates, triangles, numberTriangles, 3 ); 691 | testIntersections( ); 692 | 693 | printf( "(%f, %f, %f)\n(%f, %f,%f)\n", kdTree->getRoot( )->minX, kdTree->getRoot( )->minY, kdTree->getRoot( )->minZ, 694 | kdTree->getRoot( )->maxX, kdTree->getRoot( )->maxY, kdTree->getRoot( )->maxZ ); 695 | 696 | xMed = (kdTree->getRoot( )->minX + kdTree->getRoot( )->maxX) / 2.0; 697 | yMed = (kdTree->getRoot( )->minY + kdTree->getRoot( )->maxY) / 2.0; 698 | zMed = (kdTree->getRoot( )->minZ + kdTree->getRoot( )->maxZ) / 2.0; 699 | } 700 | 701 | 702 | 703 | void computeConvexHull( std::vector& convexHull ) 704 | { 705 | convexHull.reserve( numberPoints ); 706 | for (int i = 0; i < numberPoints; i++) 707 | { 708 | convexHull.push_back( Point2D( coordinates[3 * i], coordinates[3 * i + 1] ) ); 709 | } 710 | ConvexHull c( &convexHull[0], numberPoints ); 711 | convexHull = c.grahamScan( ); 712 | } 713 | 714 | 715 | 716 | void computeSamplesInsideSurface( ) 717 | { 718 | if (!kdTree) 719 | return; 720 | 721 | //Timer t, t1; 722 | 723 | std::vector< Point2D > convexHull; 724 | computeConvexHull( convexHull ); 725 | //t1.printTime( "Tempo para construir convexHull(s): " ); 726 | 727 | PointInPolygon p( &convexHull[0], convexHull.size( ) ); 728 | 729 | //omp_set_num_threads( 8 ); 730 | //printf( "Numero de threads: %d %d\n", omp_get_max_threads( ), omp_get_num_procs( ) ); 731 | 732 | const int a1 = kdTree->getRoot( )->minX, a2 = kdTree->getRoot( )->maxX; 733 | const int b1 = (int) kdTree->getRoot( )->minY, b2 = (int) kdTree->getRoot( )->maxY; 734 | const int c1 = (int) kdTree->getRoot( )->minZ, c2 = (int) kdTree->getRoot( )->maxZ; 735 | 736 | printf( "Total de amostras: %ld\n", (long int) (a2 - a1) * (b2 - b1) * (c2 - c1) / (incrementGrid * incrementGrid * incrementGrid) ); 737 | srand( time( NULL ) ); 738 | 739 | //#pragma omp parallel num_threads(7) 740 | { 741 | //#pragma omp for schedule( guided ) 742 | 743 | for (int x = a1; x < a2; x += incrementGrid) 744 | { 745 | //printf( "%d\n", x ); 746 | //#pragma omp parallel for 747 | for (int y = b1; y < b2; y += incrementGrid) 748 | { 749 | if (p.rotationIndex( Point2D( x, y ) ) == 1) 750 | { 751 | continue; 752 | } 753 | //#pragma omp parallel for 754 | for (int z = c1; z < c2; z += incrementGrid) 755 | { 756 | if (testIntersections( x, y, z ) % 2) 757 | { 758 | insideSurface.push_back( Point3D( x, y, z ) ); 759 | } 760 | // totalTestAABB += contAABBTest; 761 | // if (n % 2) 762 | // { 763 | // outside++; 764 | // } 765 | // else 766 | // { 767 | // inside++; 768 | // } 769 | } 770 | } 771 | } 772 | } 773 | // printf( "Inside: %d\nOutside:%d\n", inside, outside ); 774 | // printf( "Numero de testes medio por amostra: %f\n", (float) totalTestAABB / (inside + outside) ); 775 | // printf( "Total de amostras: %ld\n", (long int) (a2 - a1) * (b2 - b1) * (c2 - c1) / (increment * increment * increment) ); 776 | //t.printTime( "Tempo (s)" ); 777 | } 778 | 779 | 780 | 781 | /** 782 | * Funcao que recebe eventos de teclado na janela principal 783 | * @param key - tecla que enviou o evento 784 | * @param x - coordenada x do mouse em pixeis 785 | * @param y - coordenada y do mouse em pixeis 786 | */ 787 | void keyboardInput( unsigned char key, int x, int y ) 788 | { 789 | int a = x; 790 | a = y; 791 | teclas[key] = true; 792 | switch (key) 793 | { 794 | case 'A': 795 | openMesh( ); 796 | break; 797 | case 'p': 798 | fillSurface = !fillSurface; 799 | break; 800 | case 'q': 801 | viewSurface = !viewSurface; 802 | break; 803 | case '+': 804 | if (velocityCamera < 90) 805 | { 806 | velocityCamera += 0.1; 807 | } 808 | break; 809 | case '-': 810 | if (velocityCamera >= 0) 811 | { 812 | velocityCamera -= 0.1; 813 | } 814 | break; 815 | case '/': 816 | if (camZoom > 0)camZoom -= 1; 817 | break; 818 | case 'g': 819 | gridXY = !gridXY; 820 | break; 821 | case '*': 822 | if (camZoom < 150)camZoom += 1; 823 | break; 824 | case 'b': 825 | depth--; 826 | if (depth < 0) 827 | depth = 0; 828 | break; 829 | case 'B': 830 | depth++; 831 | break; 832 | case 'T': 833 | case 't': 834 | testIntersections( ); 835 | break; 836 | case 'R': 837 | computeSamplesInsideSurface( ); 838 | 839 | case 'c': 840 | case 'C': 841 | renderCubes = !renderCubes; 842 | break; 843 | } 844 | display( ); 845 | } 846 | 847 | 848 | 849 | /** 850 | * Recebe evento de mouse na janela principal 851 | * @param button - botao que envia o evento 852 | * @param state - estado do botao 853 | * @param x - coordenada x do mouse na tela em pixeis 854 | * @param y - coordenada y do mouse na tela em pixeis 855 | */ 856 | void mouseWindow3D( int button, int state, int x, int y ) 857 | { 858 | if (button == GLUT_LEFT_BUTTON) 859 | { 860 | if (state == GLUT_DOWN) 861 | { 862 | lastPositionMouseMoveX = x; 863 | lastPositionMouseMoveY = y; 864 | mousePressLeftButton = true; 865 | } 866 | } 867 | if (state == GLUT_UP) 868 | { 869 | 870 | 871 | 872 | mousePressLeftButton = false; 873 | } 874 | glutSetWindow( janela3D ); 875 | glutPostRedisplay( ); 876 | } 877 | 878 | 879 | 880 | /** 881 | * Funcao para inicializar os parametros que forem necessarios 882 | */ 883 | void myInit( void ) 884 | { 885 | 886 | 887 | 888 | glutSetWindow( janela3D ); 889 | 890 | //luz 1 891 | GLfloat light_ambient[] = { 0.1, 0.1, 0.1, 1.0 }; 892 | GLfloat light_diffuse[] = { 1.0, 1.0, 1.0, 0.0 }; 893 | GLfloat light_specular[] = { 1.0, 1.0, 1.0, 0.0 }; 894 | glLightfv( GL_LIGHT0, GL_AMBIENT, light_ambient ); 895 | glLightfv( GL_LIGHT0, GL_DIFFUSE, light_diffuse ); 896 | glLightfv( GL_LIGHT0, GL_SPECULAR, light_specular ); 897 | 898 | //luz 2 899 | glLightfv( GL_LIGHT1, GL_AMBIENT, light_ambient ); 900 | glLightfv( GL_LIGHT1, GL_DIFFUSE, light_diffuse ); 901 | glLightfv( GL_LIGHT1, GL_SPECULAR, light_specular ); 902 | 903 | glEnable( GL_LIGHT1 ); 904 | 905 | glLightfv( GL_LIGHT2, GL_AMBIENT, light_ambient ); 906 | glLightfv( GL_LIGHT2, GL_DIFFUSE, light_diffuse ); 907 | glLightfv( GL_LIGHT2, GL_SPECULAR, light_specular ); 908 | 909 | glEnable( GL_LIGHTING ); 910 | glEnable( GL_LIGHT0 ); 911 | glEnable( GL_LIGHT1 ); 912 | glEnable( GL_LIGHT2 ); 913 | glEnable( GL_DEPTH_TEST ); 914 | glEnable( GL_COLOR_MATERIAL ); 915 | 916 | 917 | glShadeModel( GL_SMOOTH ); 918 | // glShadeModel ( GL_FLAT ); 919 | glMaterialfv( GL_FRONT, GL_AMBIENT, mat_ambient ); 920 | glMaterialfv( GL_FRONT, GL_DIFFUSE, mat_diffuse ); 921 | glMaterialfv( GL_FRONT, GL_SPECULAR, mat_specular ); 922 | glMaterialfv( GL_FRONT, GL_SHININESS, mat_shininess ); 923 | 924 | glColor4f( 1.0f, 1.0f, 1.0f, 0.5f ); // Full Brightness, 50% Alpha ( NEW ) 925 | glBlendFunc( GL_SRC_ALPHA, GL_ONE ); 926 | glMatrixMode( GL_PROJECTION ); 927 | glLoadIdentity( ); 928 | gluPerspective( 90, 1, 2, 100 ); 929 | 930 | glMatrixMode( GL_MODELVIEW ); 931 | 932 | glClearColor( 1, 1, 1, 0 ); 933 | } 934 | 935 | 936 | 937 | int main( int argc, char *argv[] ) 938 | { 939 | glutInit( &argc, argv ); 940 | //janela 3D 941 | glutInitDisplayMode( GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH ); 942 | glutInitWindowSize( 600, 600 ); 943 | glutInitWindowPosition( 500, 50 ); 944 | janela3D = glutCreateWindow( "Surface" ); 945 | glutKeyboardFunc( keyboardInput ); 946 | glutKeyboardUpFunc( keyboardUpInput ); 947 | glutReshapeFunc( reshapeWindow3D ); 948 | glutDisplayFunc( display ); 949 | glutIdleFunc( idleWindow3D ); 950 | glutMotionFunc( mouseMoveWindow3D ); 951 | glutMouseFunc( mouseWindow3D ); 952 | myInit( ); 953 | glutMainLoop( ); 954 | 955 | if (normals != 0) 956 | { 957 | delete[] normals; 958 | } 959 | //delete[]noDataPoints; 960 | return 0; 961 | } 962 | -------------------------------------------------------------------------------- /3D/nbproject/Makefile-Debug.mk: -------------------------------------------------------------------------------- 1 | # 2 | # Generated Makefile - do not edit! 3 | # 4 | # Edit the Makefile in the project folder instead (../Makefile). Each target 5 | # has a -pre and a -post target defined where you can add customized code. 6 | # 7 | # This makefile implements configuration specific macros and targets. 8 | 9 | 10 | # Environment 11 | MKDIR=mkdir 12 | CP=cp 13 | GREP=grep 14 | NM=nm 15 | CCADMIN=CCadmin 16 | RANLIB=ranlib 17 | CC=gcc 18 | CCC=g++ 19 | CXX=g++ 20 | FC=gfortran 21 | AS=as 22 | 23 | # Macros 24 | CND_PLATFORM=GNU-MacOSX 25 | CND_DLIB_EXT=dylib 26 | CND_CONF=Debug 27 | CND_DISTDIR=dist 28 | CND_BUILDDIR=build 29 | 30 | # Include project Makefile 31 | include Makefile 32 | 33 | # Object Directory 34 | OBJECTDIR=${CND_BUILDDIR}/${CND_CONF}/${CND_PLATFORM} 35 | 36 | # Object Files 37 | OBJECTFILES= \ 38 | ${OBJECTDIR}/ConvexHull.o \ 39 | ${OBJECTDIR}/KdTree.o \ 40 | ${OBJECTDIR}/Node.o \ 41 | ${OBJECTDIR}/PointInPolygon.o \ 42 | ${OBJECTDIR}/Ray.o \ 43 | ${OBJECTDIR}/main.o 44 | 45 | 46 | # C Compiler Flags 47 | CFLAGS= 48 | 49 | # CC Compiler Flags 50 | CCFLAGS=-Wall 51 | CXXFLAGS=-Wall 52 | 53 | # Fortran Compiler Flags 54 | FFLAGS= 55 | 56 | # Assembler Flags 57 | ASFLAGS= 58 | 59 | # Link Libraries and Options 60 | LDLIBSOPTIONS= 61 | 62 | # Build Targets 63 | .build-conf: ${BUILD_SUBPROJECTS} 64 | "${MAKE}" -f nbproject/Makefile-${CND_CONF}.mk ${CND_DISTDIR}/${CND_CONF}/${CND_PLATFORM}/kdtree3d 65 | 66 | ${CND_DISTDIR}/${CND_CONF}/${CND_PLATFORM}/kdtree3d: ${OBJECTFILES} 67 | ${MKDIR} -p ${CND_DISTDIR}/${CND_CONF}/${CND_PLATFORM} 68 | ${LINK.cc} -o ${CND_DISTDIR}/${CND_CONF}/${CND_PLATFORM}/kdtree3d ${OBJECTFILES} ${LDLIBSOPTIONS} -lglut -lGL -lGLU -lm 69 | 70 | ${OBJECTDIR}/ConvexHull.o: ConvexHull.cpp 71 | ${MKDIR} -p ${OBJECTDIR} 72 | ${RM} "$@.d" 73 | $(COMPILE.cc) -g -MMD -MP -MF "$@.d" -o ${OBJECTDIR}/ConvexHull.o ConvexHull.cpp 74 | 75 | ${OBJECTDIR}/KdTree.o: KdTree.cpp 76 | ${MKDIR} -p ${OBJECTDIR} 77 | ${RM} "$@.d" 78 | $(COMPILE.cc) -g -MMD -MP -MF "$@.d" -o ${OBJECTDIR}/KdTree.o KdTree.cpp 79 | 80 | ${OBJECTDIR}/Node.o: Node.cpp 81 | ${MKDIR} -p ${OBJECTDIR} 82 | ${RM} "$@.d" 83 | $(COMPILE.cc) -g -MMD -MP -MF "$@.d" -o ${OBJECTDIR}/Node.o Node.cpp 84 | 85 | ${OBJECTDIR}/PointInPolygon.o: PointInPolygon.cpp 86 | ${MKDIR} -p ${OBJECTDIR} 87 | ${RM} "$@.d" 88 | $(COMPILE.cc) -g -MMD -MP -MF "$@.d" -o ${OBJECTDIR}/PointInPolygon.o PointInPolygon.cpp 89 | 90 | ${OBJECTDIR}/Ray.o: Ray.cpp 91 | ${MKDIR} -p ${OBJECTDIR} 92 | ${RM} "$@.d" 93 | $(COMPILE.cc) -g -MMD -MP -MF "$@.d" -o ${OBJECTDIR}/Ray.o Ray.cpp 94 | 95 | ${OBJECTDIR}/main.o: main.cpp 96 | ${MKDIR} -p ${OBJECTDIR} 97 | ${RM} "$@.d" 98 | $(COMPILE.cc) -g -MMD -MP -MF "$@.d" -o ${OBJECTDIR}/main.o main.cpp 99 | 100 | # Subprojects 101 | .build-subprojects: 102 | 103 | # Clean Targets 104 | .clean-conf: ${CLEAN_SUBPROJECTS} 105 | ${RM} -r ${CND_BUILDDIR}/${CND_CONF} 106 | ${RM} ${CND_DISTDIR}/${CND_CONF}/${CND_PLATFORM}/kdtree3d 107 | 108 | # Subprojects 109 | .clean-subprojects: 110 | 111 | # Enable dependency checking 112 | .dep.inc: .depcheck-impl 113 | 114 | include .dep.inc 115 | -------------------------------------------------------------------------------- /3D/nbproject/Makefile-Release.mk: -------------------------------------------------------------------------------- 1 | # 2 | # Generated Makefile - do not edit! 3 | # 4 | # Edit the Makefile in the project folder instead (../Makefile). Each target 5 | # has a -pre and a -post target defined where you can add customized code. 6 | # 7 | # This makefile implements configuration specific macros and targets. 8 | 9 | 10 | # Environment 11 | MKDIR=mkdir 12 | CP=cp 13 | GREP=grep 14 | NM=nm 15 | CCADMIN=CCadmin 16 | RANLIB=ranlib 17 | CC=gcc 18 | CCC=g++ 19 | CXX=g++ 20 | FC=gfortran 21 | AS=as 22 | 23 | # Macros 24 | CND_PLATFORM=GNU-MacOSX 25 | CND_DLIB_EXT=dylib 26 | CND_CONF=Release 27 | CND_DISTDIR=dist 28 | CND_BUILDDIR=build 29 | 30 | # Include project Makefile 31 | include Makefile 32 | 33 | # Object Directory 34 | OBJECTDIR=${CND_BUILDDIR}/${CND_CONF}/${CND_PLATFORM} 35 | 36 | # Object Files 37 | OBJECTFILES= \ 38 | ${OBJECTDIR}/ConvexHull.o \ 39 | ${OBJECTDIR}/KdTree.o \ 40 | ${OBJECTDIR}/Node.o \ 41 | ${OBJECTDIR}/PointInPolygon.o \ 42 | ${OBJECTDIR}/Ray.o \ 43 | ${OBJECTDIR}/main.o 44 | 45 | 46 | # C Compiler Flags 47 | CFLAGS= 48 | 49 | # CC Compiler Flags 50 | CCFLAGS= 51 | CXXFLAGS= 52 | 53 | # Fortran Compiler Flags 54 | FFLAGS= 55 | 56 | # Assembler Flags 57 | ASFLAGS= 58 | 59 | # Link Libraries and Options 60 | LDLIBSOPTIONS= 61 | 62 | # Build Targets 63 | .build-conf: ${BUILD_SUBPROJECTS} 64 | "${MAKE}" -f nbproject/Makefile-${CND_CONF}.mk ${CND_DISTDIR}/${CND_CONF}/${CND_PLATFORM}/kdtree3d 65 | 66 | ${CND_DISTDIR}/${CND_CONF}/${CND_PLATFORM}/kdtree3d: ${OBJECTFILES} 67 | ${MKDIR} -p ${CND_DISTDIR}/${CND_CONF}/${CND_PLATFORM} 68 | ${LINK.cc} -o ${CND_DISTDIR}/${CND_CONF}/${CND_PLATFORM}/kdtree3d ${OBJECTFILES} ${LDLIBSOPTIONS} -framework OpenGL -framework GLUT 69 | 70 | ${OBJECTDIR}/ConvexHull.o: ConvexHull.cpp 71 | ${MKDIR} -p ${OBJECTDIR} 72 | ${RM} "$@.d" 73 | $(COMPILE.cc) -O2 -MMD -MP -MF "$@.d" -o ${OBJECTDIR}/ConvexHull.o ConvexHull.cpp 74 | 75 | ${OBJECTDIR}/KdTree.o: KdTree.cpp 76 | ${MKDIR} -p ${OBJECTDIR} 77 | ${RM} "$@.d" 78 | $(COMPILE.cc) -O2 -MMD -MP -MF "$@.d" -o ${OBJECTDIR}/KdTree.o KdTree.cpp 79 | 80 | ${OBJECTDIR}/Node.o: Node.cpp 81 | ${MKDIR} -p ${OBJECTDIR} 82 | ${RM} "$@.d" 83 | $(COMPILE.cc) -O2 -MMD -MP -MF "$@.d" -o ${OBJECTDIR}/Node.o Node.cpp 84 | 85 | ${OBJECTDIR}/PointInPolygon.o: PointInPolygon.cpp 86 | ${MKDIR} -p ${OBJECTDIR} 87 | ${RM} "$@.d" 88 | $(COMPILE.cc) -O2 -MMD -MP -MF "$@.d" -o ${OBJECTDIR}/PointInPolygon.o PointInPolygon.cpp 89 | 90 | ${OBJECTDIR}/Ray.o: Ray.cpp 91 | ${MKDIR} -p ${OBJECTDIR} 92 | ${RM} "$@.d" 93 | $(COMPILE.cc) -O2 -MMD -MP -MF "$@.d" -o ${OBJECTDIR}/Ray.o Ray.cpp 94 | 95 | ${OBJECTDIR}/main.o: main.cpp 96 | ${MKDIR} -p ${OBJECTDIR} 97 | ${RM} "$@.d" 98 | $(COMPILE.cc) -O2 -MMD -MP -MF "$@.d" -o ${OBJECTDIR}/main.o main.cpp 99 | 100 | # Subprojects 101 | .build-subprojects: 102 | 103 | # Clean Targets 104 | .clean-conf: ${CLEAN_SUBPROJECTS} 105 | ${RM} -r ${CND_BUILDDIR}/${CND_CONF} 106 | ${RM} ${CND_DISTDIR}/${CND_CONF}/${CND_PLATFORM}/kdtree3d 107 | 108 | # Subprojects 109 | .clean-subprojects: 110 | 111 | # Enable dependency checking 112 | .dep.inc: .depcheck-impl 113 | 114 | include .dep.inc 115 | -------------------------------------------------------------------------------- /3D/nbproject/Makefile-impl.mk: -------------------------------------------------------------------------------- 1 | # 2 | # Generated Makefile - do not edit! 3 | # 4 | # Edit the Makefile in the project folder instead (../Makefile). Each target 5 | # has a pre- and a post- target defined where you can add customization code. 6 | # 7 | # This makefile implements macros and targets common to all configurations. 8 | # 9 | # NOCDDL 10 | 11 | 12 | # Building and Cleaning subprojects are done by default, but can be controlled with the SUB 13 | # macro. If SUB=no, subprojects will not be built or cleaned. The following macro 14 | # statements set BUILD_SUB-CONF and CLEAN_SUB-CONF to .build-reqprojects-conf 15 | # and .clean-reqprojects-conf unless SUB has the value 'no' 16 | SUB_no=NO 17 | SUBPROJECTS=${SUB_${SUB}} 18 | BUILD_SUBPROJECTS_=.build-subprojects 19 | BUILD_SUBPROJECTS_NO= 20 | BUILD_SUBPROJECTS=${BUILD_SUBPROJECTS_${SUBPROJECTS}} 21 | CLEAN_SUBPROJECTS_=.clean-subprojects 22 | CLEAN_SUBPROJECTS_NO= 23 | CLEAN_SUBPROJECTS=${CLEAN_SUBPROJECTS_${SUBPROJECTS}} 24 | 25 | 26 | # Project Name 27 | PROJECTNAME=KdTree3D 28 | 29 | # Active Configuration 30 | DEFAULTCONF=Debug 31 | CONF=${DEFAULTCONF} 32 | 33 | # All Configurations 34 | ALLCONFS=Debug Release 35 | 36 | 37 | # build 38 | .build-impl: .build-pre .validate-impl .depcheck-impl 39 | @#echo "=> Running $@... Configuration=$(CONF)" 40 | "${MAKE}" -f nbproject/Makefile-${CONF}.mk QMAKE=${QMAKE} SUBPROJECTS=${SUBPROJECTS} .build-conf 41 | 42 | 43 | # clean 44 | .clean-impl: .clean-pre .validate-impl .depcheck-impl 45 | @#echo "=> Running $@... Configuration=$(CONF)" 46 | "${MAKE}" -f nbproject/Makefile-${CONF}.mk QMAKE=${QMAKE} SUBPROJECTS=${SUBPROJECTS} .clean-conf 47 | 48 | 49 | # clobber 50 | .clobber-impl: .clobber-pre .depcheck-impl 51 | @#echo "=> Running $@..." 52 | for CONF in ${ALLCONFS}; \ 53 | do \ 54 | "${MAKE}" -f nbproject/Makefile-$${CONF}.mk QMAKE=${QMAKE} SUBPROJECTS=${SUBPROJECTS} .clean-conf; \ 55 | done 56 | 57 | # all 58 | .all-impl: .all-pre .depcheck-impl 59 | @#echo "=> Running $@..." 60 | for CONF in ${ALLCONFS}; \ 61 | do \ 62 | "${MAKE}" -f nbproject/Makefile-$${CONF}.mk QMAKE=${QMAKE} SUBPROJECTS=${SUBPROJECTS} .build-conf; \ 63 | done 64 | 65 | # build tests 66 | .build-tests-impl: .build-impl .build-tests-pre 67 | @#echo "=> Running $@... Configuration=$(CONF)" 68 | "${MAKE}" -f nbproject/Makefile-${CONF}.mk SUBPROJECTS=${SUBPROJECTS} .build-tests-conf 69 | 70 | # run tests 71 | .test-impl: .build-tests-impl .test-pre 72 | @#echo "=> Running $@... Configuration=$(CONF)" 73 | "${MAKE}" -f nbproject/Makefile-${CONF}.mk SUBPROJECTS=${SUBPROJECTS} .test-conf 74 | 75 | # dependency checking support 76 | .depcheck-impl: 77 | @echo "# This code depends on make tool being used" >.dep.inc 78 | @if [ -n "${MAKE_VERSION}" ]; then \ 79 | echo "DEPFILES=\$$(wildcard \$$(addsuffix .d, \$${OBJECTFILES} \$${TESTOBJECTFILES}))" >>.dep.inc; \ 80 | echo "ifneq (\$${DEPFILES},)" >>.dep.inc; \ 81 | echo "include \$${DEPFILES}" >>.dep.inc; \ 82 | echo "endif" >>.dep.inc; \ 83 | else \ 84 | echo ".KEEP_STATE:" >>.dep.inc; \ 85 | echo ".KEEP_STATE_FILE:.make.state.\$${CONF}" >>.dep.inc; \ 86 | fi 87 | 88 | # configuration validation 89 | .validate-impl: 90 | @if [ ! -f nbproject/Makefile-${CONF}.mk ]; \ 91 | then \ 92 | echo ""; \ 93 | echo "Error: can not find the makefile for configuration '${CONF}' in project ${PROJECTNAME}"; \ 94 | echo "See 'make help' for details."; \ 95 | echo "Current directory: " `pwd`; \ 96 | echo ""; \ 97 | fi 98 | @if [ ! -f nbproject/Makefile-${CONF}.mk ]; \ 99 | then \ 100 | exit 1; \ 101 | fi 102 | 103 | 104 | # help 105 | .help-impl: .help-pre 106 | @echo "This makefile supports the following configurations:" 107 | @echo " ${ALLCONFS}" 108 | @echo "" 109 | @echo "and the following targets:" 110 | @echo " build (default target)" 111 | @echo " clean" 112 | @echo " clobber" 113 | @echo " all" 114 | @echo " help" 115 | @echo "" 116 | @echo "Makefile Usage:" 117 | @echo " make [CONF=] [SUB=no] build" 118 | @echo " make [CONF=] [SUB=no] clean" 119 | @echo " make [SUB=no] clobber" 120 | @echo " make [SUB=no] all" 121 | @echo " make help" 122 | @echo "" 123 | @echo "Target 'build' will build a specific configuration and, unless 'SUB=no'," 124 | @echo " also build subprojects." 125 | @echo "Target 'clean' will clean a specific configuration and, unless 'SUB=no'," 126 | @echo " also clean subprojects." 127 | @echo "Target 'clobber' will remove all built files from all configurations and," 128 | @echo " unless 'SUB=no', also from subprojects." 129 | @echo "Target 'all' will will build all configurations and, unless 'SUB=no'," 130 | @echo " also build subprojects." 131 | @echo "Target 'help' prints this message." 132 | @echo "" 133 | 134 | -------------------------------------------------------------------------------- /3D/nbproject/Makefile-variables.mk: -------------------------------------------------------------------------------- 1 | # 2 | # Generated - do not edit! 3 | # 4 | # NOCDDL 5 | # 6 | CND_BASEDIR=`pwd` 7 | CND_BUILDDIR=build 8 | CND_DISTDIR=dist 9 | # Debug configuration 10 | CND_PLATFORM_Debug=GNU-MacOSX 11 | CND_ARTIFACT_DIR_Debug=dist/Debug/GNU-MacOSX 12 | CND_ARTIFACT_NAME_Debug=kdtree3d 13 | CND_ARTIFACT_PATH_Debug=dist/Debug/GNU-MacOSX/kdtree3d 14 | CND_PACKAGE_DIR_Debug=dist/Debug/GNU-MacOSX/package 15 | CND_PACKAGE_NAME_Debug=kdtree3d.tar 16 | CND_PACKAGE_PATH_Debug=dist/Debug/GNU-MacOSX/package/kdtree3d.tar 17 | # Release configuration 18 | CND_PLATFORM_Release=GNU-MacOSX 19 | CND_ARTIFACT_DIR_Release=dist/Release/GNU-MacOSX 20 | CND_ARTIFACT_NAME_Release=kdtree3d 21 | CND_ARTIFACT_PATH_Release=dist/Release/GNU-MacOSX/kdtree3d 22 | CND_PACKAGE_DIR_Release=dist/Release/GNU-MacOSX/package 23 | CND_PACKAGE_NAME_Release=kdtree3d.tar 24 | CND_PACKAGE_PATH_Release=dist/Release/GNU-MacOSX/package/kdtree3d.tar 25 | # 26 | # include compiler specific variables 27 | # 28 | # dmake command 29 | ROOT:sh = test -f nbproject/private/Makefile-variables.mk || \ 30 | (mkdir -p nbproject/private && touch nbproject/private/Makefile-variables.mk) 31 | # 32 | # gmake command 33 | .PHONY: $(shell test -f nbproject/private/Makefile-variables.mk || (mkdir -p nbproject/private && touch nbproject/private/Makefile-variables.mk)) 34 | # 35 | include nbproject/private/Makefile-variables.mk 36 | -------------------------------------------------------------------------------- /3D/nbproject/Package-Debug.bash: -------------------------------------------------------------------------------- 1 | #!/bin/bash -x 2 | 3 | # 4 | # Generated - do not edit! 5 | # 6 | 7 | # Macros 8 | TOP=`pwd` 9 | CND_PLATFORM=GNU-MacOSX 10 | CND_CONF=Debug 11 | CND_DISTDIR=dist 12 | CND_BUILDDIR=build 13 | CND_DLIB_EXT=dylib 14 | NBTMPDIR=${CND_BUILDDIR}/${CND_CONF}/${CND_PLATFORM}/tmp-packaging 15 | TMPDIRNAME=tmp-packaging 16 | OUTPUT_PATH=${CND_DISTDIR}/${CND_CONF}/${CND_PLATFORM}/kdtree3d 17 | OUTPUT_BASENAME=kdtree3d 18 | PACKAGE_TOP_DIR=kdtree3d/ 19 | 20 | # Functions 21 | function checkReturnCode 22 | { 23 | rc=$? 24 | if [ $rc != 0 ] 25 | then 26 | exit $rc 27 | fi 28 | } 29 | function makeDirectory 30 | # $1 directory path 31 | # $2 permission (optional) 32 | { 33 | mkdir -p "$1" 34 | checkReturnCode 35 | if [ "$2" != "" ] 36 | then 37 | chmod $2 "$1" 38 | checkReturnCode 39 | fi 40 | } 41 | function copyFileToTmpDir 42 | # $1 from-file path 43 | # $2 to-file path 44 | # $3 permission 45 | { 46 | cp "$1" "$2" 47 | checkReturnCode 48 | if [ "$3" != "" ] 49 | then 50 | chmod $3 "$2" 51 | checkReturnCode 52 | fi 53 | } 54 | 55 | # Setup 56 | cd "${TOP}" 57 | mkdir -p ${CND_DISTDIR}/${CND_CONF}/${CND_PLATFORM}/package 58 | rm -rf ${NBTMPDIR} 59 | mkdir -p ${NBTMPDIR} 60 | 61 | # Copy files and create directories and links 62 | cd "${TOP}" 63 | makeDirectory "${NBTMPDIR}/kdtree3d/bin" 64 | copyFileToTmpDir "${OUTPUT_PATH}" "${NBTMPDIR}/${PACKAGE_TOP_DIR}bin/${OUTPUT_BASENAME}" 0755 65 | 66 | 67 | # Generate tar file 68 | cd "${TOP}" 69 | rm -f ${CND_DISTDIR}/${CND_CONF}/${CND_PLATFORM}/package/kdtree3d.tar 70 | cd ${NBTMPDIR} 71 | tar -vcf ../../../../${CND_DISTDIR}/${CND_CONF}/${CND_PLATFORM}/package/kdtree3d.tar * 72 | checkReturnCode 73 | 74 | # Cleanup 75 | cd "${TOP}" 76 | rm -rf ${NBTMPDIR} 77 | -------------------------------------------------------------------------------- /3D/nbproject/Package-Release.bash: -------------------------------------------------------------------------------- 1 | #!/bin/bash -x 2 | 3 | # 4 | # Generated - do not edit! 5 | # 6 | 7 | # Macros 8 | TOP=`pwd` 9 | CND_PLATFORM=GNU-MacOSX 10 | CND_CONF=Release 11 | CND_DISTDIR=dist 12 | CND_BUILDDIR=build 13 | CND_DLIB_EXT=dylib 14 | NBTMPDIR=${CND_BUILDDIR}/${CND_CONF}/${CND_PLATFORM}/tmp-packaging 15 | TMPDIRNAME=tmp-packaging 16 | OUTPUT_PATH=${CND_DISTDIR}/${CND_CONF}/${CND_PLATFORM}/kdtree3d 17 | OUTPUT_BASENAME=kdtree3d 18 | PACKAGE_TOP_DIR=kdtree3d/ 19 | 20 | # Functions 21 | function checkReturnCode 22 | { 23 | rc=$? 24 | if [ $rc != 0 ] 25 | then 26 | exit $rc 27 | fi 28 | } 29 | function makeDirectory 30 | # $1 directory path 31 | # $2 permission (optional) 32 | { 33 | mkdir -p "$1" 34 | checkReturnCode 35 | if [ "$2" != "" ] 36 | then 37 | chmod $2 "$1" 38 | checkReturnCode 39 | fi 40 | } 41 | function copyFileToTmpDir 42 | # $1 from-file path 43 | # $2 to-file path 44 | # $3 permission 45 | { 46 | cp "$1" "$2" 47 | checkReturnCode 48 | if [ "$3" != "" ] 49 | then 50 | chmod $3 "$2" 51 | checkReturnCode 52 | fi 53 | } 54 | 55 | # Setup 56 | cd "${TOP}" 57 | mkdir -p ${CND_DISTDIR}/${CND_CONF}/${CND_PLATFORM}/package 58 | rm -rf ${NBTMPDIR} 59 | mkdir -p ${NBTMPDIR} 60 | 61 | # Copy files and create directories and links 62 | cd "${TOP}" 63 | makeDirectory "${NBTMPDIR}/kdtree3d/bin" 64 | copyFileToTmpDir "${OUTPUT_PATH}" "${NBTMPDIR}/${PACKAGE_TOP_DIR}bin/${OUTPUT_BASENAME}" 0755 65 | 66 | 67 | # Generate tar file 68 | cd "${TOP}" 69 | rm -f ${CND_DISTDIR}/${CND_CONF}/${CND_PLATFORM}/package/kdtree3d.tar 70 | cd ${NBTMPDIR} 71 | tar -vcf ../../../../${CND_DISTDIR}/${CND_CONF}/${CND_PLATFORM}/package/kdtree3d.tar * 72 | checkReturnCode 73 | 74 | # Cleanup 75 | cd "${TOP}" 76 | rm -rf ${NBTMPDIR} 77 | -------------------------------------------------------------------------------- /3D/nbproject/configurations.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 7 | ConvexHull.h 8 | IntersectRayAABB.h 9 | IntersectRayTriangle.h 10 | KdTree.h 11 | Node.h 12 | Point2D.h 13 | PointInPolygon.h 14 | Ray.h 15 | Render.h 16 | Timer.h 17 | Tipos.h 18 | Vector2D.h 19 | Vector3D.h 20 | 21 | 24 | ConvexHull.cpp 25 | KdTree.cpp 26 | Node.cpp 27 | PointInPolygon.cpp 28 | Ray.cpp 29 | main.cpp 30 | 31 | 34 | 35 | 39 | 40 | 44 | Makefile 45 | 46 | 47 | Makefile 48 | 49 | 50 | 51 | default 52 | true 53 | false 54 | 55 | 56 | 57 | -Wall 58 | 59 | 60 | -lglut -lGL -lGLU -lm 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 | default 105 | true 106 | false 107 | 108 | 109 | 110 | 5 111 | 112 | 113 | 5 114 | 115 | 116 | 5 117 | 118 | 119 | 5 120 | 121 | 122 | -framework OpenGL -framework GLUT 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 | -------------------------------------------------------------------------------- /3D/nbproject/private/Makefile-variables.mk: -------------------------------------------------------------------------------- 1 | # 2 | # Generated - do not edit! 3 | # 4 | # NOCDDL 5 | # 6 | # Debug configuration 7 | # Release configuration 8 | -------------------------------------------------------------------------------- /3D/nbproject/private/configurations.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | Makefile 4 | 5 | 6 | 7 | localhost 8 | 4 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | gdb 24 | 25 | 26 | 27 | "${OUTPUT_PATH}" 28 | 29 | "${OUTPUT_PATH}" 30 | 31 | true 32 | 0 33 | 0 34 | 35 | 36 | 37 | 38 | 39 | 40 | localhost 41 | 4 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | gdb 57 | 58 | 59 | 60 | "${OUTPUT_PATH}" 61 | 62 | "${OUTPUT_PATH}" 63 | 64 | true 65 | 0 66 | 0 67 | 68 | 69 | 70 | 71 | 72 | 73 | -------------------------------------------------------------------------------- /3D/nbproject/private/launcher.properties: -------------------------------------------------------------------------------- 1 | # Launchers File syntax: 2 | # 3 | # [Must-have property line] 4 | # launcher1.runCommand= 5 | # [Optional extra properties] 6 | # launcher1.displayName= 7 | # launcher1.buildCommand= 8 | # launcher1.runDir= 9 | # launcher1.symbolFiles= 10 | # launcher1.env.= 11 | # (If this value is quoted with ` it is handled as a native command which execution result will become the value) 12 | # [Common launcher properties] 13 | # common.runDir= 14 | # (This value is overwritten by a launcher specific runDir value if the latter exists) 15 | # common.env.= 16 | # (Environment variables from common launcher are merged with launcher specific variables) 17 | # common.symbolFiles= 18 | # (This value is overwritten by a launcher specific symbolFiles value if the latter exists) 19 | # 20 | # In runDir, symbolFiles and env fields you can use these macroses: 21 | # ${PROJECT_DIR} - project directory absolute path 22 | # ${OUTPUT_PATH} - linker output path (relative to project directory path) 23 | # ${OUTPUT_BASENAME}- linker output filename 24 | # ${TESTDIR} - test files directory (relative to project directory path) 25 | # ${OBJECTDIR} - object files directory (relative to project directory path) 26 | # ${CND_DISTDIR} - distribution directory (relative to project directory path) 27 | # ${CND_BUILDDIR} - build directory (relative to project directory path) 28 | # ${CND_PLATFORM} - platform name 29 | # ${CND_CONF} - configuration name 30 | # ${CND_DLIB_EXT} - dynamic library extension 31 | # 32 | # All the project launchers must be listed in the file! 33 | # 34 | # launcher1.runCommand=... 35 | # launcher2.runCommand=... 36 | # ... 37 | # common.runDir=... 38 | # common.env.KEY=VALUE 39 | 40 | # launcher1.runCommand= -------------------------------------------------------------------------------- /3D/nbproject/private/private.properties: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/paulaceccon/KdTree4TriangularMeshes/b7b2bc03fecdd2734e7ee38b152594d6a7a8ba80/3D/nbproject/private/private.properties -------------------------------------------------------------------------------- /3D/nbproject/private/private.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 1 5 | 1 6 | 7 | 8 | 9 | 10 | 11 | file:/Users/paulaceccon/NetBeansProjects/KdTree3D/Point2D.h 12 | file:/Users/paulaceccon/NetBeansProjects/KdTree3D/main.cpp 13 | 14 | 15 | 16 | -------------------------------------------------------------------------------- /3D/nbproject/project.properties: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/paulaceccon/KdTree4TriangularMeshes/b7b2bc03fecdd2734e7ee38b152594d6a7a8ba80/3D/nbproject/project.properties -------------------------------------------------------------------------------- /3D/nbproject/project.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | org.netbeans.modules.cnd.makeproject 4 | 5 | 6 | KdTree3D 7 | 8 | cpp 9 | h 10 | UTF-8 11 | 12 | 13 | 14 | 15 | Debug 16 | 1 17 | 18 | 19 | Release 20 | 1 21 | 22 | 23 | 24 | false 25 | 26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # KdTree4TriangularMeshes 2 | 3 | ### Algorithm 4 | 5 | ``` 6 | Node *buildKdTree(ini, end, triangles) 7 | { 8 | sort triangles by k-n centroid coordinate 9 | median = (ini + end)/2 10 | node.triangle = triangles[med] 11 | node.left = buildKdTree(ini, median, points) 12 | node.right = buildKdTree(median, fim, points) 13 | node.aabb = union(node.triangle.aabb, node.left.aabb, node.right.aabb) 14 | return node 15 | } 16 | ``` 17 | 18 | --- 19 | 20 | ## 2D Folder 21 | A KdTree implementation for triangular 2D mesh. 22 | - Keys: 23 | * d: Minus one depth 24 | * D: Plus one depth 25 | * v: View the whole kd-tree 26 | 27 | - The Triangles: 28 | 29 | ![The Triangles](https://github.com/paulaceccon/KdTree4TriangularMeshes/blob/master/2D/Screenshots/depth0.png) 30 | 31 | - kd-Tree per depth: 32 | 33 | ![alt text](https://github.com/paulaceccon/KdTree4TriangularMeshes/blob/master/2D/Screenshots/depth1.png) 34 | ![alt text](https://github.com/paulaceccon/KdTree4TriangularMeshes/blob/master/2D/Screenshots/depth2.png) 35 | ![alt text](https://github.com/paulaceccon/KdTree4TriangularMeshes/blob/master/2D/Screenshots/depth3.png) 36 | 37 | - The whole kd-Tree: 38 | 39 | ![alt text](https://github.com/paulaceccon/KdTree4TriangularMeshes/blob/master/2D/Screenshots/depth4.png) 40 | 41 | --- 42 | 43 | ## 3D Folder 44 | A KdTree implementation for triangular 3D mesh. 45 | 46 | - Keys: 47 | * A: Open mesh 48 | * b: Minus one depth 49 | * B: Plus one depth 50 | * g: Turn grid on/off 51 | * p: Fill surface 52 | 53 | --- 54 | 55 | - kd-Tree per depth: 56 | 57 | ![alt text](https://github.com/paulaceccon/KdTree4TriangularMeshes/blob/master/3D/Screenshots/depth0.png) 58 | ![alt text](https://github.com/paulaceccon/KdTree4TriangularMeshes/blob/master/3D/Screenshots/depth1.png) 59 | ![alt text](https://github.com/paulaceccon/KdTree4TriangularMeshes/blob/master/3D/Screenshots/depth2.png) 60 | ![alt text](https://github.com/paulaceccon/KdTree4TriangularMeshes/blob/master/3D/Screenshots/depth3.png) 61 | ![alt text](https://github.com/paulaceccon/KdTree4TriangularMeshes/blob/master/3D/Screenshots/depth4.png) 62 | ![alt text](https://github.com/paulaceccon/KdTree4TriangularMeshes/blob/master/3D/Screenshots/depth5.png) 63 | --------------------------------------------------------------------------------