GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — old/FaceUtils.js at master · vfasteng/old Expand file tree/ Copy pathFaceUtils.jsMore file actions 207 lines (149 loc) · 5.74 KB / Copy pathFaceUtils.jsFile metadata and controls207 lines (149 loc) · 5.74 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 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 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 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 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 import * as _ from './JSUtils'; import * as THREE from 'three'; export default class FaceUtils { constructor() { this.clamp = true; this.pendingRecursive = 0; this.__vertexNames = ['a','b','c']; } vertexHash(geometry) { geometry.vertexHash = []; var faces = geometry.faces; var vLen = geometry.vertices.length; for(var i=0;i<vLen;i++){ geometry.vertexHash[i] = []; for(var f in faces){ if(faces[f].a == i || faces[f].b == i || faces[f].c == i){ geometry.vertexHash[i].push(faces[f])}; } } } __ensureHash(geometry) { if(geometry.vertexHash == undefined){ this.vertexHash(geometry); } } getCoplanar(maxAngle, geometry, face, clamp, out, originFace) { if(this.originFace == undefined){ this.originFace = face; } this.result = out; if(out == undefined){ this.result = {count:0}; } this.__ensureHash(geometry); this.pendingRecursive++; for (var i in this.__vertexNames) { var vertexIndex = face[this.__vertexNames[i]]; var adjacentFaces = geometry.vertexHash[vertexIndex]; for(var a in adjacentFaces) { var newface = adjacentFaces[a]; var testF = this.originFace; if(clamp == false){ testF = face } if(testF.normal.angleTo(newface.normal) * (180/ Math.PI) <= maxAngle) { if(this.result["f"+newface.a+newface.b+newface.c] == undefined) { this.result["f"+newface.a+newface.b+newface.c] = newface; this.result.count++; this.getCoplanar(maxAngle, geometry, newface, clamp, this.result, this.originFace); } } } } this.pendingRecursive--; if(this.pendingRecursive == 0 && this.onCoplanar != undefined){ delete this.result.count; this.onCoplanar(this.result); } } /** * @param {THREE.Geometry} geometry - The mesh geometry * @param {THREE.Face3} face -The intersected face * @param {THREE.Vector3} point - The point in world space * @param {THREE.Matrix4} matrixWorld - The world matrix * @returns {Object} - The nearest vertex */ getNearestVertex(geometry, face, point, matrixWorld) { var worldToLocal = new THREE.Matrix4(); worldToLocal.getInverse(matrixWorld); point.applyMatrix4(worldToLocal); const faceVerticies = this.__vertexNames.map((v) => { const index = face[v]; const vertex = geometry.vertices[index]; return { vertex, distance: point.distanceToSquared(vertex), index: index }; }); var min = Number.MAX_SAFE_INTEGER; var minVertInfo = faceVerticies[0]; faceVerticies.forEach( vertexInfo => { if(vertexInfo.distance < min) { min = vertexInfo.distance; minVertInfo = vertexInfo; } }); return minVertInfo; } distancePointToSegment(v, a, b ) { var ab = b.clone().sub(a); var av = v.clone().sub(a); if (av.dot(ab) <= 0.0) return av.length(); var bv = v.clone().sub(b) ; if (bv.dot(ab) >= 0.0) return bv.length() return (ab.clone().cross( av )).length() / ab.length(); } /** * @param {THREE.Geometry} geometry - The geometry of the mesh * @param {THREE.Face3} face -The face the point intersects * @param {THREE.Vector3} point - The point in world space * @param {THREE.Matrix4} matrixWorld - matrixWorld * @returns {Array<THREE.Vector3>} - The two verticies of the closest edge transformed to world space */ getNearestEdge(geometry, face, point, matrixWorld) { const verts = this.__vertexNames.map((v) => { const index = face[v]; return geometry.vertices[index].clone().applyMatrix4(matrixWorld); }); const distances = [ this.distancePointToSegment(point, verts[0], verts[1]), this.distancePointToSegment(point, verts[1], verts[2]), this.distancePointToSegment(point, verts[2], verts[0]) ]; const max = _.indexOfMin(distances); if(max === 0) { return [verts[0], verts[1]]; } else if (max === 1) { return [verts[1], verts[2]]; } return [verts[2], verts[0]]; } getNearestEdgeLocal(geometry, face, point, matrixWorld) { var worldToLocal = new THREE.Matrix4(); worldToLocal.getInverse(matrixWorld); point.applyMatrix4(worldToLocal); const verts = this.__vertexNames.map((v) => { const index = face[v]; return { vector: geometry.vertices[index].clone(), index: index } }); const distances = [ this.distancePointToSegment(point, verts[0].vector, verts[1].vector), this.distancePointToSegment(point, verts[1].vector, verts[2].vector), this.distancePointToSegment(point, verts[2].vector, verts[0].vector) ]; const max = _.indexOfMin(distances); if(max === 0) { return [verts[0], verts[1]]; } else if (max === 1) { return [verts[1], verts[2]]; } return [verts[2], verts[0]]; } } |
| You are visitor 01402792 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |