00001 /* 00002 File: VertexJoin.cc 00003 00004 Function: Join up disconnected mesh. 00005 00006 Author: Andrew Willmott 00007 00008 Notes: Call SetBounds, and then AddVertex to get new ids for the 00009 vertices. When finished call GetFinalVertexList() to retrieve 00010 the new list of vertices. 00011 */ 00012 00013 #include "gcl/VertexJoin.h" 00014 #include <stdio.h> 00015 00016 #define VJ_DBG if (0) printf 00017 00018 Void VertexJoin::Init(const Point &min, const Point &max, Int numVertices) 00019 { 00020 Int i, gridSize; 00021 00022 VJ_DBG("Starting init [%d]\n", numVertices); 00023 vertices = new PointList; 00024 vertices->PreAllocate(numVertices); 00025 nextList.SetSize(numVertices); 00026 idList.SetSize(numVertices); 00027 00028 SELF.max = max; 00029 SELF.min = min; 00030 00031 // for now, we hard-code the grid size 00032 cells[0] = 32; 00033 cells[1] = 32; 00034 cells[2] = 32; 00035 gridSize = cells[0] * cells[1] * cells[2]; 00036 grid = new Int[gridSize]; 00037 00038 for (i = 0; i < gridSize; i++) 00039 grid[i] = -1; 00040 00041 for (i = 0; i < numVertices; i++) 00042 nextList[i] = -1; 00043 00044 VJ_DBG("done\n"); 00045 } 00046 00047 Int VertexJoin::AddVertex(const Point &where, Int id) 00048 { 00049 Int vcell[3], *indexPtr; 00050 Vector bDelta, delta; 00051 00052 // find cell of the grid. 00053 00054 VJ_DBG("Adding point...\n"); 00055 00056 bDelta = max - min; 00057 delta = where - min; 00058 00059 VJ_DBG("d1: %g %g %g d2: %g %g %g\n", 00060 bDelta[0], bDelta[1], bDelta[2], delta[0], delta[1], delta[2]); 00061 00062 delta /= bDelta; 00063 delta *= Vector(cells[0], cells[1], cells[2]); 00064 00065 VJ_DBG("where: %g %g %g delta: %g %g %g\n", 00066 where[0], where[1], where[2], delta[0], delta[1], delta[2]); 00067 00068 vcell[0] = (Int) delta[0]; 00069 if (vcell[0] >= cells[0]) vcell[0] = cells[0] - 1; 00070 vcell[1] = (Int) delta[1]; 00071 if (vcell[1] >= cells[1]) vcell[1] = cells[1] - 1; 00072 vcell[2] = (Int) delta[2]; 00073 if (vcell[2] >= cells[2]) vcell[2] = cells[2] - 1; 00074 00075 VJ_DBG("cell %d %d %d from %d %d %d\n", vcell[0], vcell[1], vcell[2], 00076 cells[0], cells[1], cells[2]); 00077 indexPtr = grid + (vcell[0] + vcell[1] * cells[0] + 00078 vcell[2] * cells[0] * cells[1]); 00079 00080 // okay, now we search for a vertex match 00081 while (*indexPtr >= 0) 00082 { 00083 VJ_DBG("index %d\n", *indexPtr); 00084 // return current index if we match... 00085 if ((id == idList[*indexPtr]) && 00086 MatchVertex(where, (*vertices)[*indexPtr])) 00087 { 00088 VJ_DBG("hit at index %d\n", *indexPtr); 00089 return(*indexPtr); 00090 } 00091 00092 // find next entry in linked list... 00093 indexPtr = &(nextList[*indexPtr]); 00094 } 00095 00096 // no match... so add the new vertex. 00097 00098 *indexPtr = vertices->NumItems(); 00099 VJ_DBG("adding new vertex at index %d\n", *indexPtr); 00100 vertices->Append(where); 00101 idList[*indexPtr] = id; 00102 return(*indexPtr); 00103 } 00104 00105 00106 PointList *VertexJoin::GetFinalVertexList() 00107 { 00108 PointList *result; 00109 00110 delete grid; 00111 grid = 0; 00112 result = vertices; 00113 vertices = 0; 00114 idList.SetSize(0); 00115 nextList.SetSize(0); 00116 00117 return(result); 00118 } 00119 00120 Bool VertexJoin::MatchVertex(const Point &a, const Point &b) 00121 { 00122 // hack for now 00123 const GCLReal eps = 1e-5; 00124 00125 return(len(a - b) < eps); 00126 } 00127